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

FireAllClients not working properly?

Asked by 4 years ago

I'm trying to create a player list, which it is working but only showing the client in the list. I'm using the FireAllClients() event which I think is supposed to fire all of the clients in the game(correct me if i'm wrong). It doesn't seem to be working though. Any help? Here is the script.

---Server Script
---Server side events
game:GetService("Players").PlayerAdded:Connect(function(p)
    game.ReplicatedStorage.Events.AddPlayerToVotingList:FireAllClients()
end)
---
game:GetService("Players").PlayerRemoving:Connect(function(p)
    game.ReplicatedStorage.Events.RemovePlayerFromVotingList:FireAllClients()
end)
---Local Script
---Wait for game to load
wait()
---Variables
local playerservice = game:GetService("Players")
local players = playerservice:GetPlayers()
local frame = script.Parent.ScrollingFrame
local temp = frame.Template
local player_table = {}
---Functions
function AddPlayer()
    print("Player added!")
    for i = 1,#players do
        if players[i] then
            if not player_table[players[i].Name] then
                table.insert(player_table,players[i])
                temp.Name = players[1].Name
                temp.Text = players[1].Name
                if i > 1 then
                    local tempclone = temp:Clone()
                    tempclone.Name = players[i].Name
                    tempclone.Text = tempclone.Name
                    return tempclone
                end
            end
        end
    end
end
---
function RemovePlayer(player)
    print("Player removing!")
    for i = 1,#player_table do
        if player_table[i] then
            table.remove(player_table,player_table[i].Name)
            local temp = AddPlayer()
            if temp then
                temp:Destroy()
            end
        end
    end
end
---Events
game.ReplicatedStorage.Events.RemovePlayerFromVotingList.OnClientEvent:Connect(RemovePlayer)
---
game.ReplicatedStorage.Events.AddPlayerToVotingList.OnClientEvent:Connect(AddPlayer)
0
What do you mean only showing "the client" in the list? legoguy939 418 — 4y
0
Let me guess here: a GUI is client-based. So you mean that the GUI only shows the list of players as the current player viewing the gui. For example if I were connected it shows "legoguy939" as the only player even though there are other players connected? legoguy939 418 — 4y
0
Yes, that's exactly what i mean't ScriptToon 70 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Okay, so I can only assume your problem is this

---Variables
local playerservice = game:GetService("Players")
local players = playerservice:GetPlayers() -- here, you define ONE table as GetPlayers. This would only run once. The function would not update it. Also it would return nil if anyone left. 
local frame = script.Parent.ScrollingFrame
local temp = frame.Template
local player_table = {}
---Functions
function AddPlayer()
local players = playerservice:GetPlayers() -- redefine your variable inside the function so the table is re-created every time a player joins. You want the table to fire with the function.
    print("Player added!")
    for i = 1,#players do
        if players[i] then
            if not player_table[players[i].Name] then
                table.insert(player_table,players[i])
                temp.Name = players[1].Name
                temp.Text = players[1].Name
                if i > 1 then
                    local tempclone = temp:Clone()
                    tempclone.Name = players[i].Name
                    tempclone.Text = tempclone.Name
                    return tempclone
                end
            end
        end
    end
end

If this doesn't fix it let me know. I'm happy to help. I just don't understand what you mean by "only shows the client"

I recommend that you don't use :GetPlayers though. Its a lot easier to go to the server script and define player_table as you did in your local script, and then every time a player joins you do table.insert(player_table, p) and then fire the table to all clients.

I programmed up a player list on a surface gui. Ill put the code so you can see what I mean. However, it is quite annoying since it doesn't replace the blank space when a player's name gets deleted. But I did it for the sake of understanding your problem.

-- Server script
local event = game:GetService("ReplicatedStorage").PlayerEvent
CurrentPlayers = {} -- here I defined the table 
game.Players.PlayerAdded:connect(function(p)
    joined = true -- keep in mind this joined variable, as i use it in the local script
table.insert(CurrentPlayers,(#CurrentPlayers+1), p.Name) -- whenever a player joins I add him/her to the player table
    event:FireAllClients(p, CurrentPlayers, joined) -- here I'm firing over all my variables I want to use 

end)

game.Players.PlayerRemoving:connect(function(p)
    joined = false
    local x = table.find(CurrentPlayers, p.Name)  -- whenever a player leaves I search for his/her name in the array. 
    table.remove(CurrentPlayers, x) -- it shouldn't return nil, but it can, so just use an if statement to fix that. I didn't, but here I am removing the player's name from the table. 
    event:FireAllClients(p, CurrentPlayers, joined) -- firing to all clients with the variables
end)
--end of script



--local script (I put it in starter player scripts)
local event = game:GetService("ReplicatedStorage").PlayerEvent
event.OnClientEvent:connect(function(p, CurrentPlayers, joined) 
if joined == true then -- there's two ways we can get an OnClientEvent. One from a player joining, one from leaving. If they joined then we do below.
for i = 1,#CurrentPlayers do 
if game.Workspace.partlist.listgui.list:FindFirstChild(CurrentPlayers[i])== nil then -- this just checks to see if there's already a GUI displaying
    -- a player's name already. Meaning I don't want doubles every time a player joins. I just want one GUI per player. That was a bug, this is my fix.
local new = game:GetService("ReplicatedStorage").TextLabel:Clone() 
new.Text = CurrentPlayers[i] 
new.Name = CurrentPlayers[i] -- I name the TextBox the player's name so its easy to find. And of course since I'm not using GetPlayers I won't need .Name because I inserted the string of player.name into the table
new.Position = UDim2.new(0,0,0,(i*50))
new.Parent = game.Workspace.partlist.listgui.list
end
end
    elseif joined == false then -- when joined is false I just get rid of the player's name on the list. I could fix everything so the list rebalances everytime a player leaves. 
        -- to do so I'd probably end up deleting everything and instantly replacing with new(like above)
    local thing = game.Workspace.partlist.listgui.list:FindFirstChild(p.Name):Destroy()

end
end)

Ad

Answer this question