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

PlayerAdded event not working?

Asked by 6 years ago

I have a code here which adds a player to my custom made playerlist here.

The PlayerAdded event in this local script does not work, I get no error, nor a print which is part of the code.

game.Players.PlayerAdded:Connect(function(plr)
    wait(0.1)
    print(plr.Name)
    local playername = plr.Name
    script.Parent.PlayerTemplate:Clone().Parent = script.Parent.Background
    local frame = script.Parent.Background:WaitForChild("PlayerTemplate")
    wait()
    frame.Name = playername
    frame.Visible = true
    frame.Name = (plr.Name)
    frame.PlayerName.PlayerName.Text = playername
    frame.Rank.Rank.Text = playerfolder[plr.Name].Rank.Value
    game.Workspace.Players[plr.Name].Team.Changed:connect(function()
        local pf = game.Workspace.Players[plr.Name]
        print(pf.Team.Value)
        if pf.Team.Value == game.Teams.Sierra.Name then
            print(plr.Name.." updating leaderboards so this player is in team Sierra")
            print(script.Parent.Background[plr.Name].BackgroundColor3)
            script.Parent.Background[plr.Name].BackgroundColor3 = Color3.new(0, 170, 255)
        elseif pf.Team.Value == game.Teams.Tango.Name then
            print(script.Parent.Background[plr.Name].BackgroundColor3)
            script.Parent.Background[plr.Name].BackgroundColor3 = Color3.new(170, 0, 0)
            print(plr.Name.." updating leaderboards so this player is in team Tango")
        else
            print(script.Parent.Background[plr.Name].BackgroundColor3)
            print(plr.Name.." updating leaderboards so this player is not in team Tango or Sierra")
            script.Parent.Background[plr.Name].BackgroundColor3 = Color3.new(0,0,0)
        end
    end)
    if plr:IsInGroup(4023191) then
        frame.Capbadge.ImageLabel.Image = ("rbxassetid://1567461093")
    elseif plr:IsInGroup(1197643) then
        frame.Capbadge.ImageLabel.Image = ("rbxassetid://117739276")
    elseif plr:IsInGroup(1197645) then
        frame.Capbadge.ImageLabel.Image = ("rbxassetid://239367802")
    elseif plr:IsInGroup(4030193) then
        frame.Capbadge.ImageLabel.Image = ("rbxassetid://756770459")
    elseif plr:IsInGroup(4023094) then
        frame.Capbadge.ImageLabel.Image = ("rbxassetid://193965796")
    elseif plr:IsInGroup(3773876) then
        frame.Capbadge.ImageLabel.Image = ("rbxassetid://702638350")
    end
end)
0
When I add a break in my code it shows plr.Name as a value not found. denizprodutions 0 — 6y
0
Does the script fire the "print(plr.Name)" on line 3? UgOsMiLy 1074 — 6y
0
Not sure, but I think it might. But nothing is printed in the console, but when I add a break in my code and hover over "plr.Name" it shows plr.Name as a value not found. denizprodutions 0 — 6y
0
Are you testing this in a Roblox Studio single player test? Because PlayerAdded doesn't fire if you do that. tentastic 32 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

This is happening because PlayerAdded is server-side only (it's only useful on the server side), and the player joins before the script runs, and when it does, it waits for the NEXT player to join, and it would run code on them. A fix to your script would be to use LocalPlayer instead.

local plr = game:GetService("Players").LocalPlayer
    wait(0.1)
    print(plr.Name)
    local playername = plr.Name
    script.Parent.PlayerTemplate:Clone().Parent = script.Parent.Background
    local frame = script.Parent.Background:WaitForChild("PlayerTemplate")
    wait()
    frame.Name = playername
    frame.Visible = true
    frame.Name = playername
    frame.PlayerName.PlayerName.Text = playername
    frame.Rank.Rank.Text = playerfolder[plr.Name].Rank.Value
   gam.Workspace.Players[plr.Name].Changed:Connect(function()
        local pf = game.Workspace.Players[plr.Name]
        print(pf.Team.Value)
        if pf.Team.Value == game.Teams.Sierra.Name then
            print(plr.Name.." updating leaderboards so this player is in team Sierra")
            print(script.Parent.Background[plr.Name].BackgroundColor3)
            script.Parent.Background[plr.Name].BackgroundColor3 = Color3.new(0, 170/255, 255/255)
        elseif pf.Team.Value == game.Teams.Tango.Name then
            print(script.Parent.Background[plr.Name].BackgroundColor3)
            script.Parent.Background[plr.Name].BackgroundColor3 = Color3.new(170/255, 0, 0)
            print(plr.Name.." updating leaderboards so this player is in team Tango")
        else
            print(script.Parent.Background[plr.Name].BackgroundColor3)
            print(plr.Name.." updating leaderboards so this player is not in team Tango or Sierra")
            script.Parent.Background[plr.Name].BackgroundColor3 = Color3.new(0,0,0)
        end
    end)
    if plr:IsInGroup(4023191) then
        frame.Capbadge.ImageLabel.Image = "rbxassetid://1567461093"
    elseif plr:IsInGroup(1197643) then
        frame.Capbadge.ImageLabel.Image = "rbxassetid://117739276"
    elseif plr:IsInGroup(1197645) then
        frame.Capbadge.ImageLabel.Image = "rbxassetid://239367802"
    elseif plr:IsInGroup(4030193) then
        frame.Capbadge.ImageLabel.Image = "rbxassetid://756770459"
    elseif plr:IsInGroup(4023094) then
        frame.Capbadge.ImageLabel.Image = "rbxassetid://193965796"
    elseif plr:IsInGroup(3773876) then
        frame.Capbadge.ImageLabel.Image = "rbxassetid://702638350"
    end
0
Actually it is also client side as well. As stated in the developer wiki: https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded - In the Notes section it states it. jonathanpecany100 0 — 4y
Ad

Answer this question