Creating daily reward with Data Store 2

I had made a very efficient code, if you want I can show you ! :grinning:

Yeah this code above sucks, its not mine but yes pls show me lol

I changed it, i didnt change the touched event as you said.

local datastore2 -- datastore2 location

local claimed
local goalTime

game.Players.PlayerAdded:Connect(function(player)
	local rewardDataStore = datastore2("DailyReward", player)
	goalTime = rewardDataStore:Get()
	claimed = true
	if goalTime == 0 then claimed = false end
	
	spawn(function(timer)
		while true do
			wait(1)
			local difference
			if claimed == true then
				if goalTime ~= nil then
					if os.time() < goalTime then
						difference = goalTime - os.time()
						local hours = math.floor(difference/3600)
						local minutes = math.floor((difference - (hours*3600))/60)
						local seconds = ((difference - (hours * 3600)) - (minutes * 60))
						if hours < 10 then
							hours = 0 .. hours
						end
						if minutes < 10 then
							minutes = 0 .. minutes
						end
						if seconds < 10 then
							seconds = 0 .. seconds
						end
						script.Parent.Parent.Treasure.Timer.TextLabel.Text = (hours..":"..minutes..":"..seconds)
					end
				end
			end
		end
	end)
end)

script.Parent.Touched:Connect(function(touched)
	if touched.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:FindFirstChild(touched.Parent.Name)
		if db == false then
			db = true
			if timeDataStore:GetAsync(player.UserId.."-Daily") == nil then
				timeDataStore:SetAsync(player.UserId.."-Daily", (os.time() + waitTimeHours * 3600))
				goalTime = timeDataStore:GetAsync(player.UserId.."-Daily")
				claimed = true
			end
			wait(5)
			db = false
		end
	end
end)
1 Like

Is there a way to implement the touched event with Zone +. That’s why I didn’t want to mess with that part

Zone+ like that module? I have it but sadly i didnt have the time to use it so i cant help with that.

yeah it’s basically like .touched but the function is different. For ex: Zone.playerEntered:Connect(function(plr)
print(plr.Name)
end)

Ik about it but not implementing it, if you can give me the “base code” and i need to do the Datastore2 part I would be down.

Wait some time I’m scripting ! :grinning:

It’s good I’ve finish :

local DataStoreService = game:GetService("DataStoreService")
local DataStoreDailyReward = DataStoreService:GetDataStore("DailyReward")
local Players = game.Players

Players.PlayerAdded:Connect(function(Player)
	if DataStoreDailyReward:GetAsync(Player.UserId) == nil then
		print("Can receive daily reward")
		DataStoreDailyReward:SetAsync(Player.UserId, os.time())
	elseif os.difftime(DataStoreDailyReward:GetAsync(Player.UserId), os.time()) >= 60*60*24 then
		print("Can receive daily reward")
		DataStoreDailyReward:SetAsync(Player.UserId, os.time())
	else
		print("Can't receive daily reward")
	end
end)

I hope that was helpful ! :yum:

Read the post (Creating daily reward with Data Store 2)

1 Like

Oh sorry, I made a mistake, so I can’t help you sorry ! :sweat_smile:
Have a nice day ! :yum:

I was thinking smth like this

DailyChestZone.playerEntered:Connect(function(plr)
	local rewardDataStore = DS2("DailyRewardTimer", plr)
	
	if rewardDataStore:Get() == 0 then
		workspace.Buildings.Spawn.ChestCircle.RewardHolder.SurfaceGui.TextLabel.Text = "Collect Reward!"
	end
end)

Correct! Although when you collect it do Daily reward:Set(10) change 10 to the time you need to wait before collecting and change this

datastore2("DailyReward", player) 

Ok last thing is I don’t know how to make it countdown for offline players. Like say it has 5 hours left and I left the game, then came back 5 hours later and it says “Collect Reward”

I got everything else to work btw thanks :slight_smile:

1 Like

Isn’t that script supposed to do it? I think a while while true do loop will coneven if he left.

no for some reason it doesn’t work.

I dont quit know about these type of scripting, im quite new aswell so ya.

is it maybe bc Im in studio? or should it work when Im in studio

Maybe thats it, try in a real server