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

Why isn't the debounce I put in this script working?

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)

1 answer

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!

22 comments

0

I did that and it still doesn't work

0

on line 51, the end of the first end statement

0

Please help me

0

I'd like you to edit your question's code, so I can see your edits.

0

Ok

0

Read my edit.

0

Lol ok

0

K I edited it. Is it ok now?

0

Much better. Just DAMN INDENT PROPERLY OK??????????????????

0

Ok

0

I used the indent script and this is what it came out with. Btw, the debounce still isn't working it still says: "rilgundam has the Red Flag!"x3

0

Thats strange. Did you stand on it for 6 seconds?

0

No

0

I walked across it and it said what I told u

0

Strange. Try printing the value of debounce in the touched event before the if statement.

0

Ok

0

But shouldn't it be false because that's what the variable is

0

I want to see the value when the player touches it to see what problem it is, if it is the check, the debounce, etc.

0

So what should I do?

0

You can reread my comments, you know...

0

It said that debounce is false

0

Only once or multiple times???

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