Attempt to perform arithmetic on a string value

I’m working on a translator from Normal -> 12 year old AOLer using this website as a base: http://ssshotaru.homestead.com/files/aolertranslator.html

It’s mainly finished but upon calling the chat hook to translate it, it throws up the error in the title, or to be more precise:

‘Hook ‘ISaid’ Failed: [addons\evolve\lua\ev_plugins\sh_derpchat2.lua:25] attempt to perform arithmetic on a string value’

The entire lua code for this is here:

Any help?

if (textArray* == (“YOUR”) & wordLength == 4) || (textArray* == (“YOU’RE”) & wordLength == 6)

You have to use && instead of &, & is a binary operator.

Also, using || and && in lua is bad practise, use “or” and “and” instead.

Also please learn how to regex!

Did a find and replace on the code:

|| with or
&& with and

Same error.

You have single-ampersand operators (&) in places. Change those to either “&&” or “and” and you’re set.

Preferably “and”, because it’s pure Lua.

You’re using + for string concatenation, that’s the reason it errors. The concatenation operator is …, like so:

[lua]
local blah = “a string”
blah = blah … " works like this"[/lua]

I updated the code to this (Updated OP too):

Getting the following error while attempting to use it:

‘Hook ‘ISaid’ Failed: [addons\evolve\lua\ev_plugins\sh_derpchat2.lua:33] attempt to concatenate field ‘?’ (a nil value)’

You are doing a for loop from 1 to the length of “textArray”, yet you try to index textArray[i+1] and i+2 at some point. This code is bound to error…

How strange, I’m just basically attempting rewrite from the original:

You’re going out of bounds on the array. You need to make sure i is one, otherwise you’d be trying to concatenate an index of the array that doesn’t exist.

Try:

[lua]

if(table.Count(textArray) >= 3 and (textArray* … " " … textArray[i+1] … " " … textArray[i+2]) == (“GOT TA GO”) and i == 1) then

[/lua]

Seemed to work for line 33, not with 38 though…

Hook ‘ISaid’ Failed: [addons\evolve\lua\ev_plugins\sh_derpchat2.lua:38] attempt to concatenate field ‘?’ (a nil value)

Code updated:

You need to look at the order you are doing operations. Here, it’ll try to go i+1, then after that check for i == 1. You need to do the latter first. As such:

[lua]
if(table.Count(textArray) >= 2 and i == 1 and (textArray* … " " … textArray[i+1]) == (“GOTTA GO”))
[/lua]

An alternative way to do it, is to check if that array index exist, like so:

[lua]
if(table.Count(textArray) >= 2 and textArray* and (textArray* … " " … textArray[i+1]) == “GOTTA GO” then
[/lua]

Bold tag’s don’t work in code D:

I will try this now…

Yeah, sorry, edit borked out some of my post, adding some BBtags. Should be fixed now.

Okay:



DerpChat is on!
Text = HELLO THERE SMALL CHILD
1	=	HELLO
2	=	THERE
3	=	SMALL
4	=	CHILD
arrayLength = 4
wordLength = 5
Output = THEIR
Hook 'ISaid' Failed: [addons\evolve\lua\ev_plugins\sh_derpchat2.lua:170] attempt to compare nil with number
(Owner) Dr. Matt: hello there small child