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)
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()