You can write your topic however you want, but you need to answer these questions:
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)
What is the issue? Include screenshots / videos if possible!
I can’t code it my code won’t work
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.
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)
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)
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)
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)
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!