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

teleport people that are in a certain table?

Asked by 8 years ago

okay, so i made this code so that i can teleport people to a competition, but only people who's names are in the table "competitors". This code doesn't seem to work. Can i get any help? Much appreciated :)

    local spawns = chosencompetitionClone.Spawns:GetChildren()
    for i,v in pairs(competitors) do
        name = table.concat(v)
        check = game.Workspace:FindFirstChild(name)
        if check then
            check:MoveTo(spawns[i].Position)
        end
    end

1 answer

Log in to vote
2
Answered by 8 years ago

Couple of errors, the first error is that you are trying to find the character's model using their name, that is not a really efficient way, because sometimes the name can change, so to prevent it from glitching we use this:

local spawns = chosencompetitionClone.Spawns:GetChildren()

for i,v in pairs(competitors) do
    if v.Character then -- check if the player's model exists
        v.Character:MoveTo(spawns[math.random(1, #spawns)].Position)
    end
end

The second error is that you were trying to use spawns[i].Position while you were iterating through competitors, meaning that "i" would respond to the number of the current competitor, and not the spawn brick, which may error if you have a smaller amount of spawn bricks than players in the table because it would not be able to find it.

So the fifth line generates a random number between the number one and the number of spawn bricks there are in the "Spawns" model.

Also I think you should read more about table.concat because you shouldn't have had it there: table.concat is used to connect to strings in a table together to make one sentence or word, depending on what you have as the separating character in the second parameter, here you can learn more:

http://wiki.roblox.com/index.php?title=Function_dump/Table_manipulation#table.concat

Hope that helped, if you have any questions I will be glad to answer them.

Ad

Answer this question