Need help getting players from server

Hello everyone,

I have a local script running on loop currently which is firing a remote event constantly every few seconds which when received by the server, the server adds a new value to their leaderstats.

And I want to not let people with clients be able to spam the event and get a unfair lead. I also can not put a debounce on it as because the adding value script is the game for everyone if someone else’s event fires while the script is in debounce they wont be able to get the added value, and if I add debounce locally it would be too easy for hackers to delete.

I want to be able to access the players, and their leaderstats via a server script. I have tried to use a table but got no where with it.

Help will be appreciated!

2 Likes
local Players = game:GetService("Players")

task.wait(5)

for _, player in ipairs(Players:GetPlayers()) do
	for _, stat in ipairs(player.leaderstats:GetChildren()) do
		print(player.Name.." has "..stat.Value..stat.Name..".")
	end
end

The task.wait(5) isn’t entirely necessary, it’s just to allow for the leaderstats to be created first, the loop is what you’d need.

3 Likes

I will check it out in just a min, I just have to get something done.

1 Like

One of my other question would be how would be if I am using a table, how can I keep the table updated on what player is currently in game and who has left, because the insert on player added isnt working well for me.

1 Like

Try this

local playersTable = {}

game.Players.PlayerAdded:Connect(function(player)
	table.insert(playersTable, player.Name)
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(playersTable) do
		if v == player.Name then
			table.remove(playersTable, i)
		end
	end
end)
1 Like

I had to made some changed here and there to make it work with my game, but it works well thanks!

Update: Your code was better I just made it complicated, back to your one!

2 Likes

You can just use Players:GetPlayers() or Players:GetChildren(), both will return an array of player instances which you can iterate over, you don’t need to create and manage your own array like in the provided script.

2 Likes
local Players = game:GetService("Players")

local PlayersInGame = Players:GetPlayers() --array of player instances
local PlayersInGame2 = Players:GetChildren() --same thing (slightly different), dont do this if u plan on parenting other instances to the players service
1 Like