Hello! I’m making a game, but I need to do some debugging. This remote function will fire, but the debounce is not working. It fires more than once, making it feel uncomfortable. Any ideas on how to fix this?
local player
local debounce = true
script.Parent.Touched:Connect(function(plr)
local playerService = game:GetService("Players")
if debounce then
local modelPlayer = plr:FindFirstAncestorWhichIsA("Model")
if modelPlayer then
debounce = not debounce
plr.Parent.PrimaryPart.Position = workspace.Telepart.Position
player = playerService:GetPlayerFromCharacter(plr.Parent)
game.ReplicatedStorage["Start Game"]:FireClient(player)
task.wait(1)
debounce = true
end
else
warn("TouchPart is accessory")
wait(0.5)
end
end)
Here’s the code. Thanks.
Edit: Edited code
Tell me if this works:
local player
local debounce = false
script.Parent.Touched:Connect(function(plr)
if debounce == true then return end
debounce = true
local playerService = game:GetService("Players")
local modelPlayer = plr:FindFirstAncestorWhichIsA("Model")
if modelPlayer then
debounce = not debounce
plr.Parent.PrimaryPart.Position = workspace.Telepart.Position
player = playerService:GetPlayerFromCharacter(plr.Parent)
game.ReplicatedStorage["Start Game"]:FireClient(player)
task.wait(1)
debounce = false
end
end)
Nevermind, all I did to fix it was move bits of the code in the if statement. The warning is one thing preventing errors for me. Thanks for helping though.
Edit: Nah it broke after a minute I’ll try yours
The problem with this chunk here is that the touched event can fire even if a non-player touches the part. It’s important to only set the debounce to true once you’ve verified that the part that touches is a player.
local player
local debounce = true
local playerService = game:GetService("Players")
script.Parent.Touched:Connect(function(plr)
-- Check if part that touched is a player
local player = playerService:GetPlayerFromCharacter(plr.Parent)
if player then
-- Check if debounce is true before setting it to false
if debounce then
debounce = false
plr.Parent.PrimaryPart.Position = workspace.Telepart.Position
player = playerService:GetPlayerFromCharacter(plr.Parent)
-- Fire client and wait 1 before setting debounce back to true
game.ReplicatedStorage["Start Game"]:FireClient(player)
task.wait(1)
debounce = true
end
end
end)
1 Like
Yes but it has to be a npc. That would be a little too much btw. Here is a better compacted version:
local player
local debounce = false
script.Parent.Touched:Connect(function(plr)
if plr == nil then return end
if debounce == true then return end
debounce = true
local playerService = game:GetService("Players")
local modelPlayer = plr:FindFirstAncestorWhichIsA("Model")
if modelPlayer then
debounce = not debounce
plr.Parent.PrimaryPart.Position = workspace.Telepart.Position
player = playerService:GetPlayerFromCharacter(plr.Parent)
game.ReplicatedStorage["Start Game"]:FireClient(player)
task.wait(1)
debounce = false
end
end)
1 Like
It still fires a buncha times, I keep teleporting back to a part (teleporting the player is part of the event’s role) I’m getting a mega stack from both of your code. Debounce sure is broken for me.
That is odd… I simulated this code in my own test place and the player is only teleported once, as the the debounce isn’t reset until 1 second after I’ve set it to false.
Test.rbxl (29.3 KB)
1 Like
I’ll let you use the file for the game:
Speedrunners (ALPHA).rbxl (36.5 KB)
Worked on it for 2 days now and there’s still a lot of broken stuff to fix.
1 Like
Wow this is extremely odd. I set up print statements, and even upon touching it once, the touched event is still being fired
The debounce has been broken since I put it. Have I been doing it wrong? Unfortunately I’ll have to reach you later, since I’m busy at the moment.
1 Like
I’ll look into it. From what I see right now, the client and server see two different things.
The client views their character at the start, meanwhile the server sees this:
1 Like
I’ve identified the error. Rather than setting the position of the character’s primary part to the telepart’s position, set the character’s humanoidRootPart CFrame instead.
local player
local debounce = true
local playerService = game:GetService("Players")
script.Parent.Touched:Connect(function(part)
local character = part.Parent
local player = playerService:GetPlayerFromCharacter(character)
if player then
if debounce then
debounce = false
character.HumanoidRootPart.CFrame = workspace.Telepart.CFrame
game.ReplicatedStorage["Start Game"]:FireClient(player)
task.wait(1)
debounce = true
end
end
end)
Place:
Test.rbxl (29.3 KB)
2 Likes