I have tried many things, yet this debounce still isn't working out for me. The script keeps on printing multiple of "rilgundam has the Red Flag!" Please help me if you can.
local hasFlag = false local Thing = true local DeBounce = false function accessBackpack(hit) if hit and hit.Parent then local chara = hit.Parent local player = game.Players:GetPlayerFromCharacter(chara) local backpack = player:FindFirstChild("Backpack") local flag = backpack:FindFirstChild("RedFlag") if flag and flag.Parent == backpack and hasFlag == true then return "RedFlag" else return print("No flag") end end end script.Parent.RedFlag.Handle.Touched:Connect(function(char) if DeBounce == false then DeBounce = true hasFlag = true if char and char.Parent then local player = game.Players:GetPlayerFromCharacter(char.Parent) if player then--check if they are a player if char.Parent:FindFirstChild("Humanoid") then if char.Parent.Humanoid.Health > 0 then--if they are still alive print(player.Name .. " has the Red Flag!") char.Parent:FindFirstChild("Humanoid").Died:Connect(function() print("Person who has flag died!") if char.Parent:FindFirstChild("RedFlag") or accessBackpack(char) and hasFlag == true then --if they die and they have the flag in their hand or backpack hasFlag = false local FlagClone = game:GetService("ServerStorage"):WaitForChild("RedFlag"):Clone()--the flag gets cloned and put into workspace FlagClone.Parent = game.Workspace FlagClone.Handle.CFrame = CFrame.new(char.Parent.Head.Position) FlagClone:WaitForChild("Handle").Anchored = true Thing = false wait(3) Thing = true FlagClone.Handle.Touched:Connect(function() wait(Thing) if FlagClone.Parent.Parent == game.Workspace then FlagClone.Handle.Anchored = false end end) end end) end end end end DeBounce = false end end)
The reason is because you are making the value of debounce false outside of the if statement. If that is confusing, basically
--Good local deb = false script.Parent.Touched:Connect(function() if not deb then deb = true print('Ouch!') wait(2) print('Can you get off of me?!') deb = false end end) --Bad local deb = false script.Parent.Touched:Connect(function() if not deb then deb = true print('Ouch!') wait(2) print('Can you get off of me?!') end deb = false end --More bad stuff local deb = false script.Parent.Touched:Connect(function() if not deb then deb = true deb = false print('Ouch!') wait(2) print('Can you get off of me?!') end end
So, imagine this. The bad code getting hit once is fine. The second time u hit it, it is fine. The third time, however, the second time u hit it, deb became false. So, now you could run the code again. Hope this helps!