Hello! I’m making a game and I wanted to implement weekly reward system for top 50 players on leaderboard. Top 1 player gets reward number 1, Top 25 players gets reward number 2, and Top 50 players gets reward number 3. if player is Top 1 then he only gets reward number one
The problem is that I don’t know how to reward all online players on every server and how to reward all offline players that played before and got in top 50 players.
Script should reward every online player that taken place in leaderboard and if player is offline then script should reward him after he joins game. It should happen after one week. After one week timer ends then it should start new one.
I tried a lot of things but these are the closest ones:
every server saves same top 50 players in one server data store and rewards every online player after 5 seconds or when somebody from top 50 players joins game (but what if one server lagged and saved data 10 seconds later? it would reward every player twice time),
every server rewards only top 50 players that are currently active on this server, if player is on other server then don’t do anything and if player is offline then save him in data store for later (but how to check if player is on other server? and what if server lagged and saved data 10 seconds later?),
If I only had something like one script that runs for every server. I know that there is messaging service, but every server has same timer and every server starts it’s own function that rewards players.
I also don’t ask for entire script. I just want any tips or way to do it. More I think how to make it, it gets more hard
You can use a DataStore and os.time() to record weekly rewards.
To check if a week has passed, simply subtract the previous time to current time then divide the difference by 604800 seconds (one week) and check if it’s equal to or more than one. If it is more than one week, set the previous time to the current time and repeat the cycle.
-- previousTime is a UNIX EPOCH time of when the player was last rewarded
if ((os.time() - previousTime) / 604800) >= 1 then
previousTime += 604800 -- changes previousTime to current time
-- reward the player
end
To reward the player even when offline, you can use DataStores for the rewards, and simply get the UserIds of the top 50 players, then use DataStore:SetAsync() to reward the player.
if ((os.time() - previousTime) / 604800) >= 1 then
previousTime += 604800
-- reward the player
DataStore:SetAsync(userId, points + 1000) -- let points be the data of the player and 1000 the reward
end
Alright, that helped me a lot but there is still a problem. If there are 5 active servers then offline player would get points + 1000 five times We also would need to check if player is online on current server, if player is online but on other server and if player is not playing game at this moment. How can I reward players only one time on 5 running servers? Is it even possible?
Oh right, reward the player when they joined instead.
Players.PlayerAdded:Connect(function(Player)
-- setting up the datastore
-- weekly reward
local weeksTime = 604800
local weeksPassed = math.floor((os.time() - previousTime) / weeksTime)
if weeksPassed >= 1 then
previousTime += weeksTime * weeksPassed
-- reward the player
DataStore:SetAsync(userId, points + 1000) -- let points be the data of the player and 1000 the reward
end
end)