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

How to make a Touched function 'repeatable?'

I've written the following code in an attempt to change the fire's color every time a player touches it:

local fire = script.Parent.Fire

local rgb = math.random(0, 255)

script.Parent.Touched:Connect(function(connect)
	if connect.Parent:FindFirstChild("Humanoid") then
		fire.Color = Color3.new(rgb/255,rgb/255,rgb/255)
	end
end)

However, the function only changes the color once per play test. I'd like it to be able to change colors an unlimited amount of times, for every time a player touches the part.

1 answer

✓ Accepted answer

You're practically there! The reason you may think the function only changes the color once per play test, is because you set the random value in the beginning of your code, which is outside of the event being triggered each time. Simple fix is to move it down!

I've taken the liberty of making your color even more random by randomizing each R, G, and B value. Also a quick tip, instead of dividing by 255, you can use Color3.fromRGB()

local fire = script.Parent.Fire

script.Parent.Touched:Connect(function(connect)
    if connect.Parent:FindFirstChild("Humanoid") then
        fire.Color = Color3.fromRGB(math.random(0, 255),math.random(0, 255),math.random(0, 255))
    end
end)

3 comments

0

God dammit! I was going to post that! Fine, you take the throne.

0

Nice try! Also, (Since I can see your deleted post since I'm an SH Admin), you still technically didn't solve his problem! You'd have to move line 2 to after the Touched event

0

Perfect. Thank you!

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