You can write your topic however you want, but you need to answer these questions:
What I want to achieve! I want a daily reward system where they get 1,000 coins on touch or in the circle area. and they can only collect the reward after 24 hours
The issue! Only one person can get it in the server no one else is able to collect their daily reward, and if someone joins a new server they can still collect their daily reward within the same day.
--Made by ExoticEclipez--
currency = "Coins" -- Change Gold to the currency you want to give
amnt = 1000 -- Change 50 to the amount of money you want to give
debounce = false -- Don't change this
function onTouch(hit)
if hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == false then
if game.Players:findFirstChild(hit.Parent.Name) ~= nil then
ThisPlayer = game.Players:findFirstChild(hit.Parent.Name)
ThisPlayer.leaderstats:findFirstChild(currency).Value = ThisPlayer.leaderstats:findFirstChild(currency).Value + amnt
script.Parent.Transparency = 1
script.Parent.CanCollide = false
debounce = true
wait(86400) -- Respawn time for the cash giver
script.Parent.Transparency = 0
script.Parent.CanCollide = true
debounce = false
end
end
end
script.Parent.Touched:connect(onTouch)
Please help me with this problem it has been a strugle for many hours!
Unfortunately, and I donât intend to sound rude here, but this logic is entirely wrong.
Hereâs why-
Youâre attempting to wait a whole day in a single server. When the server shuts down, this code will reset and other players who has redeemed a reward recently can potentially redeem it again.
You would need to use os.time(), and datastores, for this system. Datastores are for saving existing data, so you can always be sure when a player needs to be rewarded again, and os.time(), for recording time difference between their last visit.
This is by all means not how to go about what youâre trying to achieve, even though at first glance the code you wrote may seem to make sense. This is being run on a server scriptâ one script which is affecting everyone in the game. Once a player touches the brick, they make it so the script waits a full day until ANYONE can touch the brick and get their reward. However, this in and of itself is faulty since people can just create a new server to get their reward again without needing to wait a day. Instead, youâll have to tackle this code on a player-to-player basis which replicated across all servers, current and yet to be created. You can do this with a simple data store (be sure to read about them on the developer hub) which saves a tick() for when the player last received their award. Then, when the player rejoins at any time, the script will check if the current tick() is more than 1 day larger than the playerâs saved tick(). If so, then the script will award the player with their prize and update the data store with the current tick(). Surely the last part of this message was confusing if you donât know about tick() ir datastores, so be sure to read about them to be able to make this system work for you.
No worries, itâs definitely going to be a bit âmore advancedâ than the code you wrote above. The most useful resources for learning to write this code will be the developer hubâs pages on DataStoreService, Players.PlayerAdded event (just make sure to understand events in general), and tick().
and i already have a Data store system to save players data but the only problem is only one player can collect the daily reward and they can recollect it by rejoining a new server.
Right, but itâs definitely clear that your data store doesnât have to do with what is being talked about in your above script. If you were to implement a data store, however, it would be replicated across all servers, current or not-yet-created, so there would be no ability to join new servers to bypass the day waiting period.
Save os.time() to a DataStore for a player and upon the player joining, compare whether 24 hours have elapsed, if they have then save the current os.time() value to the DataStore again.
You could even use what some other games do, some sort of platform when touched, or a Gui button that when activated, runs the algorithm and detects whether 24hrs have passed- this prevents looping infinitely to check whether 24hrs elapsed while in game.
Building off my original post, hereâs some pseudocode that could do the trick for you:
Untested.
local ds = game:GetService("DataStoreService"):GetDataStore("DailyReward")
local stat_name = "Coins" --//Name of your leaderstat
local amount = 1000 --//Amount of X resource to give each daily reward
game.Players.PlayerAdded:Connect(function(player)
local recorded_time
pcall(function() recorded_time = ds:GetAsync(player.UserId) end)
if recorded_time and (os.time() - recorded_time >= 86400) then --//See if player is eligible for a reward
ds:SetAsync(player.UserId, os.time()) --//Reset timer
local leaderstats = player:WaitForChild("leaderstats") --//Wait for leaderstats to be made
local stat = leaderstats:FindFirstChild(stat)
if stat then
stat.Value = stat.Value + amount --//Give reward to player
else
warn(stat.." does not exist in leaderstats")
end
elseif not recorded_time then
ds:SetAsync(player.UserId, os.time()) --//Reset timer
end
end)
The code you have sent me has a error the stat that comes after leaderstats:FindFirstChild has a orange underline could you tell me whats wrong @C_Sharper by the way your user is brilliant
Thatâs because it was explicitly marked as âpseudocodeâ and youâll need some alterations to suit it to your game, the categoryâs not for entirely functional code to be handed to you.
Itâs likely they meant to do
local stat = leaderstats:FindFirstChild(stat_name)
instead of stats, stat would be undefined otherwise