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

Can someone help me with this code?

Alright here is the code:

local frame = script.Parent
local num = frame.Players
local template = frame.Template

for i, v in pairs(game.Players:GetPlayers()) do
    if v.Character then
         player1 = v
    end
end

function Joining(player)
	num.Value = num.Value + 1
	local slotnum = num.Value
	local slot = template:Clone()
	slot.Parent = frame
	slot.Name = "Slot"..slotnum
	slot.Position = UDim2.new(0,0,0,55*num.Value)
	slot.Text = player1.Name
	slot.Visible = true
end

function Leaving(player)
	for i,label in pairs(frame:GetChildren()) do
		if label then
			if string.find(label.Name,"Slot") then
				if label.Text == player.Name then
					label:Destroy()
					num.Value = num.Value - 1
				else
				end
			end
		end
	end
end

script.Parent.Parent.Submit.MouseButton1Down:connect(Joining)
game.Players.PlayerRemoving:connect(Leaving)

And I am getting this error: Workspace.Part.SurfaceGui.Hosts.ListScript:18: attempt to index global 'player1' (a nil value)

I just want it to print the player's name onto the surfacegui.

3 answers

This code doesn't make any sense.

  • The for loop at the beginning accomplishes nothing
  • Joining is attached to when you click a button rather than when anyone joins

A sketch

It appears that you want to make a list of players, each with their own GUI frame. In that case...

  • A player joining creates a new GUI
  • A player leaving deletes a GUI
    • It may be that this is not the bottom player -- but you don't want to leave a gap.
  • There's no reason to use global variables like num -- at best you are storing a list that you can guess the number from

Because of the last detail, we need to maintain a full list of GUIs paired with players. But it's not too complicated.

Update with a list

The simplest solution is to be able to "update" based on the player list:

function update(playerList)
	frame:ClearAllChildren()
	for i, player in pairs(playerList) do
		local slot = template:Clone()
		slot.Parent = frame
		slot.Position = UDim2.new(0, 0, i * 55)
		slot.Text = player.Name
		slot.Visible = true
	end
end

local players = {} -- List of players currently in the game
game.Players.ChildAdded:connect(player)
	-- For stupid reasons, you cannot use PlayerAdded from a LocalScript
	if player:IsA("Player") then
		table.insert(players, player)
		update(players)
	end
end

game.Players.ChildRemoving:connect(player)
	-- For stupid reasons, you cannot use PlayerRemoving from a LocalScript
	if player:IsA("Player") then
		for i = 1, #players do
			if players[i] == player then
				table.remove(players, i)
			end
		end
		update(players)
	end
end

Using ROBLOX's list

Note that if you don't mind the list re-arranging itself (though this is unlikely anyway), you can eliminate even more:

-- just use :GetPlayers() instead of having a parameter?
function update()
	frame:ClearAllChildren()
	for i, player in pairs( game.Players:GetPlayers() ) do
		local slot = template:Clone()
		slot.Parent = frame
		slot.Position = UDim2.new(0, 0, i * 55)
		slot.Text = player.Name
		slot.Visible = true
	end
end

game.Players.ChildAdded:connect(update)
game.Players.ChildRemoving:connect(update)

2 comments

0

I got this error: Workspace.Part.SurfaceGui.Hosts.ListScript:19: '' expected near 'end'

0

You should tab your code correctly, then. You have an end that's not attached to any block.

Instead of using 'Player1' because the number value next to the variable will confuse the script. Remove the 1 then retest it.

1 comment

0

I got this error: Workspace.Part.SurfaceGui.Hosts.ListScript:18: attempt to index local 'player' (a number value)

Just add this to the top of the script to make sure that there are players in the game to work with that have an existant character:

repeat wait() until game.Players.NumPlayer>=1 and game.Players:GetChildren()[1].Character

All this does is wait until the player has a character that the game is able to work with.


So the script all together should look like this:

repeat wait() until game.Players.NumPlayer>=1 and game.Players:GetChildren()[1].Character

local frame = script.Parent
local num = frame.Players
local template = frame.Template

for i, v in pairs(game.Players:GetPlayers()) do
    if v.Character then
         player1 = v
    end
end

function Joining(player)
    num.Value = num.Value + 1
    local slotnum = num.Value
    local slot = template:Clone()
    slot.Parent = frame
    slot.Name = "Slot"..slotnum
    slot.Position = UDim2.new(0,0,0,55*num.Value)
    slot.Text = player1.Name
    slot.Visible = true
end

function Leaving(player)
    for i,label in pairs(frame:GetChildren()) do
        if label then
            if string.find(label.Name,"Slot") then
                if label.Text == player.Name then
                    label:Destroy()
                    num.Value = num.Value - 1
                else
                end
            end
        end
    end
end

script.Parent.Parent.Submit.MouseButton1Down:connect(Joining)
game.Players.PlayerRemoving:connect(Leaving)

Anyways, hope this helped. If you have any further questions/problems, please leave a comment below :P

####-Dyler3

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