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

How do I fix this problem with my Queue Teleport Script?

Asked by 3 years ago

I have this script that I worked on, and it doesn't seem to work even if I use the servers with 2 players, they never teleport. Any help with this would be greatly appreciated, I've searched around and nothing of value has come up.

It is meant to work like this; If 2 people are standing on 'Part', then they get teleported to a different game.

ts=game:GetService("TeleportService")
player_count = 0

game.Workspace.Part.Touched:Connect(function(hit)
    player_count = player_count + 1
    if player_count == 2 then
        game.Workspace.Part.Touched:Connect(function(hit)
            if hit.Parent and hit.Parent.Humanoid then
                ts:Teleport(7586111845)
            end
        end)
    end
end)

Thank you in advance.

1 answer

Log in to vote
0
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago

Problem

Players are not being teleported when your condition is met.

Solution

The first thing I noticed is you have an event inside of the same event. Don't do this.

Next, if you don't give the Teleport method a player object to teleport, nothing will happen to the player. (In the code below, I changed the method to TeleportPartyAsync)

The last thing is how you are incrementing the player_count variable. You have not confirmed that the 'hit' part is a descendant (or child) of the player's character.

Code

local players_service = game:GetService("Players")
local ts = game:GetService("TeleportService")
players_touching = {}

workspace:WaitForChild("Part").Touched:Connect(function(hit)
    local player = players_service:GetPlayerFromCharacter(hit.Parent)
    if player ~= nil then
        if table.find(players_touching,player) then
            table.insert(players_touching,player)
            if #players_touching >= 2 then
                ts:TeleportPartyAsync(7586111845,players_touching)
                players_touching = {}
            end 
        end
    end
end)

workspace:WaitForChild("Part").TouchEnded:Connect(function(hit)
    local player = players_service:GetPlayerFromCharacter(hit.Parent)
    if player ~= nil then
        local index = table.find(players_touching,player)
        if index ~= nil then
            table.remove(players_touching,index)
        end
    end
end)

players_service.PlayerRemoving:Connect(function(player)
    local index = table.find(players_touching,player)
    if index ~= nil then
        table.remove(players_touching,index)
    end
end)

I added 2 new events. The PlayerRemoving event which fires when a player leaves the game, and the TouchEnded event which fires when a BasePart stops touching another BasePart. When the player is no longer touching the part, we want to prevent them from being included in the teleport.

I also changed the 'player_count' variable which was a number to the 'players_touching' variable which is now a table that stores the player object value for each player we plan on teleporting.

Conclusion

If anything I have said that is confusing, you find any more bugs or you would like more help with another topic, you can contact me through my Discord server.

0
This worked, thank you. I took everything that you said into consideration and tried to understand the code, which I did and it will help me avoid the same mistake in the future. I've learned some stuff from this too, Thank you~ TheJacksterYT 19 — 3y
Ad

Answer this question