So I have a serverScript with a cooldown of 60 seconds that gives data to players and saves them and reloads a Leaderboard in Workspace.
Everything works, however this script gets spammed and it loads for each player in-game. As what the console shows, there are currently 4 users in game, so it shows a print for each player. This affects the Leaderboard I have and the Contents inside get’s multiplied sometimes by 2 or 3 depending on how many Users are in-game.
this is my ServerScript (Just to let you know, I have the PlayerService removed before as well and it still reloads for every single player in-game), so any alternatives?
game:GetService("Players").PlayerAdded:Connect(function(plr)
local CoolDown = 55
while wait(CoolDown) do
print("Data Refreshing.")
GROUPDONO_DATA.ReloadLeaderboard()
PLYR_HANDLER.IncrementTime(plr)
PLYR_HANDLER.SaveData({plr})
wait(5)
PLYR_HANDLER.sendData(plr)
LEADERBOARD_DATA.setUpLeaderboard()
print("Data Refreshed.")
end
end)
I would first want to start by seperating both updating the players data and the leaderboard, this is so we can easily found where the issue is, cause the issue could be in a different script all together.
ended up just making new functions and a few more prints for debugging
local function player(plr)
local playerDataCooldown = 55
while wait(playerDataCooldown) do
print("Refreshing data for player:", plr.Name)
PLYR_HANDLER.IncrementTime(plr)
PLYR_HANDLER.SaveData({plr})
wait(5) print("5 second cooldown finished.")
PLYR_HANDLER.sendData(plr)
print("Data refreshed for player:", plr.Name)
end
end
local function leaderboard()
local leaderboardCooldown = 60
while wait(leaderboardCooldown) do
print("Refreshing leaderboard.")
GROUPDONO_DATA.ReloadLeaderboard()
LEADERBOARD_DATA.setUpLeaderboard()
print("Leaderboard refreshed.")
end
end
game:GetService("Players").PlayerAdded:Connect(function(plr)
spawn(function()
print("Player found: ", plr.Name)
player(plr)
end)
end)
-- Start updating the leaderboard independently
spawn(leaderboard)
If player added isn’t a good option for it then maybe resort to
for _, plr in ipairs(game:GetService("Players"):GetPlayers()) do
it wasnt really for solving the issue or making it more efficient, it was just to help in some cases. its because im unsure of what you are actually asking for
Hello, This is my attempt on fixing your issue of it printing for each player in the game, and some general optimization. If you have any questions, feel free to ask me.
--|< Configuration >|--
local coolDown = 60;
--|< Functions >|--
local function refreshData()
print("Data Refreshing.");
GROUPDONO_DATA.ReloadLeaderboard();
for _, player in ipairs(game.Players:GetPlayers()) do
PLYR_HANDLER.IncrementTime(player);
PLYR_HANDLER.SaveData({player});
PLYR_HANDLER.sendData(player);
end
LEADERBOARD_DATA.setUpLeaderboard();
print("Data Refreshed.");
end
--|< Main >|--
while task.wait(coolDown) do
refreshData();
end
local players = game:GetService("Players")
local playerDataCooldown = 55
local leaderboardCooldown = 60
local refreshingPlayers = false
local playerQueue = {} -- list
local function refreshPlayer()
refreshingPlayers = true
while #playerQueue > 0 do -- continue while there are players in the queue
local plr = table.remove(playerQueue, 1) -- get the first player from the list
if plr and plr.Parent == players then -- check if player is still in the game
print("Refreshing data for player:", plr.Name)
PLYR_HANDLER.IncrementTime(plr)
PLYR_HANDLER.SaveData({plr})
wait(5) print("5 second cooldown finished.")
PLYR_HANDLER.sendData(plr)
print("Data refreshed for player:", plr.Name)
end
-- cooldown time distributed among all players
wait(playerDataCooldown / #players:GetPlayers())
end
refreshingPlayers = false
end
-- add a player to the list and start refreshing if not already running
local function addToList(plr)
table.insert(playerQueue, plr)
if not refreshingPlayers then
spawn(refreshPlayer)
end
end
-- function to refresh the leaderboard periodically
local function refreshLeaderboard()
while true do
print("Refreshing leaderboard.")
GROUPDONO_DATA.ReloadLeaderboard()
LEADERBOARD_DATA.setUpLeaderboard()
print("Leaderboard refreshed.")
wait(leaderboardCooldown)
end
end
players.PlayerAdded:Connect(function(plr)
print("Player found:", plr.Name)
addToList(plr) -- add new player to the list for data refresh
end)
-- players that are already in the game, to be added to the list
for _, plr in ipairs(players:GetPlayers()) do
addToList(plr)
end
spawn(refreshLeaderboard)
im probably going to sleep now, so someone else might have to continue to help