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

PlayerAdded Script not working in online?

--[iLegimate]--

game.Players.PlayerAdded:connect(function(player)
	if player.Name == "Player" then
		player:WaitForChild("StarterGear")
	local sc = game.ReplicatedStorage["HARD_DRIVE"].ScriptStorage.MapChoserApproval:Clone()
	local sc2 = game.ReplicatedStorage["HARD_DRIVE"].ScriptStorage.MapChoserApproval2:Clone()
		sc2.Parent = player.StarterGear
		sc.Parent = player.Backpack
		else
		return nil
	end
end)

So this works perfectly in studio..But for some reason when i test it in an actual game it wont work?

(MapChoserApproval 1 & 2 are scripts which clone a gui into the player's playergui) (My intention is to clone a gui into my playergui (Basically like a gear in startergear,But in PlayerGui)

5 comments

0

It's because of the if then statement on line 4. Unless someone named Player enters, your script will not run.

0

Um,Wait it has PlayerAdded event to it,So when a player joins it checks if the player's name is ilegimate,If it isnt it doesnt run

0

" if player.Name == "Player" then" That is 100% what line 4 says...

0

You don't need to check anything, the PlayerAdded event will only fire if an actual player connects to the server. Just remove the if statement and you'll be good to go.

0

And the return nil

1 answer

✓ Accepted answer

As the others have explained, line 4 is what prevents your code from executing. The reason it works in Studio, is because the testing server makes you the "Player" player. There's no need to check if it's an actual Player, because the PlayerAdded event only fires if it's a legitimate player. I also reduced some lines by moving stuff together.

game.Players.PlayerAdded:connect(function(player)
	player:WaitForChild("StarterGear")
	game.ReplicatedStorage["HARD_DRIVE"].ScriptStorage.MapChoserApproval:clone().Parent = player.Backpack
	game.ReplicatedStorage["HARD_DRIVE"].ScriptStorage.MapChoserApproval2:clone().Parent = player.StarterGear
end)

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