Creating daily reward with Data Store 2

Hello, I’m having some issues with creating a daily chest using Data Store 2. I went to other posts, but all of them use regular data store and they use :SetAsync() or :GetAsync() which is really throwing me off because I don’t know how to convert that to Data Store 2. The whole idea of os.time() is really confusing as well and when I tried it, it worked… kinda, but the timer didn’t countdown when the player was outside of the game. I don’t have code to show as it’s very messy and I’m just so lost on what to write. If someone could explain what to do or show an example of it using Data Store 2 that would be helpful.

what did you say? it says (post deleted by author) for some reason

I wanted one of the scripts that you found so i can change it so it can be used with Datastore2, and you didnt reply so i thought you wanted someone to make a fully new one so deleted it.

oh no I was showering rq I was just waiting for someone to reply

1 Like

Here is a script that someone made. It’s not really a script, but it explains what to do. I was confused what he was saying but here it is

local datastore2 -- datastore2 location here

game.Players.PlayerAdded:Connect(function(player)
    local rewardDataStore = datastore2("DailyReward", player)
    -- Check when the player has collected the reward using os.time()
    -- I personally forgot how to use datastore2 because I don't work with datastores that much
    -- But save the time when the player collected the reward, like a remote event
    -- Save it in datastore2 as os.time() when the player collects it
    -- I never made daily rewards in my entire life
end)

I meant the daily reward script, i know how to use Datastore2, i was going to change to code so its “compatible” with Datastore2

game.Players.PlayerAdded:Connect(function(player)
	if timeDataStore:GetAsync(player.UserId.."-Daily") then
		goalTime = timeDataStore:GetAsync(player.UserId.."-Daily")
		claimed = true
	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)
					else
						claimed = false
						goalTime = nil
						timeDataStore:RemoveAsync(player.UserId.."-Daily")
						script.Parent.Parent.Treasure.Timer.TextLabel.Text = ("Chest Ready!")
					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

dont worry abt the touched event

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)