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

BrickColor changes but then immediately reverts back?

The entire script shown below works fine except the last part. It is supposed to return the parts color back to what is was before being changed to black. The confusing thing is that it works sometimes but other times it will immediately turn back to black. No error messages are shown in the output. Any insight would be appreciated.

-- whoever touches the part gets put on fire, a black color applied to them, then has damage dealt to them. after 5 seconds the fire is removed and after 1 second the skin tone is returned back to normal

local trap = script.Parent
local dmg = 5
local function onTouch(objectTouched)
print("Something touched the trap")
local color = objectTouched.BrickColor
local fire = Instance.new("Fire")
local person = objectTouched.Parent
local human = person:FindFirstChildWhichIsA("Humanoid")
if ( human ) then
	fire.Parent = objectTouched
	objectTouched.BrickColor = BrickColor.new(0, 0, 0)
	human.Health = human.Health - dmg
	wait(5)
	objectTouched.Fire:Destroy()
	wait(1)
	objectTouched.BrickColor = color
end
end
trap.Touched:Connect(onTouch)

1 comment

0

debounces should fix the issue

1 answer

✓ Accepted answer

OnTouched scripts will run multiple times without a debounce (since dozens of touches can be processed before the scripts runs to completion). Try using the following setup for your onTouched script.

function onTouched(hit)
	print("Touched")
	if debounce then
		debounce = false
		local human = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid") 
		if human ~= nil then
			debounce = false
			-- insert stuff you want it to do when touched.
			debounce = true
		end
	end
end

-- Making sure the brick is still in existence before processing the touch.
if script.Parent ~= nil then
	debounce = true
	local connection = script.Parent.Touched:connect(onTouched)
end

1 comment

0

Thanks for the detailed reply , I can't get it working right now but I will try again once I'm less sleepy.

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