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

How would I find all players in game in one message?

Asked by
ACHRONlX 255 Moderation Voter
4 years ago

I have tried creating a table and adding player names into it when they join and removing it when they leave, however that hasn't worked. Can anyone tell me how I would make the webhook print every player in game's name?

local players = {}

local url = ""
local http = game:GetService('HttpService')


game.Players.PlayerAdded:Connect(function(p)
table.insert(players, p.Name)

end)

game.Players.PlayerRemoving:Connect(function(p)
local finder = table.find(players, p.Name)
if finder then
table.remove(players.p.Name)
end
end)
p.Chatted:Connect(function(msg)
if msg == ":players" then

local data = { 
["content"] = players -- I know this won't work, I've tried other methods but they also do nothing.

http:PostAsync(url, http:JSONEncode(data))
end
end)


1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
local playersTable = {}
local http = game:GetService("HttpService")
local url = ""

game.Players.PlayerAdded:Connect(function(player)
    for _,oldPlayer in pairs(game:GetService("Players"):GetPlayers()) do
        if not table.find(playersTable, oldPlayer.Name) then
            table.insert(playersTable, oldPlayer.Name)
        end
    end

    player.Chatted:Connect(function(msg)
        if msg:sub(1,8) == ":players" then
            local jsonTable = http:JSONEncode(playersTable)
            local data = {
            ["content"] = jsonTable
            }

            http:PostAsync(url, http:JSONEncode(data))
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local pos = table.find(playersTable, player.Name)
    table.remove(playersTable, pos)
end)

The code looks messy, so it's hard to tell what the error is exactly.

But, my assumption is that you're trying to use a non-JSON table?

Not 100% sure, just a guess. But, this should work. Hope this helped!

Ad

Answer this question