Problem with my table script

Hello everyone,

So I made this script which basically when you click the button it will add your name to a table then print it (there will be more to the script just made this for now). For some reason it seems to not be printing peoples names out.

local JoinList = script.Parent

local namelist = {}

local function addToList(player)

table.insert(namelist, player)

end

local function printList()

for i,player in pairs(namelist) do

print("#"..tostring(i).."\t"..player.Name)

end

end

JoinList.MouseButton1Click:Connect(function(plr)

print("ButtonJoinClicked")

addToList()

wait(0.5)

printList()

end)

You’re running this locally, from what I gather, so the table isn’t synced for every player. You need to fire a remote event when a player clicks on the button and handle inserting/managing the table on the server only.

In your current implementation, your table will have at most one unique element which would be the local player

addtolist(plr)
printlist(plr)

you left the inputs the blank

2 Likes

That is a server script not a local one.

.MouseButton1Click is an event that can only be used locally which is why I assumed this was a local script.

If you need to detect when the player clicks the button, you need to make the GUI a child of the PlayerGui (if it’s a billboardUI, you can adjust the adornee to be a part on the workspace).

well, for one, “addToList” requires an argument which you are not passing (the player).
Secondly, you said this is a server script. You should really not be handling UI on server scripts.

I tried a local script though and due to it being a surface UI button it seems not to work.

Again, change the Adornee, but parent the GUI to the player’s PlayerGui. That’s the only way to make the UI elements be interactable for the player.

local System = {
    Function = {},
    List = {},
    JoinButton = script.Parent
}

function System.Function.AddToList(player)
 if not player then 
  return
 end
 table.insert(System.List,1,player)
end

function System.Function.Print()
   for i,plr in pairs(System.List) do
      if plr then
        print(plr.Name) 
      end
   end
end

JoinButton.MouseButton1Click:Connect(function(player)
 System.Function.AddToList(player)
 wait()
 System.Function.Print()
end)

Dont forget to make sure to tell it what position you want it to be within the table otherwise it wont insert it.

What kind of script is it, what instance triggers this, and where is it?

As an alternative you could try using a ClickDetector and connect your function with ClickDetector.MouseClick because it passes a Player parameter, unlike MouseButton1Click

Also make sure you are specifying a parameter in addToList(), so in your case it would be addToList(plr) in the last function