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

Why won't my changed function work?

Asked by 10 years ago

For some strange reason, NUMBER 2 & NUMBER 3 are not changing text. But NUMBER 1 & NUMBER 4 are. Please can you help me? Thank you!

Credit will be given at my place as well as a correct answer :)

FanOnOne=game.Workspace.FanValues.One
FanOnTwo=game.Workspace.FanValues.Two
FanOnThree=game.Workspace.FanValues.Three
FanOff=game.Workspace.FanValues.Off

FanOnOne.Changed:connect(function()

    if FanOnOne.Value == true then -- NUMBER 1
        script.Parent.Text = "Fan Power: One"

    elseif FanOnTwo.Value == true then -- NUMBER 2
        script.Parent.Text = "Fan Power: Two"

    elseif FanOnThree.Value == true then -- NUMBER 3
        script.Parent.Text = "Fan Power: Three"

    elseif FanOff.Value == true then -- NUMBER 4
        script.Parent.Text = "Fan Power: Off"
    end
end)

1 answer

Log in to vote
2
Answered by 10 years ago

You're only connecting a function to the Changed event of only one of the objects. You would need to have a separate Changed event for all of the values.

FanOnOne=game.Workspace.FanValues.One
FanOnTwo=game.Workspace.FanValues.Two
FanOnThree=game.Workspace.FanValues.Three
FanOff=game.Workspace.FanValues.Off

FanOnOne.Changed:connect(function()
    if FanOnOne.Value == true then -- NUMBER 1
        script.Parent.Text = "Fan Power: One"
    end
end)

FanOnTwo.Changed:connect(function()
    if FanOnTwo.Value == true then -- NUMBER 1
        script.Parent.Text = "Fan Power: Two"
    end
end)

--And so on...

An easier way to do this:

You could make what you're doing easier by putting all your value objects into a table and then use a for loop to connect a function to the Changed event of each item in the table.

FanValues = {game.Workspace.FanValues.One, game.Workspace.FanValues.Two, game.Workspace.FanValues.Three, game.Workspace.FanValues.Off} --FanValues all in a table.

for i,v in pairs(FanValues) do --Loop through all of the values in the table
    ypcall(function() --Stops the script from breaking if it errors, allows yielding as opposed to pcall.
        v.Changed:connect(function() --Connects a function to the Changed event of the value of the table that the for loop is currently looping through. 
            if v.Value == true then --If the value of the object is true then
                script.Parent.Text = "Fan Power: "..v.Name --Sets the Text of the script's parent to "Fan Power: " then the name of the value object.
            end
        end)
    end)
end

Click here for more information on pcall()

Click here for more information on ypcall()

0
Wow! I diddn't know pcall() and ypcall() existed! Thank you for such a detailed and informative answer! :) WelpNathan 307 — 10y
0
You're welcome. The "easier way" is really useful for situations where you want to link a single function to multiple objects without typing masses of connection lines or copying scripts into each item. I had this issue once and the "easier way" proved to work great! Spongocardo 1991 — 10y
0
Haha! Well, defiantly going to take some practise but defiantly simple! :D WelpNathan 307 — 10y
Ad

Answer this question