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

Players will not move to part?

Asked by 5 years ago
Edited 5 years ago

I have cloned a part to workspace inside a folder named mapHolder. Anyways there are spawns in there that I am trying to make every player teleport to, however, this will not work and there are no errors.

Teleport code:

-- this code should teleport a player to a random spawn part on the map
-- mapSelected is a string value which indeed holds the name of the map

function teleportPlayers()
    for i,v in pairs(allPlayers) do
        wait()
        if allPlayers[v] ~= nil then
            print("Player is there")
            if allPlayers[v].Character ~= nil then
                print("Character is there")
                if allPlayers[v]:FindFirstChild("Humanoid").Health ~= 0 then
                    print("Player is alive")
                    local mapSpawns = mapHolder[mapSelected.Value]:FindFirstChild("Spawns"):GetChildren()
                    allPlayers[v].Character:FindFirstChild("HumanoidRootPart").CFrame = mapSpawns[math.random(1, #mapSpawns)].CFrame-- + Vector3.new(0, 3, 0)
                end
            end
        end
    end
end

1 answer

Log in to vote
1
Answered by 5 years ago

On this line,

 if allPlayers[v] ~= nil then

You are trying to do allPlayers[Player] instead of the index of the player in the table.

It should be either just v or allPlayers[i] like this,

function teleportPlayers()
        for i,v in pairs(allPlayers) do
            wait()
            if v ~= nil then
                print("Player is there")
                if v.Character ~= nil then
                    print("Character is there")
                    if v:FindFirstChild("Humanoid").Health ~= 0 then
                        print("Player is alive")
                        local mapSpawns = mapHolder[mapSelected.Value]:FindFirstChild("Spawns"):GetChildren()
                        v.Character:FindFirstChild("HumanoidRootPart").CFrame = mapSpawns[math.random(1, #mapSpawns)].CFrame-- + Vector3.new(0, 3, 0)
                    end
             end
            end
        end
    end

0
Weird, it still doesn't work. YouSNICKER 131 — 5y
0
Fixed it.... allPlayers I guess was the problem instead I just did game.Players:GetChildren() YouSNICKER 131 — 5y
Ad

Answer this question