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

Teleporter debounce problem?

Asked by 9 years ago

TELEPORTER 2 SCRIPT:

db = false

script.Parent.Touched:connect(function(tch)
    if db == true then return end
    db = true
    local trs = tch.Parent:FindFirstChild("Torso")
    local lrm =  tch.Parent:FindFirstChild("Left Arm")
    local rrm =  tch.Parent:FindFirstChild("Right Arm")
    local lg =  tch.Parent:FindFirstChild("Left Leg")
    local rg =  tch.Parent:FindFirstChild("Right Leg")
    local hd =  tch.Parent:FindFirstChild("Head")
    if trs ~= nil and lrm ~= nil and rrm ~= nil and lg ~= nil and rg ~= nil and hd ~= nil then

        tch.Parent:MoveTo(script.Parent.Parent.TP1.Position)
        wait(2)
        db = false
    end
end)

TELEPORTER 1 SCRIPT:

db = false

script.Parent.Touched:connect(function(tch)
    if db == true then return end
    db = true
    local trs = tch.Parent:FindFirstChild("Torso")
    local lrm =  tch.Parent:FindFirstChild("Left Arm")
    local rrm =  tch.Parent:FindFirstChild("Right Arm")
    local lg =  tch.Parent:FindFirstChild("Left Leg")
    local rg =  tch.Parent:FindFirstChild("Right Leg")
    local hd =  tch.Parent:FindFirstChild("Head")
    if trs ~= nil and lrm ~= nil and rrm ~= nil and lg ~= nil and rg ~= nil and hd ~= nil then

        tch.Parent:MoveTo(script.Parent.Parent.TP1.Position)
        wait(2)
        db = false
    end
end)

This is related to my last post, about when players die when they teleport. Now I have another problem, when I tried to give it a debounce. The problem is the player teleports from one teleporter to the other, then back to the original one. After that is when the debounce works. I want the debounce to work after you teleport once, from the touched teleporter to the other one.

1 answer

Log in to vote
0
Answered by 9 years ago

See how the scripts are practically the same? (Actually, they're identical, but I assume you meant to have teleporter1 teleport to teleporter2.) This indicates you could use a single function (in the same script) to give you more power (easier to maintain one function instead of two, and in this case, it also solves your problem). By combining your two scripts into one, they will be able to use the same function and they can also easily share debounce:

db = false
teleporter1 = script.Parent.TP1 --this requires you to put the script "beside" the teleporters in the explorer windows
teleporter2 = script.Parent.TP2
function Teleport(tch, destination)
    if tch.Parent == nil then return end --I added this for safety; projectiles can break Touch functions like this if the projectile deletes itself OnTouch
    if db == true then return end
    db = true
    local trs = tch.Parent:FindFirstChild("Torso")
    local lrm =  tch.Parent:FindFirstChild("Left Arm")
    local rrm =  tch.Parent:FindFirstChild("Right Arm")
    local lg =  tch.Parent:FindFirstChild("Left Leg")
    local rg =  tch.Parent:FindFirstChild("Right Leg")
    local hd =  tch.Parent:FindFirstChild("Head")
    if trs ~= nil and lrm ~= nil and rrm ~= nil and lg ~= nil and rg ~= nil and hd ~= nil then
        tch.Parent:MoveTo(destination)
        wait(2)
        db = false
    end
end)
teleporter1.Touched:connect(function(tch) Teleport(tch, teleporter2.Position) end)
teleporter2.Touched:connect(function(tch) Teleport(tch, teleporter1.Position) end)

Btw: if trs ~= nil and lrm ~= nil and rrm ~= nil and lg ~= nil and rg ~= nil and hd ~= nil then could just be if trs and lrm and rrm and lg and rg and hd then, as all instances count as "true" in an if statement.

You could further improve the script by keeping track of a different "db" for each player (by making db a table indexed by "tch.Parent" (if that's behaviour that you want).

Initialization: db = {}

Check for db: if db[tch.Parent] then return end

Activate db: db[tch.Parent] = true

Remove db: db[tch.Parent] = false

Ad

Answer this question