Are there any alternatives to touched events?

In my script I have a while loop, saying while a part is touched, turn blue. But it’s really buggy and keeps fluctuating back to its original colour. Is there another thing I can use instead of a touched event?

1 Like

You can usually try Region3 to create a Custom Touched Event

2 Likes

if you are using touched events then while loops are unnecessary, furthermore I believe that touched events are fine for this and that you are just not using it correctly. lastly, the alternatives probably require more resources or processing than touched events.

Sorry I’m not using a while loop. Here is my script.

part1.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 == nil then -- Check if not standing and if not occupied

			part1.BrickColor = BrickColor.new("Teal") -- Set color
			standingOnArena1Player1 = Player
		end	
	end
end)

part1.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 ~= nil then -- Check if standing and is occupied
			if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
			end
			part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
			standingOnArena1Player1 = nil

			if standingOnArena1Player2 ~= nil then
				if standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

It’s super buggy, if the player moves at all while on the part, it will flick back and fore between the colours.

perhaps you could create a hitbox with an extended height and cancollide off to mitigate the “flicking”.

1 Like

How do I create a hitbox?

bfhjdsufbsd

You could try using magnitude to calculate how close someone is to the part and then trigger the colour change when they are very close and not close

You can use raycasthitbox it uses raycast and attachments.

All of these are either overcomplicated or outdated ways of doing what GetPartsInPart, GetPartBoundsInBox and GetPartBoundsInRadius do. Use those for the simplest and most performant way of doing this.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.