Attempting to make a daily reward

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

  1. What do you want to achieve? Keep it simple and clear!
    a daily reward. When I click a button it is going to give your daily reward. Like in a simulator basically (MY game isn’t a simulator but this kinda theme)
  2. What is the issue? Include screenshots / videos if possible!
    I can’t code it :frowning: my code won’t work
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    code below
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- You can change the name of the datastore to whatever you want

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.leaderstats.Brix.Value, -- First value from the table
		player.OwnsShovel.Value
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves the game
	saveData(player) -- Save the data
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do
		saveData(player) -- Save the data
	end
end)

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Brix = Instance.new("IntValue")
	Brix.Name = "Brix"
	Brix.Parent = leaderstats
	
	local OwnsShovel = Instance.new("BoolValue")
	OwnsShovel.Name = "OwnsShovel"
	OwnsShovel.Parent = player

	local data
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId)

	end)

	if success and data then
    Brix.Value = data[1]
    OwnsShovel.Value = data[2]
	else
		print("The player has no data!") -- The default will be set to 0	
	end

	end)




while task.wait(1) do
	for _, player in pairs(game.Players:GetPlayers()) do
		player.leaderstats.Brix.Value += 1 -- Give the player 1 Brix every second
	end
end


Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

That is the code, this is my daily reward code however I want it to give it to you when you click a button on topbar plus

  local DataStoreService = game:GetService("DataStoreService")

local DailyRewardDataStore = DataStoreService:GetDataStore("DailyReward")


game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local foundData = DailyRewardDataStore:GetAsync(player.UserId)
		if foundData then
			if tonumber(foundData) < os.time() - 86400 then
				print(player.Name.." is receiving their daily reward")

				-- Do your daily reward script!

				DailyRewardDataStore:SetAsync(player.UserId,os.time())
			end
		else
			DailyRewardDataStore:SetAsync(player.UserId,os.time())
		end
	end)
end)

Use a Remotevent when you click on a button and fire the event from client to server:

Button.MouseButton1Click:Connect(function()
	Event:FireServer()
end)

And from the server handle the rest of the code like this:

local DataStoreService = game:GetService("DataStoreService")

local DailyRewardDataStore = DataStoreService:GetDataStore("DailyReward")


Event.OnServerEvent:Connect(function(player)
	pcall(function()
		local foundData = DailyRewardDataStore:GetAsync(player.UserId)
		if foundData then
			if tonumber(foundData) < os.time() - 86400 then
				print(player.Name.." is receiving their daily reward")

				-- Do your daily reward script!

				DailyRewardDataStore:SetAsync(player.UserId,os.time())
			end
		else
			DailyRewardDataStore:SetAsync(player.UserId,os.time())
		end
	end)
end)
2 Likes

Btw just so you know (In case someone makes a post similar)

Topbar plus would use

Icon.new()
 :bindEvent("selected", function(icon)
 -- code
end)

(it is a module script system, that is why it doesn’t use luau syntax)

1 Like

shouldn’t it be OnServerEvent? Idk I may be wrong, but in case the person doesn’t know how event recievers should be named.

Yes, it should be. my mistake for that

1 Like

Sorry for late reply, but how would I track how long the player has left until next daily reward?

In case I don’t know how they should be named or him?

I don’t know what you are refering to, his code is right i think?

you can calculate the difference between the current time and the time stored in the DataStore:

if tonumber(foundData) < os.time() - 86400 then

and you save from here:

DailyRewardDataStore:SetAsync(player.UserId,os.time())

How would I put this into Hrs:Mins:Secs format? Sorry for asking so many questions.

I am trying to make text that says how long you have left

You would need this function:

local function toHMS(s)
	return ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
end

-- That's how you call it:
toHMS(100) -- seconds

I am struggling with putting this into the script D:

You would need to fire a RemoteEvent to the Client with the FoundData and then from client use the function on the sent data:

EventForHMS:FireClient(Player,tonumber(foundData))

Now from client:

EventForHMS.OnClientEvent:Connect(function(foundData)
	print(toHMS(100)) -- if you have textlabel do it like this: TextLabel.Text = toHMS(100)
end)

forgot about client-server runtime… ok ty!

1 Like
game.ReplicatedStorage.EventForHMS.OnClientEvent:Connect(function(foundData)
	local function toHMS(s)
		return ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
	end
	
script.Parent.TextLabel.Text = toHMS(100)

end)

sorry for SUPER late reply but it won’t work

you should replace the (100) with the foundData:

game.ReplicatedStorage.EventForHMS.OnClientEvent:Connect(function(foundData)
	local function toHMS(s)
		return ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
	end
	
script.Parent.TextLabel.Text = toHMS(foundData)

end)

Text doesn’t change.
image

Are you firing the event from server to client?

Yes. It is client-server for the daily reward (topbar+ to daily reward script)

THEN server-client (daily reward - text label)

--[[

> If you'd like to receive automatic updates make sure to enable
`AutoUpdate` within PackageLink (inside the Icon).

> I recommend enabling auto updates as the application constantly
receives bug fixes, improvements, etc.

> All updates (within this version of TopbarPlus) will be backwards
compatible so you don't need to worry about updates interfering
with your code.

> Try not to modify any code within Icon. For instance, don't accept
"This script is part of a package. Click here to modify." when
viewing the source code otherwise it will break the package link.
If you want to add your own themes I recommend cloning the `Default`
module outside of the package then requiring and applying that.

> DISCLAIMER: as of 27th March 2024 public packages haven't been fully
rolled out so you won't be able to retrieve the latest version
for the time being.

> READ_ME is Script with RunContext set to 'Client' meaning you can
store it in ReplicatedStorage and Workspace and it will still run 
like a normal LocalScript. DO NOT PLACE place in StarterPlayerScripts
(because this is a Script with RunContext). You need to create a separate
LocalScript for anything under StarterPlayerScripts. 

> You're welcome to move `Icon` and require it yourself. You can
then delete this folder and READ_ME.

> Have feedback? Post it to ``devforum.roblox.com/t/topbarplus/1017485``
which I actively monitor. Enjoy! ~ForeverHD

--]]
local container  = script.Parent
local Icon = require(container.Icon)
local dropdown = {
	Icon.new()
	:setLabel("Show/Hide Country Flag (For me)")
	:bindEvent("selected", function(icon)
			game.Players.LocalPlayer.Character:WaitForChild("Head"):FindFirstChild(game.Players.LocalPlayer.Name.."CountryBillboardGUI").ImageLabel.Visible = false
	end)
	:bindEvent("deselected", function(icon)
			game.Players.LocalPlayer.Character:WaitForChild("Head"):FindFirstChild(game.Players.LocalPlayer.Name.."CountryBillboardGUI").ImageLabel.Visible = true
	end),

	Icon.new()
		:setLabel("Show/Hide Country Flag (For everyone)")
		:bindEvent("selected", function(icon)
		 game:GetService("ReplicatedStorage").HideCountryFlag:FireServer()
		end)
		:bindEvent("deselected", function(icon)
			game:GetService("ReplicatedStorage").ShowCountryFlag:FireServer()
		end)
	
	
}




Icon.new()
	:setLabel("Settings")
	:setImage(16086868244, "Deselected")
	:setImage(16086868447, "Selected")
    :setDropdown(dropdown)
Icon.new()
	:setLabel("Summon bricks!")
	:bindEvent("selected", function(icon)
		game.Players.LocalPlayer.PlayerGui.Help.BetterUI.Visible = true
	end)
	:bindEvent("deselected", function(icon)
		game.Players.LocalPlayer.PlayerGui.Help.BetterUI.Visible = false
	end)
Icon.new()
	:setLabel("Shop")
	:bindEvent("selected", function(icon)
		game.Players.LocalPlayer.PlayerGui.Menu.ScrollingFrame.Visible = true
	end)
	:bindEvent("deselected", function(icon)
		game.Players.LocalPlayer.PlayerGui.Menu.ScrollingFrame.Visible = false
	end)

Icon.new()
	:setLabel("Claim Daily Reward!")
	:bindEvent("selected", function(icon)
		game.ReplicatedStorage.DailyReward:FireServer()
	end)


ServerSide

local DataStoreService = game:GetService("DataStoreService")

local DailyRewardDataStore = DataStoreService:GetDataStore("DailyReward")

game.ReplicatedStorage.DailyReward.OnServerEvent:Connect(function(player)
	pcall(function()
		local foundData = DailyRewardDataStore:GetAsync(player.UserId)
		if foundData then
			if tonumber(foundData) < os.time() - 86400 then
				print(player.Name.." is receiving their daily reward")

				player.leaderstats.Brix.Value += 100

				DailyRewardDataStore:SetAsync(player.UserId,os.time())
			else
				print(player.Name.."Is trying to claim their reward early!")
	
				 -- seconds
			end
		else
			print(player.Name.." is receiving their first daily reward")
			player.leaderstats.Brix.Value += 100
			DailyRewardDataStore:SetAsync(player.UserId,os.time())
			game.ReplicatedStorage.EventForHMS:FireClient(player,tonumber(foundData))

		end
	end)
end)

Client Side again

game.ReplicatedStorage.EventForHMS.OnClientEvent:Connect(function(foundData)
	local function toHMS(s)
		return ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
	end
	
script.Parent.TextLabel.Text = toHMS(foundData)

end)

That’s all!
ty for your help btw, really appreciated!

1 Like

Made it work with this code

function convertToHMS(s)
	local minutes = math.floor(s / 60)
	local seconds = s % 60
	local milliseconds = (seconds * 10) % 10
	seconds = math.floor(seconds)
	milliseconds = tostring(milliseconds)

	milliseconds = tonumber(milliseconds)
	return string.format("%02i:%02i.%01i", minutes, seconds, milliseconds)
end

needs some tinkering with so I won’t mark as solution yet