Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

1 comment

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

1 answer

✓ Accepted answer

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)

7 comments

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.

0

Also, I despise anonymous functions :p

0

Alright, I'll edit my answer.

0

Answer edited.

0

Thanks. I will accept yours since you were the first to answer.

0

Wait, why a repair wait until the character exists? Isnt that the entire point of the CharacterAdded event?

1

@Crazy It's a safety measure in case the character hasn't loaded yet.

This discussion is preserved as it was. New answers, comments, and votes are closed.