Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why can't I find the player from String?

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.

1 comment

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?

2 answers

✓ Accepted answer

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)

1 comment

0

You can use the generic for instead of having to go through that whole process.

local Players = game:GetService("Players")
local curr_player = game.Players.LocalPlayer

Add those and see if it fixes.

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.