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

Teleporting system not working?

Asked by 3 years ago

I'm trying to make a teleport system to where players can 1v1 each other. Please help, thanks much! <3

local mat = script.Parent --model with all the parts and scripts

local TPStart1 = mat.TPStart1 --part which one player has to stand on to get teleported
local TPStart2 = mat.TPStart2 --part which another player has to stand on to get teleported
local TPEnd1 = mat.TPEnd1 --endpoint part of player on TPStart1
local TPEnd2 = mat.TPEnd2 --endpoint part of player on TPStart2

local bothReady = false
local TP1Ready = false
local TP2Ready = false


TPStart1.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

    TP1Ready = true

    if humanoid and bothReady then
        local rootPart = humanoid.Parent:WaitForChild("HumanoidRootPart")
        rootPart.CFrame = CFrame.new(TPEnd1.Position)
    end
end)

TPStart2.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

    TP1Ready = false

    if humanoid and bothReady then
        local rootPart = humanoid.Parent:WaitForChild("HumanoidRootPart")
        rootPart.CFrame = CFrame.new(TPEnd2.Position)
    end
end)

if TP1Ready and TP2Ready then
    bothReady = true
end
0
Are there any errors? Galaxybombboy 134 — 3y
0
No Dehydrocapsaicin 483 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

The issue is line 35. You only check if both variables are true once. To circumvent this, you should either set up a while wait loop (which is considered polling and therefore inefficient) or you can take the more efficient route and use a BindableFunction.

When a BindableFunction objects :Invoke() method is called, it will run the function you bind to it when you do BindableFunction.OnInvoke = someFunction. :Invoke() will also pass over any arguments to the function parameters as well.

Here is the completed script:

local mat = script.Parent --model with all the parts and scripts

local TPStart1 = mat.TPStart1 --part which one player has to stand on to get teleported
local TPStart2 = mat.TPStart2 --part which another player has to stand on to get teleported
local TPEnd1 = mat.TPEnd1 --endpoint part of player on TPStart1
local TPEnd2 = mat.TPEnd2 --endpoint part of player on TPStart2

local bothReady = false
local TP1Ready = false
local TP2Ready = false

local readyEvent = Instance.new("BindableFunction")

TPStart1.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
    TP1Ready = true
    readyEvent:Invoke()
    if humanoid and bothReady then
        local rootPart = humanoid.Parent:WaitForChild("HumanoidRootPart")
        rootPart.CFrame = CFrame.new(TPEnd1.Position)
    end
end)

TPStart2.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
    TP2Ready = true
    readyEvent:Invoke()
    if humanoid and bothReady then
        local rootPart = humanoid.Parent:WaitForChild("HumanoidRootPart")
        rootPart.CFrame = CFrame.new(TPEnd2.Position)
    end
end)

readyEvent.OnInvoke = function()
    bothReady = TP1Ready and TP2Ready
end

I must remind you that I see multiple issues with your code. It has a high likelihood of being anywhere from very buggy to completely dysfunctional. I have only fixed your issue and nothing else.

Ad

Answer this question