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

Teleporter debounce is skipping my If statement, why?

Asked by 2 years ago

Okay so Basically I have made a teleporter that returns the player to the lobby and gives them cash, however, for some reason the debounce gets activated early and doesn't do the if loop, to combat this I added a wait before the debounce which works, however it will give the player a ton of cash, usually about 70 dollars, I know this is because parts of the body are activating the if statement before the debounce turns on I just don't know what to do.

Original code that skips the if statement and prints error:

local p = script.Parent
debounce = false

p.Touched:Connect(function(plr)
    if plr.Parent.Name ~= "Stalker" then -- Checks to see if they are the killer in the game
        if debounce == false then 
            debounce = true
            plr.Parent.HumanoidRootPart.CFrame = game.Workspace.Lobby.SpawnLocation.CFrame + Vector3.new(0, 3, 0)
            wait()
            local pp = plr.Parent.Name
            local e = game.Players:FindFirstChild(pp)
            print(pp, " found")
            local ppp = e:FindFirstChild("leaderstats")
            local cash = ppp:FindFirstChild("Cash")
            cash.Value = cash.Value+10
            print("Gave Cash to ", pp)
            wait(2)
            debounce = false
            elseif debounce == true then print("Error")
            end
    end
end)

Altered code that gives random amounts of money:

local p = script.Parent
debounce = false

p.Touched:Connect(function(plr)
    if plr.Parent.Name ~= "Stalker" then
        if debounce == false then 
            wait()
            debounce = true
            plr.Parent.HumanoidRootPart.CFrame = game.Workspace.Lobby.SpawnLocation.CFrame + Vector3.new(0, 3, 0)
            wait()
            local pp = plr.Parent.Name
            local e = game.Players:FindFirstChild(pp)
            print(pp, " found")
            local ppp = e:FindFirstChild("leaderstats")
            local cash = ppp:FindFirstChild("Cash")
            cash.Value = cash.Value+10
            print("Gave Cash to ", pp)
            wait(2)
            debounce = false
            elseif debounce == true then print("Error")
            end
    end
end)

1 answer

Log in to vote
0
Answered by 2 years ago

I looked at your code a bit, I think I've found the problem.

First off, when you start a Debounce, try using "if not" statements instead of "if debounce =="

for example, when you write:

local p = script.Parent
debounce = false

p.Touched:Connect(function(plr)
    if plr.Parent.Name ~= "Stalker" then -- Checks to see if they are the killer in the game
        if debounce == false then 
            debounce = true

you should preferably instead write,

local p = script.Parent
debounce = false

p.Touched:Connect(function(plr)
    if plr.Parent.Name ~= "Stalker" then -- Checks to see if they are the killer in the game
        if not debounce then
--now continue with your script

However, there is an error in this code, you should not set debounce to true so soon in the first code. Debounce should be set to true after all your code has run, for example:

local p = script.Parent
debounce = false

p.Touched:Connect(function(plr)
    if plr.Parent.Name ~= "Stalker" then -- Checks to see if they are the killer in the game
        if debounce == false then 
             wait()
           local pp = plr.Parent.Name
            local e = game.Players:FindFirstChild(pp)
            print(pp, " found")
            local ppp = e:FindFirstChild("leaderstats")
            local cash = ppp:FindFirstChild("Cash")
            cash.Value = cash.Value+10
            print("Gave Cash to ", pp)
        debounce = true -- here
            wait(2)
            debounce = false
            elseif debounce == true then print("Error")
            end
    end
end)

Lmk if this helped! Hopefully it did, good luck on your game, I would love to see it finished.

0
Hey thanks for the help, I appreciate it, the change sort of fixed the issue but it turned out there was problems on my part with putting the teleporter vertically so it couldn't find the humanoid. Thanks for all the kind words! Canyon620_YT 5 — 2y
Ad

Answer this question