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

Change BrickColor when touched glitching HELP? [Solved]

I'm trying to make a part change color when you're on it and change back when you get off the part.

This is the problem I'm having. It's flashing when I step on and off really fast.

My current code is:

local pad = script.Parent
local off = BrickColor.new('Medium stone grey')
local on = BrickColor.new('Light blue')

local onPad = false

pad.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if not onPad then
			pad.BrickColor = on
			onPad = true
			wait(1)
		end
	end
end)

pad.TouchEnded:Connect(function(hit)
	if onPad then
		pad.BrickColor = off
		onPad = false
		wait(1)
	end
end)

1 comment

0

Roblox's touch and touch ended is really crappy in terms of functionality. I'll write an answer on an alternative method in a second

1 answer

As I said, roblox's touched and touchended events are really bad at accurate detection.

Another way you could do this is with Raycasting, a way to (most of the time) accurately detect hits.

You could cast a ray upwards constantly to see if anything is on top of it.

local ray = Ray.new(script.Parent.Position, Vector3.new(0,50,0)) --Cast a ray facing directly up

while wait() do
	local hit, position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
	
	if hit then
		print("Hit")
		if hit:WaitForChild("Humanoid", 0.1) or hit.Parent:WaitForChild("Humanoid", 0.1) then
			print("Humanoid")
			
			script.Parent.BrickColor = BrickColor.new("Really red")
		end
	else
		script.Parent.BrickColor = BrickColor.new("White")
	end
end

Let me know if this worked.

1 comment

0

I fixed it already by adding a debounce, but I tried this and it works. Thanks!

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