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

How would I check for every time a player spawns and then teleport them?

Asked by
wrenzh 65
10 years ago

I want to check every time a player spawns in the game and then teleport them to their "campsite".

The code I have right now is this:

game.Workspace.ChildAdded:connect(function(child)
    if game.Players:GetPlayerFromCharacter(child) then
        player = game.Players:GetPlayerFromCharacter(child)
        if workspace:findFirstChild("Home"..player.Name) then
            x = workspace:findFirstChild("Home"..player.Name)
            player.Character.Torso.CFrame = CFrame.new(Vector3.new(x.Home.spawnbrick.Position))
        end
    end
end)

Output is returning no errors, and I can't find any either.

Can someone please kindly help?

2 answers

Log in to vote
0
Answered by 10 years ago

Everytime the character itself is changed, which in otherwords "Respawns" then it would execute the following.


function Respawn(prop,player) if prop == "Character" then x = workspace:findFirstChild("Home"..player.Name) player.Character.Torso.CFrame = CFrame.new(Vector3.new(x.Home.spawnbrick.Position)) end end game.Workspace.ChildAdded:connect(function(child) if game.Players:GetPlayerFromCharacter(child) then player = game.Players:GetPlayerFromCharacter(child) if workspace:findFirstChild("Home"..player.Name) then x = workspace:findFirstChild("Home"..player.Name) player.Changed:connect(function(prop) Respawn(prop,player) end) player.Character.Torso.CFrame = CFrame.new(Vector3.new(x.Home.spawnbrick.Position)) end end end)

This should work.

0
It did not work. wrenzh 65 — 10y
0
"should" lol Anyways the idea is just to check every time the character is changed, it executes the teleport to the home brick. soaprocks2 75 — 10y
Ad
Log in to vote
0
Answered by
Nickoakz 231 Moderation Voter
10 years ago

I wouldn't recommend looking inside Workspace for an added character. Insted, try doing it this way.

function Character(p)
    p.CharacterAdded:connect(function(char)
        char:WaitForChild("Torso")
        local x = Workspace:findFirstChild("Home"..player.Name)
        player.Character.Torso.CFrame = CFrame.new(Vector3.new(x.Home.spawnbrick.Position))
    end)
end 

game.Players.PlayerAdded:connect(function(p)
    Character(p)
end)

all=game.Players:getChildren()
for a=1,#all do
    Character(all[a])
end 

Edit: Script missing something, fixed.

Answer this question