Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Making a value into a string error?

Asked by
Mystdar 352 Moderation Voter
8 years ago

I am trying to get some values, which works, make them a string and put them into a table, however this is not working:

function Move(Unit)
    local PositionX = tonumber(Unit.Location.Value:sub(1,1))
    print (PositionX)
    local PositionZ = tonumber(Unit.Location.Value:sub(3,3))
    local Adjacent = {}
    local Position1 = string(PositionX..","..(tonumber(PositionZ) +1))
    local Position2 = string(PositionX..","..tonumber(PositionZ) -1)
    local Position3 = string(tonumber(PositionX) +1 ..","..PositionZ)
    local Position4 = string(tonumber(PositionX) -1 ..","..PositionZ)
    table.insert(Adjacent, Position1)
    table.insert(Adjacent, Position2)
    table.insert(Adjacent, Position3)
    table.insert(Adjacent, Position4)
    for i=1, 4 do
        game.Workspace.Plates[Adjacent[i]].Material = "Neon"
    end
end 

Error: 14:56:08.739 - Workspace.Script:6: attempt to call global 'string' (a table value)

2 answers

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

tonumber takes a numerical string and converts it into a number type. Similarly, tostring converts things into a string.

string is actually a library, so it thinks you're doing something with that. Write tostring instead.

Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

You can't just make up a function an expect it to work.

To convert something into a string, use tostring. It receives an argument of any type and literally converts it to string format.

local str = tostring(5)
print(type(str)) --> string
4
nob User#5978 25 — 8y
1
nob [2] ConnorXV 5 — 8y
0
:( Perci1 4988 — 8y
0
I wasn't "making up a function an expecting it to work", string came up in a special font colour so I presumed it was an in-built function. Mystdar 352 — 8y
0
Right, but you didn't know what it did, so just look that stuff up in the future. Also, it's rather annoying to accept the answer that came 3 hours later, gave the same advice, but didn't include a code snippet... Perci1 4988 — 8y

Answer this question