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

Why is Randomize button not working correctly?

I have a button that when I click it, it randomly fires a Remote Event in order to give me a random shirt color. It works the first time I click it, but when I try and click it again, nothing happens. Either its not firing again, or it just randomly selects one event and keeps selecting that event, instead of selecting another event every time I click it. Here's my code:

local ShirtColors = game.ReplicatedStorage.Customization.ShirtColor:GetChildren()
local RandomShirtColor = ShirtColors[math.random(1,#ShirtColors)]

script.Parent.MouseButton1Click:Connect(function()
		RandomShirtColor:FireServer()
	
end)

Thanks in advance for anyone who helps.

1 answer

✓ Accepted answer

You need to re-calculate the randomness everytime you run the funcion, so the variable that contais math.random needs to be inside the click event

local ShirtColors = game.ReplicatedStorage.Customization.ShirtColor:GetChildren()


script.Parent.MouseButton1Click:Connect(function()
        local RandomShirtColor = ShirtColors[math.random(1,#ShirtColors)] --Set the variable here
        RandomShirtColor:FireServer()

end)

1 comment

0

Thanks it worked

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