part.Touched:Connect(function(otherPart) issue

I have an issue, with a part and Touched event. The script prints, that a part (“DriveSeat”) is touching it and untouching it really fast, even tho the part just sits in it.

local entered = 0
local preventered = 0
script.Parent.Touched:Connect(function(otherPart)
	if otherPart.Name == "DriveSeat" then
		print(otherPart)
		if entered == 0 then
			script.Parent.Canister.Enabled = false
			game.Players.LocalPlayer.PlayerGui.ActionTextFueling.Enabled = true
			print(entered)
			game.Players.LocalPlayer.PlayerGui.UI.Notification:Play()
			entered = 1
		end
	end
end)

script.Parent.TouchEnded:Connect(function(otherPart)
	if otherPart.Name == "DriveSeat" then
		if entered == 1 then
			game.Players.LocalPlayer.PlayerGui.ActionTextFueling.Enabled = false
			script.Parent.Canister.Enabled = true
			game.Players.LocalPlayer.PlayerGui.Infoteinmet.Infoteinemt.Money.Value -= math.round(game.Players.LocalPlayer.PlayerGui.ActionTextFueling.Fueling["Your STA"].Litre.Value*game.Players.LocalPlayer.PlayerGui.ActionTextFueling.Fueling.Price.Value.Value)
			if game.Players.LocalPlayer.PlayerGui.ActionTextFueling.Fueling["Your STA"].Litre.Value >= 4 then
				game.Players.LocalPlayer.PlayerGui.ActionTextFueling.Fueling["Your STA"].Litre.Value = 0
				game.Players.LocalPlayer.PlayerGui.Infoteinmet.money:Play()
				game.Players.LocalPlayer.PlayerGui.ActionTextFueling.Fueling["Your STA"].Litre.Value = 0
				local badge = 4349101436674228
				game.Workspace.Badge:FireServer(badge)
			end
			entered = 0
		end
	end
end)

1 Like

Touchended isn’t very reliable, you should probably use something else, someone smarter can tell you what that “something else” is

2 Likes

I was playing with the script and found this:

script.Parent:GetTouchingParts()
1 Like

that could work ig, but it only detects parts with collision on

2 Likes

Have you considered using GetPartBoundsInBox?

1 Like

please look into this it works really well and there are a tons of tutorials on it.

Touched will fire when they touch it. However, if they stop still on the touched part, it will fire a TouchEnded. As soon as they move again, it will fire a new Touched and so on.
In this case, the truck is vibrating a bit, so while on the touched part, they are constantly re-firing Touched/TouchEnded as I mentioned.

Considering this, it may be better to approach this differently.
Here is one possible way. (not tested)

local seat = workspace:WaitForChild("DriveSeat")
local parent = script.Parent
local entered = false

task.spawn(function()
	while true do
		local distance = (parent.Position - seat.Position).Magnitude
		if distance < 5 and not entered then entered = true
			-- Action for entering
			
		elseif distance >= 5 and entered then entered = false
			-- Action for exiting
			
		end task.wait(0.33)
	end
end)

You could also use just Touched with two transparent parts - one at the entrance and one at the exit, set up so they can’t touch both at the same time. A gap big enough to not be touching either one. (this is what I do in most cases)

1 Like

I like your idea. I have modified the script and it should work now.

1 Like

Ya, touched works fine as long as you understand just how it works. It truly seems broken if you don’t and try to use TouchEnded. The part would have to be set up in a way they can’t just stop while still on the part.

1 Like