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
11 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:

1game.Workspace.ChildAdded:connect(function(child)
2    if game.Players:GetPlayerFromCharacter(child) then
3        player = game.Players:GetPlayerFromCharacter(child)
4        if workspace:findFirstChild("Home"..player.Name) then
5            x = workspace:findFirstChild("Home"..player.Name)
6            player.Character.Torso.CFrame = CFrame.new(Vector3.new(x.Home.spawnbrick.Position))
7        end
8    end
9end)

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 11 years ago

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

01function Respawn(prop,player)
02    if prop == "Character" then
03        x = workspace:findFirstChild("Home"..player.Name)
04        player.Character.Torso.CFrame = CFrame.new(Vector3.new(x.Home.spawnbrick.Position))
05    end
06end
07 
08game.Workspace.ChildAdded:connect(function(child)
09    if game.Players:GetPlayerFromCharacter(child) then
10        player = game.Players:GetPlayerFromCharacter(child)
11        if workspace:findFirstChild("Home"..player.Name) then
12            x = workspace:findFirstChild("Home"..player.Name)
13        player.Changed:connect(function(prop) Respawn(prop,player) end)
14            player.Character.Torso.CFrame = CFrame.new(Vector3.new(x.Home.spawnbrick.Position))
15        end
16    end
17end)

This should work.

0
It did not work. wrenzh 65 — 11y
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 — 11y
Ad
Log in to vote
0
Answered by
Nickoakz 231 Moderation Voter
11 years ago

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

01function Character(p)
02    p.CharacterAdded:connect(function(char)
03        char:WaitForChild("Torso")
04        local x = Workspace:findFirstChild("Home"..player.Name)
05        player.Character.Torso.CFrame = CFrame.new(Vector3.new(x.Home.spawnbrick.Position))
06    end)
07end
08 
09game.Players.PlayerAdded:connect(function(p)
10    Character(p)
11end)
12 
13all=game.Players:getChildren()
14for a=1,#all do
15    Character(all[a])
16end

Edit: Script missing something, fixed.

Answer this question