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

Why is tonumber not converting my string?

Asked by 10 years ago

-- SOLVED -- I had made a stupid mistake with "Pack1 = script.Parent" because the numbered TextButtons are actually script.Parent.Pack1!

Hi guys. I have this script that changes a decal depending on if a player has completed a level. All of the children of Pack1 are numbered 1 through to 12. This script has broken as I have commented down below.

Help much appreciated!

while true do
    wait(2)
    LevelsCompleted = game.Players.LocalPlayer.LevelsCompleted
    Pack1 = script.Parent:GetChildren()

    for i,child in pairs(Pack1) do
        Level = tonumber(child.Name) 
        if Level <= LevelsCompleted.Value then -- this line errors. It says "cannot compare string with a number" or something similar.
            child.ImageLabel.Image = "http://www.roblox.com/asset?id=173263900"
        elseif (Level + 1) == LevelsCompleted.Value then
            child.ImageLabel.Image = "http://www.roblox.com/asset?id=173701607"
        else
            child.ImageLabel.Image = "http://www.roblox.com/asset?id=173701600"

        end
    end

end
0
On retesting the error has changed to "attempting to compare nil with a number" (Line 8). Not sure whats going on :p MasterDaniel 320 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago

Check if the child's name is not a string...

while true do
    wait(2)
    LevelsCompleted = game.Players.LocalPlayer.LevelsCompleted
    Pack1 = script.Parent:GetChildren()

    for i,child in pairs(Pack1) do
    if tonumber(child.Name)  ~= nil then --Checks if the name is not a string.
        Level = tonumber(child.Name) 
        if Level <= LevelsCompleted.Value then -- this line errors. It says "cannot compare string with a number" or something similar.
            child.ImageLabel.Image = "http://www.roblox.com/asset?id=173263900"
        elseif (number + 1) == LevelsCompleted.Value then
            child.ImageLabel.Image = "http://www.roblox.com/asset?id=173701607"
        else
            child.ImageLabel.Image = "http://www.roblox.com/asset?id=173701600"

        end
    end
    end

end

Sorry if this doesn't work, if it doesn't then i'll try look for a solution.

0
Still doesn't work :S. The childs names are 1,2,3 through to 12 (there are 12 children) but for some reason tonumber isn't converting the name from a string into a number :/ MasterDaniel 320 — 10y
0
Is the LevelsCompleted value a NumberValue/IntValue? Cause if it is a StringValue then you may have to change that to a number by doing tonumber(s) ROBLOXING213 130 — 10y
0
Its a NumberValue :/ MasterDaniel 320 — 10y
0
I found out what I was doing wrong. Thanks for the help anyway MasterDaniel 320 — 10y
0
Nice job, stupid mistakes always get me confused ROBLOXING213 130 — 10y
Ad

Answer this question