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

How to use a function when a value "Changes"?

So, I made a map loading script, and the problem is that the script, is ment to pick one of the 5 maps randomly, it works, the porblem is that it does it too many times, it's only ment to do it once. Well, I know what to do to fix it, I just don't know how... I mean I know I have to use it as a function, not a while wait.. And I know that theres a way to do the function whenever a value changes... I just don't know how! Help?

RoundStart = game.Workspace.RoundStart
Room1 = game.ServerStorage.Room1
Room2 = game.ServerStorage.Room2
Room3 = game.ServerStorage.Room3
Room4 = game.ServerStorage.Room4
Room5 = game.ServerStorage.Room5

while wait() do
	wait(1)
	if RoundStart.Value == true then
		RandomRoom = (math.random(5))
		if RandomRoom == 1 then
			Map1 = Room1:Clone()
			Map1.Parent = game.Workspace
		end
		if RandomRoom == 2 then
			Map1 = Room2:Clone()
			Map1.Parent = game.Workspace
		end
		if RandomRoom == 3 then
			Map1 = Room3:Clone()
			Map1.Parent = game.Workspace
		end
		if RandomRoom == 4 then
			Map1 = Room4:Clone()
			Map1.Parent = game.Workspace
		end
		if RandomRoom == 5 then
			Map1 = Room5:Clone()
			Map1.Parent = game.Workspace
		end
	end
	
	
end

3 comments

0

Um what? If you only want it to happen once then don't put it in a loop. Or if you want put it in a function so you can call it multiple times.

0

Yeah, I know the point is, I want to put it in a function, I just don't know what... Event?.. To use to use that function when the bool value "RoundStart" Changes..

0

I think you SHOULD make all of your Map1 =... be a local Map1

1 answer

✓ Accepted answer

The reason it isn't picking random number is because you didn't give it a range, which it requires. So do:

RandomRoom = math.random(1,5) or math.random(5)

also I noticed your script says:

while wait() do
	wait(1)

which isn't necessary, you can just do

while wait() do
--or
while wait(1) do
--the difference is "1" is every second, and () is much faster

NOW TO ANSWER THE MAIN PART OF YOUR QUESTION To connect when a value changes use

game.Workspace.RoundStart.Changed:connect(function()
	--do whatever
end)

3 comments

0

EDITING RIGHT NOW

2

You're incorrect in regards to his attempt of picking a random number - math.random(5) would work exactly the same if he did math.random(1,5) - If the random number he wanted wasn't between 1 and 5, but rather 5 and 10, he would need to dictate "a range".

0

Yeah, the random thing DID work for me my way too...

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