Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't my changed function work?

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

✓ Accepted answer

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

3 comments

0

Wow! I diddn't know pcall() and ypcall() existed! Thank you for such a detailed and informative answer! :)

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!

0

Haha! Well, defiantly going to take some practise but defiantly simple! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.