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

Why can't I find the player from String?

Asked by 7 years ago

It's a script a leader board, which works by collecting all the players to a table, then generating a Frame for each of them, one by one and it always restarts when someone leaves/joins. The problem is that I can't the Player using the table I have. It just won't find it.

(Non local-script)

local playercount = 0
local playerlist = {}
local curr_player

function Regenerate()
    playerlist = {}
    for _, player in pairs(game.Players:GetPlayers()) do
        table.insert(playerlist, player)
    end
    playercount = #playerlist

    for i=1, playercount+1 do
        curr_player = game.Players:FindFirstChild(playerlist[i])
        local persFrame = Instance.new("Frame", script.Parent.Main)
        --Design..
        local persPoint = Instance.new("TextBox", persFrame)
        persPoint.Name = "Points"
        --Desing...
        print(playerlist[i])    --This shows the player's name correct
        persPoint.Text = curr_player.Name  --This is where I get the error
    end
end

function onPlayerAdded(player)
    Regenerate()
end

game.Players.PlayerRemoving:connect(function(player)
    Regenerate()
end)

game.Players.PlayerAdded:connect(onPlayerAdded)

I get an error that says it can't get "curr_player" Name, because it's a nil. I'm sure it must be a tiny error, but i'm really looking forward to find it out. Thanks for the advice.

0
Why do you need to create a separate for loop to add a GUI to them? Why not just do it for the first for loop? TheeDeathCaster 2368 — 7y

2 answers

Log in to vote
0
Answered by
systack 123
7 years ago

You were feeding findfirstchild a userdata type.

 curr_player = game.Players:FindFirstChild(playerlist[i])

Instead of a string type.

 curr_player = game.Players:FindFirstChild(playerlist[i].Name)

I fixed up your code a bit, it should work just fine :)

local playercount,playerlist,curr_player
function Regenerate()
    playerlist = game.Players:GetPlayers()
    playercount = #game.Players:GetPlayers()
    for i= 1, playercount do
        curr_player = game.Players:FindFirstChild(playerlist[i].Name)
        local persFrame = Instance.new("Frame", script.Parent.Main)
        --Design..
        local persPoint = Instance.new("TextBox", persFrame)
        persPoint.Name = "Points"
        --Design..
      persPoint.Text = curr_player.Name  
    end
end
game.Players.PlayerAdded:connect(Regenerate);game.Players.PlayerRemoving:connect(Regenerate)

0
You can use the generic for instead of having to go through that whole process. TheeDeathCaster 2368 — 7y
Ad
Log in to vote
0
Answered by
waifuSZN 123
7 years ago
local Players = game:GetService("Players")
local curr_player = game.Players.LocalPlayer

Add those and see if it fixes.

0
Why'd you create the "Players" variable only to use game.Players.. Also curr_player isn't intended to reference the localplayer as far as I can tell from the OP. systack 123 — 7y

Answer this question