Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make a Touched function 'repeatable?'

Asked by
Hypgnosis 186
6 years ago
Edited 6 years ago

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

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
6 years ago

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)

0
God dammit! I was going to post that! Fine, you take the throne. wentman 19 — 6y
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 Shawnyg 4330 — 6y
0
Perfect. Thank you! Hypgnosis 186 — 6y
Ad

Answer this question