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

Can someone help me with this code?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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)
0
I got this error: Workspace.Part.SurfaceGui.Hosts.ListScript:19: '<eof>' expected near 'end' intrance 50 — 9y
0
You should tab your code correctly, then. You have an `end` that's not attached to any block. BlueTaslem 18071 — 9y
Ad
Log in to vote
-1
Answered by
Traide -2
9 years ago

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

0
I got this error: Workspace.Part.SurfaceGui.Hosts.ListScript:18: attempt to index local 'player' (a number value) intrance 50 — 9y
Log in to vote
-1
Answered by
dyler3 1510 Moderation Voter
9 years ago

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

Answer this question