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

How to fix this changed event function?

Asked by
Kitorari 266 Moderation Voter
7 years ago
ValueTest = game.workspace.Partscan.Sugar.Value

SugarLabel = game.workspace.Partno.SurfaceGui.Frame.Sugar


game.workspace.Partscan.Sugar.Value.Changed(function(Scanthiscrap)
    if ValueTest == 0 then

    SugarLabel.Text = "Sugar 0/1"

elseif ValueTest == 1 then

   SugarLabel.Text = "Sugar 1/1"

end
end)

Error:20:06:02.864 - Workspace.Partno.Script:6: attempt to index field 'Value' (a number value)

I'm not sure what this error means.. Help please?

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Future reference

Analyze your error messages. Even if it looks like something you deem confusing, the output will tell you first-hand what's not working in your program in the simplest way possible.

Error

Just like your error message states, you're trying to index a number value for Changed - which is a complete logic error. You probably meant to use the Changed event on the actual object, and not it's value.

It also seems that you tried indexing game for workspace. This is another problem, since Lua is case-sensitive and will not recognize workspace as an actual variable. You'd either replace it with game.Workspace, or just the built-in variable workspace, which is the one you should be using.

-- Have reasonable variable names, and please use WaitForChild as well.
local sugar = workspace:WaitForChild("Partscan"):WaitForChild("Sugar")
local SugarLabel = workspace:WaitForChild("Partno"):WaitForChild("SurfaceGui"):WaitForChild("Frame").Sugar

sugar.Changed:connect(function(prop)
    if sugar.Value == 0 then
        SugarLabel.Text = "Sugar 0/1"
    elseif sugar.Value == 1 then
        SugarLabel.Text = "Sugar 1/1"
    end
end)

If you have any questions, let me know.

0
Actually, I'm using a Numbervalue, not a object... Kitorari 266 — 7y
1
A NumberValue is an object. ScriptGuider 5640 — 7y
Ad

Answer this question