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

Using ".PlayerAdded" event. What am I doing wrong?

Asked by
Necrorave 560 Moderation Voter
10 years ago

I am trying to experiment with events and methods I have yet to use, and this was one of them.

I am not really sure what I did wrong with this, although. If somone could explain to me what I am missing that would be fantastic.

function spawn(player)
    player = game.Workspace:FindFirstChild(player.Name)
        if player ~= nil then
            player:MoveTo(Vector3.new(1, 1, 1))
    end
end

game.Players.PlayerAdded:connect(spawn)

Thanks

EDIT: No errors are being displayed in the Output. Probably due to my if statement protecting my script from having any errors.

EDIT2: I would prefer if you did not rewrite something entirely different. Unless you explain why my method was incorrect to even attempt.

0
i dont know if the if statements is actually protecting your code or not OR just all if statements dont work normally in a playerAdded event proROBLOXkiller5 112 — 3y

1 answer

Log in to vote
2
Answered by 10 years ago

My recommended way:

game.Players.PlayerAdded:connect(function(player) --Anonymous function for when the player is added, keep an open parenthesis (bracket).
    player.CharacterAdded:connect(function(char) --Anonymous function for when the player's character has been added/respawned. Keep an open parenthesis (bracket).
        repeat wait() until char --Waits until the char variable is available
            char:MoveTo(Vector3.new(1, 1, 1)) --Moves the character to Vector3 point 1,1,1.
    end) --Ends character added anonymous function with a closing parenthesis to make up for the open parenthesis at line 2.
end) --Ends player added anonymous function with a closing parenthesis to make up for the open parenthesis at line 1.

More info:

http://wiki.roblox.com/index.php?title=Anonymous_function

What you did wrong:

You're replacing the player variable, when you can go and use the player's Character property. I also recommend linking that function to the player's CharacterAdded event in a separate function for the Player Added event.

function Spawn(char)
    repeat wait() until char
    char:MoveTo(Vector3.new(1, 1, 1))
end

function PAdded(player)
    player.CharacterAdded:connect(Spawn)
end

game.Players.PlayerAdded:connect(PAdded)
0
Sorry, but this was not the answer I was looking for. Thanks for giving me something, but I want to know what I am doing wrong and why its wrong. Necrorave 560 — 10y
0
Also, I despise anonymous functions :p Necrorave 560 — 10y
0
Alright, I'll edit my answer. Spongocardo 1991 — 10y
0
Answer edited. Spongocardo 1991 — 10y
View all comments (3 more)
0
Thanks. I will accept yours since you were the first to answer. Necrorave 560 — 10y
0
Wait, why a repair wait until the character exists? Isnt that the entire point of the CharacterAdded event? iaz3 190 — 10y
1
@Crazy It's a safety measure in case the character hasn't loaded yet. Spongocardo 1991 — 10y
Ad

Answer this question