So my goal is to make a gift system like in pet simulator x. That is, you can pick up a reward every certain time. The question is how to do it?
2 Likes
I would suggest making a timer on the server and looping through the players and reward them like so:
RewardScript (Parent: ServerScriptService)
while true do
for i, player in pairs(game.Players:GetChildren()) do
-- Reward the player
player.leaderstats.Cash.Value += 10 -- Your reward, I chose cash in leaderstats but that is adjustable
end
-- Wait 300 seconds (5 minutes)
task.wait(300)
end
1 Like
You can try something like this:
local Players = game:GetService("Players")
local RewardInterval = 5 * 60 -- 5 minutes in seconds
local PlayerPlaytimes = {} -- {player.UserId = seconds_played}
Players.PlayerAdded:Connect(function(player)
PlayerPlaytimes[player.UserId] = 0
-- Timer loop
while player do
wait(1)
PlayerPlaytimes[player.UserId] = PlayerPlaytimes[player.UserId] + 1
if PlayerPlaytimes[player.UserId] >= RewardInterval then
-- Player eligible for reward, reset the timer for the player
PlayerPlaytimes[player.UserId] = 0
player:SetAttribute("CanClaimReward", true)
end
end
end)
Players.PlayerRemoving:Connect(function(player)
PlayerPlaytimes[player.UserId] = nil
end)
You’ll need a function or an event that gives the reward to the player when they choose to claim it:
--// Maybe use remote event?
local function giveReward(player)
if player:GetAttribute("CanClaimReward") then
-- Give the reward to the player here
-- Reset the attribute
player:SetAttribute("CanClaimReward", false)
end
end