Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago
Edited 6 years ago

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

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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!

0
I did that and it still doesn't work rilgundam 65 — 6y
0
on line 51, the end of the first end statement rilgundam 65 — 6y
0
Please help me rilgundam 65 — 6y
0
I'd like you to edit your question's code, so I can see your edits. hiimgoodpack 2009 — 6y
View all comments (18 more)
0
Ok rilgundam 65 — 6y
0
Read my edit. hiimgoodpack 2009 — 6y
0
Lol ok rilgundam 65 — 6y
0
K I edited it. Is it ok now? rilgundam 65 — 6y
0
Much better. Just DAMN INDENT PROPERLY OK?????????????????? hiimgoodpack 2009 — 6y
0
Ok rilgundam 65 — 6y
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 rilgundam 65 — 6y
0
Thats strange. Did you stand on it for 6 seconds? hiimgoodpack 2009 — 6y
0
No rilgundam 65 — 6y
0
I walked across it and it said what I told u rilgundam 65 — 6y
0
Strange. Try printing the value of debounce in the touched event before the if statement. hiimgoodpack 2009 — 6y
0
Ok rilgundam 65 — 6y
0
But shouldn't it be false because that's what the variable is rilgundam 65 — 6y
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. hiimgoodpack 2009 — 6y
0
So what should I do? rilgundam 65 — 6y
0
You can reread my comments, you know... hiimgoodpack 2009 — 6y
0
It said that debounce is false rilgundam 65 — 6y
0
Only once or multiple times??? hiimgoodpack 2009 — 6y
Ad

Answer this question