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

Why is the player returning as nil?

Asked by 8 years ago
Edited 8 years ago

Server Script in Workspace:

game.Players.PlayerAdded:connect(function(plr)
    local value = Instance.new("StringValue")
    value.Value = plr.Name
    value.Parent = game.Lighting.PlayerStore
    script.Trigger:FireAllClients(plr)
end)

game.Players.PlayerRemoving:connect(function(plr)
    if game.Lighting.PlayerStore:FindFirstChild(plr.Name) then
        game.Lighting.PlayerStore[plr.Name]:Remove()
    end
    script.Trigger2:FireAllClients(plr)
end)

LocalScript in StarterGui under a ScreenGui

local localplayer = game.Players.LocalPlayer
local currentSlot = 0
local back = script.Parent.Back
local players = game.Lighting.PlayerStore:GetChildren()

local function updatelist()
    for i,v in pairs(back:GetChildren()) do
        v.Position = UDim2.new(.02, 0, 0, (35*currentSlot))
    end
    back.Size = UDim2.new(.2, 0, (.048 * currentSlot), 0)
end

game.Workspace.Script.Trigger.OnClientEvent:connect(function(plr)
    local player = plr.Name

    if player ~= localplayer.Name then
        local default = script:WaitForChild("Default"):Clone()
        default.Parent = back
        default.PlayerName.Text = player
        default.Name = player
        default.Slot.Value = currentSlot
        default.Position = default.Position + UDim2.new(0, 0, 0, (35 * currentSlot))
        back.Size = back.Size + UDim2.new(0, 0, (.048 * currentSlot), 0)
        currentSlot = currentSlot + 1
    elseif player == localplayer.Name then
        for i,v in pairs(players) do
            local default = script:WaitForChild("Default"):Clone()
            default.Parent = back
            default.PlayerName.Text = v.Value
            default.Name = v.Value
            default.Slot.Value = currentSlot
            default.Position = default.Position + UDim2.new(0, 0, 0, (35 * currentSlot))
            back.Size = back.Size + UDim2.new(0, 0, (.048 * currentSlot), 0)
            currentSlot = currentSlot + 1
        end     
    end
end)

game.Workspace.Script.Trigger2.OnClientEvent:connect(function(plr)
    local player = plr.Name --Here it errors, it says plr is nil?
    local slot
    for i,v in pairs(back:GetChildren()) do
        if v.Name == player then
            slot = v.Slot.Value
            v:Destroy()
        end
    end
    if slot ~= nil then
        for i,v in pairs(back:GetChildren()) do
            if v.Slot.Value > slot then
                v.Slot.Value = v.Slot.Value - 1
            end
        end
    end
    currentSlot = currentSlot - 1
    slot = nil
    updatelist()
end)

I am making a custom PlayerList, and for some reason, it returns player as nil (I put a note next to where it is erroring at)

Help is appreciated, thanks! :D

1 answer

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Replication

It's nil because it genuinely is nil. Because the player was removed from the game, aside from the closure of the Instance, it is no longer replicated and does not exist on the client. Solution? Connect to the events locally - It works now.

In the off chance that it doesn't work locally, send the UserId instead.

0
that doesn't work thehybrid576 294 — 8y
Ad

Answer this question