PlayerAdded function on client?

– NinjoOnline –

local lighting = game:GetService('Lighting')

local blur = lighting:WaitForChild('Blur')

local frame = script.Parent

local XY = frame.Parent

local ui = XY.Parent

local gui = ui.Parent

local hud = gui:WaitForChild('HUD')

local title = frame:WaitForChild('Title')

print('1')

game.Players.PlayerAdded:Connect(function(player)

print('2')

repeat wait() until player.Character

print('3')

XY:TweenPosition(UDim2.new(0, 0, 0.5, 0), 'Out', 'Quad', 1, false)

wait(5)

XY:TweenPosition(UDim2.new(0, 0, 1.3, 0), 'Out', 'Quad', 1, false)

wait(0.5)

for i = 30, 0, -3 do

blur.Size = i

wait()

end

hud.Enabled = true

ui:Destroy()

end)

Prints 1, but nothing after the player added function. Wiki states that works with client now so idk why it ain’t working for this. This is all in a LocalScript, which the wiki states:

  • Up until recently, this event didn’t work on the client (in Localscript s), but this has been changed
1 Like

It will print 1, but you need another player to join in order for it to fire, it cannot fire for yourself.

2 Likes

But if its in a localscript, then it should fire for the local client?

The script loads in after the player does, it’s not logically possible for it to detect PlayerAdded because the player itself is already added by the time the script runs.

2 Likes

How would I go abouts having a local script wait for the player to join before firing something (for say an intro to a game) without having to connect remote events and all that

1 Like

If I’m understanding you correctly then all you need to do is create a local script with a wait(x) delay and parent it to StarterPlayerScripts or ReplicatedFirst, no need for the PlayerAdded listener since the script will be localized to the player itself and will run when it’s ready.

All you’d need to do is add a short wait and 0ut the script into starter player scripts. Does this solve your problem?

Also doing player added from the client does not make it FE if that’s what you are curious about. But just doing what I did above will fix it.

You just need to yield until you have relative proof that the player has mostly loaded.

repeat wait() until game.Players.LocalPlayer.Character
local c = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

These are the two methods I use to do this.
I would not rely on PlayerAdded in LocalScripts if your goal is purely to yield temporarily.

6 Likes

@SummerEquinox comment is probably yoru best bet for what you are trying to do.

For my style of coding I do the folowing:

For client sided operations I would recomend keeping the player’s client slightly seperate from the rest of the players if you are using PlayerAdded. The idea here is you can check for the local client details first and add what ever that client needs, then check for the rest of the players using PlayerAdded or a loop through all the current players for their own sorts of functions that you may require.

This is useful for when you have all sorts of complicated client sided effects for your character and you apply them first. Every other client can have seperate functions for replication and so forth. This is great for when you are replicating players client sided instead of server sided.

This is the client alternative to player added

game.Players.ChildAdded:connect(function(pl)
if pl:IsA(‘Player’) then
–code here
end
end)

2 Likes

If I understand you correct, you want it to fire when the player joins? You don’t have to use playeradded, as the script will automatically go off when it has loaded. If you want to wait until the game is loaded, use game:IsLoaded()

1 Like