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

PlayerAdded() not triggering, why?

This is not the full script, but this is the part of it that's bothering me. Neither of these 2 things get printed and the event doesn't get fired, any ideas on how to fix that?

wait()
local players = game:GetService("Players")




players.PlayerAdded:Connect(function(plr)
	
	local player = players.LocalPlayer
	local character = player.Character
local humanoid = character.Humanoid
local hp = character.Humanoid.Health
local maxhp = character.Humanoid.MaxHealth

	print("playeradded")
	
	if plr == player then
		
		print("plradded") 
		
		workspace.Events.DFEvents.PhoenixFruitEvents.ParticlesE:FireServer()
	

2 comments

0

Are there any errors? Also why are you redefining locaplayer every time someone joins?

0

You can't set player variable value to players.LocalPlayer because you have already plr variable returned by the function.

3 answers

You have forgotten to add the ends:=) LocalScript cannot run if its in workspace

From the roblox wiki:

A LocalScript will only run Lua code if it is a descendant of one of the following objects:

A Player’s Backpack, such as a child of a Tool

A Player’s Player/Character|character model

A Player’s PlayerGui

A Player’s PlayerScripts

The ReplicatedFirst service

wait()
local players = game:GetService("Players")




players.PlayerAdded:Connect(function(plr)

    local player = players.LocalPlayer
    local character = player.Character
local humanoid = character.Humanoid
local hp = character.Humanoid.Health
local maxhp = character.Humanoid.MaxHealth

    print("playeradded")

    if plr == player then

        print("plradded") 

        workspace.Events.DFEvents.PhoenixFruitEvents.ParticlesE:FireServer()
end
end)

3 comments

0

Fixed the code block.

0

local player = plr

0

Thx. I was in a hurry. I saw the mistake. But didn't get time to fix:=)

here are a few flaws that are are probably leading to errors..

1). You forgot the end keyword, this will lead to a syntax error

2). The code has to be in a server script..

3).you can't use Players.LocalPlayer in a server script..

so here is a example structure that is guaranteed to work:

make sure the code is in server script

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	print(player.Name," has joined the server!")
	
	--the rest of the code goes here..!	

end)

4 comments

0

I did count all of that in, and here are my explanations why i did that:

0
  1. this article says that since recently it can be used ina local script https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded
0
  1. The code i posted is not the full script, cause i have some other things not linked to this going on in there, but all of the ends are there
0

well, here is a thing bud, "PlayerAdded" event won't run in a LocalScript.. copy my sample code, and put it in a sever script, and add ur additional code, and I guarantee it will work

There are a few things that hinder your PlayerAdded for firing the way you want to.

The first reason is the wait() at the top of your script. This will yield your script and cause the listener to be connected at a later time, which will not fire for when the first player joins.

The second reason is this being a LocalScript. LocalScript connections to a PlayerAdded event will only fire for the players joining after the client.

An additional note, you should also use the event player.CharacterAdded to wait for the character to finish loading.

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