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

Trying to teleport players, but need to give data from one function to another, how?

Asked by 3 years ago

Hello there, I am trying to make a thing that teleports everybody that is trying to teleport to a place to that place when an event is triggered. If you click a button, it teleports you to a part this script is inside of that is transparent and has CanCollide off but has a touched event inside it that make a value inside the player true. My problem is that the thing that teleports people only happens once, and it can't happen again. I'm pretty sure the problem is that it's a changed event inside a touched event function, and needs to be outside of it. I need to retrieve the data from the touched event to the changed event.

local TeleportService = game:GetService("TeleportService")
local GameID = 5948456863

local TeleportBoolian = game.ReplicatedStorage.TeleportBoolian

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    if player then
        local CTB = player:FindFirstChild("CanTeleportBoolian")

        if CTB then
            print("CTB HERE")
            CTB.Value = true
            print("true")

            --where the code used to be
        end
    end
end)

--where the code is now
TeleportBoolian.Changed:Connect(function()
    print("Teleporting Phase 1")
    if TeleportBoolian and CTB.Value == true then
        print("Teleporting Phase 2")
        TeleportService:Teleport(GameID, player)
    end
end)

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago
Edited 3 years ago
local TeleportService = game:GetService("TeleportService")
local GameID = -1 -- change this back to your game id!

local TeleportBoolian = game.ReplicatedStorage.TeleportBoolian

local PlayersTable = {}
local deBounce = false -- a check to stop any repeat touches in a short amount of time.
script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    if player and deBounce == false then
        deBounce = true

--// just a small hack to mkae sure the bool teleport tag exists inside the player, if it doesnt then create it!
        local CTB = player:FindFirstChild("CanTeleportBoolian")
        if(not CTB) then
            CTB = Instance.new("BoolValue",player)
            CTB.Name = "CanTeleportBoolian"
        end
        if CTB ~= nil then
            print("CTB HERE")
            CTB.Value = true
           -- print("true")
            PlayersTable[player] = CTB
            TeleportBoolian.Value = true -- another hack to change the server teleport tag to true on player touch
            --where the code used to be
            wait(1)
            deBounce = false
        end
    end
end)

--where the code is now
TeleportBoolian.Changed:Connect(function(TeleBool)
    print("Teleporting Phase 1")
    for player,CTB in pairs(PlayersTable) do
        if TeleBool == true and CTB.Value == true then
            print("Teleporting Phase 2")
            print("Teleporting[",player,"]")
            TeleportService:Teleport(GameID, player)
            PlayersTable[player] = nil -- might bug out a little due to us removing the player from the same table we're working on, but should be good
        end
    end
end)
--[[
while true do -- a debugging loop just to see whats going on so not needed!
    print(TeleportBoolian.Value)
    wait()
end
]]

So i think whats going on with things not triggering is the way we was changing the global teleport trigger. I've tried to change the value from my studio and it didnt respond so i decided to change the value from within the script and it works. I also added a deBounce check to stop any spamming of touches when you run into the teleport part.

Hope this helps you in some way :)

0
im gonna try this out and see if it works MrGustavio 34 — 3y
0
sorry, but it's still running into the same problem, ill try something i think is gonna work tommorow MrGustavio 34 — 3y
0
Think i found the problem its when you change the global TeleportBoolian Value it doesnt get replicated on players. so i've added a way to change it from this server side script. (Note: i've changed the Gameid to 1 so i dont teleport to your game unexpectally lol), Updatd my answer TGazza 1336 — 3y
Ad

Answer this question