I’m trying to make a playtime reward system where every 10 minutes, you get a reward. However, i can’t seem to find a tutorial on how anywhere. Can anyone explain how i would make one?
For Example:
10 mins after joining: 100 coins
20 mins after joining: 200 coins
30 mins after joining: 300 coins
1 hour after joining: 500 coins
Hi there! To achieve a playtime reward system, here’s an example of what you can do:
local Players = game:GetService("Players")
-- Define your rewards
local Rewards = {
{
Time = 600, -- 600s = 10 minute,
Coins = 100
},
{
Time = 1200, -- 1200s = 20 minute,
Coins = 200
}
}
-- Detect when the player joins the game
game.Players.PlayerAdded:Connect(function(player)
-- Initialize their playtime. Note; I'm using seconds in this example, but feel free to use minutes.
local playerPlaytime = 0
-- Every one second
while true and wait(1) do
-- Add a second to their playtime
playerPlaytime += 1
-- Check if any reward was reached
for i, v in ipairs(Rewards) do
if v.Time == playerPlaytime then
-- Do something with v.Coins
end
end
end
end)
This is just a basic system you could use for the countdown part of it, which would be run client-side.
You would have a server-side Script and a client-side LocalScript connected by a RemoteEvent.
When the player joins, the RemoteEvent is used to tell the client to begin the countdown. When the countdown has finished, the client will use the RemoteEvent to tell the server to add the rewards. Modify this how you want!
Client-side example:
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent") --change to your event
local countdown = 600 --change to your minutes countdown, in seconds
local exampleLabel = nil --change to a text label you want only the client to see and update
event.OnClientEvent:Connect(function()
for i = countdown, 1, -1 do
exampleLabel.Text = i
task.wait(1)
end
event:FireServer()
end)
please note I haven’t implemented any step-up system into this, so it can just be used for the countdown. Hope this helps!
yes, I am aware. RemoteEvents are used to communicate between the server and the client, so they would need to go in the best place that both of them can access.
You have to define your RemoveEvent anywhere in the script, if that’s what you were asking for. Here’s a tiny edited version:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.PATH_TO_YOUR_REMOTE
-- Detect when the player joins the game
game.Players.PlayerAdded:Connect(function(player)
-- Define your rewards, locally this time
local Rewards = {
{
Name = "firstReward",
Time = 600, -- 600s = 10 minute,
Coins = 100,
Reached = false,
Claimed = false,
},
{
ame = "secondReward",
Time = 1200, -- 1200s = 20 minute,
Coins = 200,
Reached = false,
Claimed = false,
}
}
-- Detect when the remote is fired from a client
RemoteEvent.OnServerEvent:Connect(function(remotePlayer, rewardName)
if remotePlayer ~= player then return end
for i, v in ipairs(Rewards) do
if v.Name == rewardName and v.Reached then -- If the reward is found and it is reached
if not v.Claimed then -- Check if it was claimed already
-- Do something with v.Coins
end
end
end
end)
-- Initialize their playtime. Note; I'm using seconds in this example, but feel free to use minutes.
local playerPlaytime = 0
-- Every one second
while true and wait(1) do
-- Add a second to their playtime
playerPlaytime += 1
-- Unlock rewards if playerPlaytime is enough
for i, v in ipairs(Rewards) do
if v.Time == playerPlaytime then
v.Reached = true
break
end
end
end
end)
This is only the server script. You would have to make a local script so whenever the player clicks on a button, it fires the Remote. Example:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.PATH_TO_YOUR_REMOTE
YOUR_BUTTON.MouseButton1Click:Connect(function()
RemoteEvent:FireServer("firstReward")
end)
You’re welcome! Once you learn about it, you’ll see how simple they are to use. Feel free to send me a Direct Message on the DevForum or on Discord (maxencexz) if you need more help / have questions!