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

How to properly set the player spawn position?

Asked by 10 years ago

I have a bunch of models that have their own spawn positions called SpawnPoint. I want to make it so the model which has the FactoryNumber as the same as LocalPlayer does, and OwnerName is the same as LocalPlayer.Name, make the player spawn there, however, it isn't working. Here is my script:

game.Players.PlayerAdded:connect(function(player)
    print ("[MainScript] Setting up player "..player.Name)
    setupPlayer(player)

    --[[print ("[MainScript] Loading stats for player "..player.Name)
    if loadPlayerStats(player) then
        print ("[MainScript] "..player.Name.." stats were loaded successfully!")
    else
        print ("[MainScript] Could not load stats for "..player.Name..". Reason: player not connected.")
    end]]

    for _,factory in pairs(game.Workspace.Factories:GetChildren()) do
        local isOwnedBy = factory.OwnerName
        local factoryNumber = factory.FactoryNumber
        local spawnPos = factory.SpawnPoint.Position

        if isOwnedBy.Value == "" and player.FactoryNumber.Value == 0 then
            player.FactoryNumber.Value = factoryNumber.Value
            isOwnedBy.Value = player.Name
        end

        if isOwnedBy.Value == player.Name and player.FactoryNumber.Value == factoryNumber.Value then
            player.CharacterAdded:connect(function(character)
                character.Torso.Position = spawnPos
            end)
        end     

    end

end)

while wait(.1) do
    for _,player in pairs(game.Players:GetPlayers()) do
        local pFactory = player.FactoryNumber

        for __,factory in pairs(game.Workspace.Factories:GetChildren()) do
            if factory.OwnerName.Value == player.Name and factory.FactoryNumber.Value == pFactory.Value then
                player.CharacterAdded:connect(function(character)
                    character.Torso.Position = factory.SpawnPoint.Position
                end)
            end
        end
    end
end

Answer this question