Touch event firing many times(SOLVED)

I want my touch event to fire once but its firing a lot of times even though I stop it
Server:

local intermissiontime = 5
local ball = game.Workspace.Ball
local basespeed = 20
local playerleft
local serverevent
local playercount = 0

game.Players.PlayerAdded:Connect(function()
	local count = 0
	for i,v in pairs(game.Players:GetChildren())  do
		count+= 1
	end
	if count == 2  then
		playercount = 0	
		start(playercount)
		print("START")
	else
		Text("WAITING FOR PLAYERS")
end
end)
function start()
	playerleft = game.Players.PlayerRemoving:Connect(function()
		playercount-=1
	end)
	local timer = intermissiontime
	repeat
		Text(timer)
		task.wait(1)
		timer-=1
	until timer == 0
	for i,v in pairs(game.Players:GetChildren())  do
		playercount+= 1
		local value = Instance.new("BoolValue")
		value.Parent = game.Workspace:FindFirstChild(v.Name)
	end
	local speed = basespeed
	Text("Game Started")
	local currentplayer = PickRandom(speed)
	serverevent = game.ReplicatedStorage.Ball.OnServerEvent:Connect(function(plr,died)
		print(died)
		if died == true and playercount > 1 then
			currentplayer = PickRandom(speed,currentplayer)
			playercount -= 1
			game.Workspace.kill:Play()
			game.Workspace:FindFirstChild(currentplayer.Name):FindFirstChild("Humanoid").Health-= 100
		elseif playercount > 1 then
			speed*=1.1
			currentplayer = PickRandom(speed,currentplayer)
		end
		if playercount <= 1 then
			endgame()
		end
	end)
end
function endgame()
	playerleft:Disconnect()
	serverevent:Disconnect()
	ball:SetNetworkOwner(nil)
	ball.Position = game.Workspace.BallSpawn.Position
	ball.BodyVelocity.Velocity = Vector3.new(0,0,0)
	print(ball.BodyVelocity.Velocity)
	playercount = 0
	for i,v in pairs(game.Players:GetChildren()) do
		if v:FindFirstChild("Value") then
			v:FindFirstChild("Value"):Destroy()
		end
	end
	start()
end
function Text(text)
	for i,v in pairs(game.Players:GetChildren())  do
		v:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("TextLabel").Text = text
	end
end
function PickRandom(speed,prevplr)
	print("PICK RANDOM FUNCTION RAN")
	local pickedPlayer
	repeat
	local players = game:GetService("Players"):GetPlayers()
	pickedPlayer = players[math.random(1, #players)]
	until pickedPlayer.Name ~= prevplr and game.Workspace:FindFirstChild(pickedPlayer.Name):FindFirstChild("Value")
	
	ball:SetNetworkOwner(pickedPlayer)
	game.ReplicatedStorage.Ball:FireClient(pickedPlayer,speed)
	return pickedPlayer
end
game.ReplicatedStorage.Effects.OnServerEvent:Connect(function(plr)
	local handle = game.Workspace:FindFirstChild(plr.Name):FindFirstChild("Tool").Handle
	handle.hit:Play()
	for i,v in pairs(handle.HitEffect:GetChildren()) do
		v:Emit(1)
	end
end)

Client:

local connection
local db = false
game.ReplicatedStorage.Ball.OnClientEvent:Connect(function(speed)
	local char = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
	local ball = game.Workspace.Ball
	ball.Color = Color3.new(1, 0, 0)
	connection = ball.Touched:Connect(function(hit)
		if hit.Parent.Name == char.Name then
			if db == false then
				db = true
				if char:FindFirstChild("Tool") and char:FindFirstChild("Tool").DB.Value == true then
					char.Humanoid:LoadAnimation(script.Slash):Play()
					game.ReplicatedStorage.Effects:FireServer()
					game.ReplicatedStorage.Ball:FireServer(false)
					print(db)
				else
					game.ReplicatedStorage.Ball:FireServer(true)
				end
			end
		end
	end)
	repeat 
		task.wait()
		game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position-game.Workspace.Ball.Position)).Unit*speed
	until db == true
	connection:Disconnect()
	print("RAN")
	db = false
	ball.Color = Color3.new(99, 95, 98)
end)
2 Likes

Its because the character has multiple parts in it, so its firing for each part that’s in the character.

Change this:
if hit.Parent.Name == char.Name then

To this: 
if hit.Parent:FindFirstChild("HumanoidRootPart") then
3 Likes

Should be this, right?
if hit.Name == "HumanoidRootPart" then

2 Likes

DIDNT WORK

local connection
local db = false
game.ReplicatedStorage.Ball.OnClientEvent:Connect(function(speed)
	local char = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
	local ball = game.Workspace.Ball
	ball.Color = Color3.new(1, 0, 0)
	connection = ball.Touched:Connect(function(hit)
		if hit.Parent.Name == char.Name and hit.Name == "HumanoidRootPart" then
			if db == false then
				db = true
				if char:FindFirstChild("Tool") and char:FindFirstChild("Tool").DB.Value == true then
					char.Humanoid:LoadAnimation(script.Slash):Play()
					game.ReplicatedStorage.Effects:FireServer()
					game.ReplicatedStorage.Ball:FireServer(false)
					print(db)
				else
					game.ReplicatedStorage.Ball:FireServer(true)
				end
			end
		end
	end)
	repeat 
		task.wait()
		game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position-game.Workspace.Ball.Position)).Unit*speed
	until db == true
	connection:Disconnect()
	print("RAN")
	db = false
	ball.Color = Color3.new(99, 95, 98)
end)
2 Likes

DID NOT WORK :frowning:
30 gijkagrqgwqg

2 Likes

or maybe that doesn’t really matter does it? Both do the same thing?

2 Likes

Yes, but yours would still fire multiple times right?

2 Likes

Can you print what parts are being touched to see if we can find what isn’t working?

2 Likes
Try change this:
local char = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)

To this:
game.Players.LocalPlayer.Character
2 Likes

Oh yes probably, yours is probably the better option then.

I’ve got this code for something else I’m working on and it works fine to get the player

self.Detector.Touched:Connect(function(OtherPart)
		if OtherPart.Name == "Head" and OtherPart.Parent:FindFirstChild("Humanoid") then
			local Player = Players:GetPlayerFromCharacter(OtherPart.Parent)
			RemoteEvent:FireClient(Player, "Set Current Door", Door)
		end
	end)

	self.Detector.TouchEnded:Connect(function(OtherPart)
		if OtherPart.Name == "Head" and OtherPart.Parent:FindFirstChild("Humanoid") then
			local Player = Players:GetPlayerFromCharacter(OtherPart.Parent)
			RemoteEvent:FireClient(Player, "Set Current Door", nil)
		end
	end)
2 Likes

didnt work
hstahsahgwrrwbsgbsgr

2 Likes

After you set debounce to true print something random to see if it prints.

2 Likes

I alerady have a db :man_facepalming:
aaaaaaaaaaaaaa

2 Likes

I said after you set it to true. THEN print something random to see if it gets pass that line.

2 Likes

Debounces have to have a longer task.wait in them, otherwise they just fire straight away.
The timer is what makes it a debounce.

2 Likes

They don’t even have a timer for the debounce do they?

3 Likes

yeah i the touch event to fire once

2 Likes

image

1 Like

Means its something else in your script

1 Like

i sent the entire thing
aaaaaaaaaa

2 Likes