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

What is wrong with line 6?

Asked by 9 years ago
Im trying to spawn a part called "wall1"

when someone touches the part with this script and has more than 4 points but I dont think it recognizes that. this is the script so far i wrote. output says 16:53:39.847 - Workspace.Part.Script:5: attempt to compare number with nil 16:53:39.847 - Stack Begin 16:53:39.848 - Script 'Workspace.Part.Script', Line 5 16:53:39.849 - Stack End

function Buy()
        player = game:GetService("Players").LocalPlayer
        stats = player:findFirstChild("leaderstats")
        Points = stats:findFirstChild("points")
    if Points > 4 then
            Points.Value = Points.Value - 5
                wall = Instance.new("Part")
                wall.Parent = workspace
                wall.Size = Vector3.new(5 ,5 , 1)
            wall.Postition = Vector3.new(10 ,5 ,5)
            wall.Anchored = true
            script.Parent:Destroy()


    end
    end


    script.Parent.Touched:connect(Buy)

3 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

As the error says, you are "attempt[ing] to compare [a] number to nil".

The number is 4, so what must be nil is Points.

Points = stats:findFirstChild("points"). :FindFirstChild returns nil when there is no such object.

There is no value called "points" in the leaderstats. This is case-sensitive -- is your value called "Points"?

Once that is fixed, you will get a different error. Something like "attempt to compare number to userdata".

That's because Points is a NumberValue, not a number. You have to get its .Value like you do on the next line.

if Points.Value > 4 then

Cleanup

You should tab your code correctly. Precisely one indent increase per block.

You should use local variables -- defining variables without local makes it really easy to make mistakes.

You should consider using >= 5 instead of > 4. It's a lot easier to not think and subtract one.

Ad
Log in to vote
1
Answered by 9 years ago

You're ending the function if they have more than 4 points,. Return ends the function and returns any arguments after it.

Line 5 should be

if points < 4 then

Here's the page in the Lua reference about functions and return http://www.lua.org/pil/5.1.html

Log in to vote
0
Answered by 9 years ago

Answer this question