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

The part.TouchEnded function doesnt work properly, any solutions?

Here's my script. I tried using if Part:GetTouchingParts() ~= nil but that didnt work to well as it wouldnt fire if there were no parts touching it to fire the event.

script.Parent.MeshPart1.Touched:Connect(function(Part)
	script.Parent.MeshPart1.BrickColor = BrickColor.new("Really red")
	script.Parent.MeshPart2.BrickColor = BrickColor.new("Really red")
	script.Parent.MeshPart3.BrickColor = BrickColor.new("Really red")
end)

script.Parent.MeshPart1.TouchEnded:Connect(function(Part)
	script.Parent.MeshPart1.BrickColor = BrickColor.new("Deep blue")
	script.Parent.MeshPart2.BrickColor = BrickColor.new("Deep blue")
	script.Parent.MeshPart3.BrickColor = BrickColor.new("Deep blue")
end)

I couldn't really find any non laggy uncomplicated ways that dont involve things such as while true loops etc.

3 comments

0

note that gettouchingparts only returns baseparts that are literally intersecting the part's boundaries. that is, only a part inside another part will be included in the table. simply touching the surface does not count

0

diy

1 answer

✓ Accepted answer

TouchEnded is only really useful for when a player walks through a large invisible part. I think if you made an invisible part the player had to walk through, and it was fairly thick, then you could set it up so while the player is inside that area touching it, the brick would stay that color, and when they stop touching or walk out of the area, the TouchEnded event would trigger. I personally hate using TouchEnded and it needs to be fixed in my opinion. Anyways, your script will work if you basically just make a thick hitbox the player walks through.

Here's an example of what I mean, just do yours with an invisible hitbox and instead of printing text, have it change the color, and your script is currently setup to pretty much do that. https://imgur.com/a/XQKfO

Try this with that method:

local touching = false

script.Parent.MeshPart1.Touched:Connect(function(Part)
	if touching == false then
		script.Parent.MeshPart1.BrickColor = BrickColor.new("Really red")
		script.Parent.MeshPart2.BrickColor = BrickColor.new("Really red")
		script.Parent.MeshPart3.BrickColor = BrickColor.new("Really red")
		touching = true
	end
end)

script.Parent.MeshPart1.TouchEnded:Connect(function(Part)
	if touching == true then
		script.Parent.MeshPart1.BrickColor = BrickColor.new("Deep blue")
		script.Parent.MeshPart2.BrickColor = BrickColor.new("Deep blue")
		script.Parent.MeshPart3.BrickColor = BrickColor.new("Deep blue")
		touching = false
	end
end)

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