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

PlayerAdded() not triggering, why?

Asked by
Echtic 128
4 years ago

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()

0
Are there any errors? Also why are you redefining locaplayer every time someone joins? Smash108 70 — 4y
0
You can't set player variable value to players.LocalPlayer because you have already plr variable returned by the function. NiniBlackJackQc 1562 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited by DeceptiveCaster 4 years ago

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)
0
Fixed the code block. DeceptiveCaster 3761 — 4y
0
local player = plr NiniBlackJackQc 1562 — 4y
0
Thx. I was in a hurry. I saw the mistake. But didn't get time to fix:=) Skydoeskey 108 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)
0
I did count all of that in, and here are my explanations why i did that: Echtic 128 — 4y
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 Echtic 128 — 4y
0
2. 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 Echtic 128 — 4y
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 User#23252 26 — 4y
Log in to vote
0
Answered by
megukoo 877 Moderation Voter
4 years ago

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.

Answer this question