Hello. I’m planning on making a coin-collecting system in my game. I want to make it so the player has to wait 24 hours before the coin respawns. The problem is that I don’t want players to bypass this by just rejoining the game. Is there a way to make a wait time continue while the player isn’t in-game?
I believe I need to use DataStores and os.time for this, but I’m not quite sure how I should add it to my script.
I had pretty much no idea how to script this, but here is my attempt.
waitTime = os.time() + 86400
amount = 1
local player = game.Players.LocalPlayer.UserId
local dataStoreService = game:GetService("DataStoreService")
local timeLeft = dataStoreService:GetDataStore("timeLeft")
local function toHMS(s)
return ("%02i:%02i:%02i"):format(s/3600, s/60%60, s%60)
end
while true do
print(toHMS(waitTime - os.time()))
wait(1)
end
timeLeft:SetAsync(player, waitTime)
function onTouched(part)
local h = part.Parent:FindFirstChild("Humanoid")
if (h~=nil) then
local thisplr = game.Players:FindFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local stats = thisplr:FindFirstChild("leaderstats")
if (stats~=nil) then
local score = stats:FindFirstChild("Coins")
if (score~=nil) then
score.Value = score.Value + amount
end
end
end
script.Parent.Transparency = 1
script.Disabled = true
wait(waitTime)
script.Parent.Transparency = 0
script.Disabled = false
end
end
script.Parent.Touched:Connect(onTouched)
Thank you.