Daily Reward does not work with everyone in the game!

You can write your topic however you want, but you need to answer these questions:

  1. 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

  2. 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! :slight_smile:

3 Likes

It’s a serverside debounce, so only 1 person in the server can activate the touch event every 86,400 seconds.

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.

4 Likes

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.

3 Likes

Thank you very much but i dont know how to write the code your claiming im a beginner :frowning:

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.

1 Like

Ok i will review those forums thankyou!

1 Like

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.

Your current implementation is a bit flawed.

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.

related post

@Chatowillwin

Also in these cases, use os.time() over tick() as the latter extends to milliseconds which is not something you necessarily need to save.

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)
2 Likes

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

1 Like

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

2 Likes