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

TouchEnded Working Fine In Studio But Is Constantly Fired In A Real Game?

Through a local script, I made Touch and TouchEnded functions set up with a debounce. It's linked to a server script through a remote event if you're wondering why I'm using a local script. For some reason, when the character is touching the part, the functions get spammed.

Local Script:

local stuff = script.Parent
local bar = stuff:WaitForChild("Bar")
local point = workspace:WaitForChild("CapturePointC")
local model = point:WaitForChild("CircleThings")
local cappoint = model:WaitForChild("CapturePoint")
local mainscript = cappoint:WaitForChild("Capture")
local rs = game:GetService("ReplicatedStorage")
local events = rs:WaitForChild("CapturePoints")
local point1 = events:WaitForChild("PointC")
local point1stop = events:WaitForChild("PointCStop")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local torso = char:WaitForChild("Hitbox")
local ifcap = char:WaitForChild("IfCapturing")
local touching = false

torso.Touched:Connect(function(hit)
	if touching == false then
		touching = true
		if ifcap.Value == false then
			if hit == cappoint then
				print('ok')
				stuff.Visible = true
				point1:FireServer()
			end
		end
	end
end)

torso.TouchEnded:Connect(function(hit)
	if hit == cappoint then
		touching = false
		point1stop:FireServer()
		print('ok2')
		stuff.Visible = false
	end
end)

2 comments

0

You shouldn’t handle touched event locally.

1 answer

Basically what TheeDeathCaster said.

local isTouching = false

torso.TouchEnded:Connect(function(hit)
    if hit == cappoint then
	if isTouching == false then
	isTouching = true
        touching = false
        point1stop:FireServer()
        print('ok2')
        stuff.Visible = false
	wait(1.5) -- Just change this to how much time you think the touch end will be over in.
	isTouching = false
	end
    end
end)

This is basically Debounce. You can also find it here as a better explanation. http://robloxdev.com/articles/Debounce

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