Button Hide when click and show after Cooldown

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!
    Hide the button when Rewards claim and show it again after the player can claim the reward again.
  2. What is the issue? Include screenshots / videos if possible!

    when the player click the claim reward, it hides the button ,


but when the player leaves and rejoins the game, the button is visible again to that player even though he already claimed the reward for that day, though there is no reward given since it only gives rewards every 24hrs.

below is the structure of my script .
image

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

The only solution I tried is the hide button, but as stated above it only hides when players don’t leave the game, but if he leaves and rejoins the button shows again.

Mainscript

local datastoer = game:GetService("DataStoreService")
local daysstroe = datastoer:GetDataStore("DaysStorage103")
local config = require(script.Parent:WaitForChild("MainConfig"))


game.Players.PlayerAdded:Connect(function(player)
	local daysfolder = Instance.new("Folder", player)
	daysfolder.Name = "Days"

	local days = daysstroe:GetAsync(player.UserId)
	local clone = script.Script:Clone()

	if days then

		for i, v in pairs(days) do
			local new = Instance.new("BoolValue")
			new.Name = i
			new.Parent = daysfolder
		end
	end


	
	clone.Parent = script
	clone.Name = player.Name
	clone.Disabled = false
end)

function savestats(player)
	local days = {}
	for i, v in pairs(player.Days:GetChildren()) do
		table.insert(days, v.Name)
	end

	daysstroe:SetAsync(player.UserId, days)
end

game.Players.PlayerRemoving:Connect(function(player)
	savestats(player)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		savestats(player)
	end
end)

Script under the mainscript

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TimeStore = DataStoreService:GetDataStore("TimeStorage103")
local Config = require(script.Parent.Parent.MainConfig)
local CanSave = true
local DNew = false
local labelGui = game.StarterGui.DailyReward.Normal.Cashout
local labelCooldown = 86400 

game.ReplicatedStorage.GiveMoney.OnServerEvent:Connect(function(player, plrName)
	print("event Fired")
	print("Player:", player)
	print("Player Name:", plrName)
	if player and DNew == false then
		DNew = true
	end
end)
print("Server script is running")

while true do
	wait()
	local player = Players:FindFirstChild(script.Name)
	if player then
		local timeLast = TimeStore:GetAsync(player.UserId)
		local currentTime = os.time()
		if not timeLast or (currentTime - timeLast) >= 86400 then
			local daysFolder = player:FindFirstChild("Days")
			local max = 0
			for _, v in ipairs(daysFolder:GetChildren()) do
				local now = tonumber(v.Name)
				if now > max then
					max = now
				end
			end
			local nextNum = max + 1
			if nextNum - 1 == Config.Days then
				daysFolder:ClearAllChildren()
				nextNum = 1
			end

			local reward = Config.checkreward(nextNum, player)
			print("Reward:", reward)
			DNew = false
			repeat
				wait()
			until DNew == true

			local stats = player:FindFirstChild("stats")
			if stats then
				local currency = stats:FindFirstChild("Currency")
				if currency then
					currency.Value = currency.Value + reward
					print("Rewards have been given")
				else
					print("Currency value not found in stats")
				end
			else
				print("stats not found")
			end

			local bool = Instance.new("BoolValue")
			bool.Name = tostring(nextNum)
			bool.Parent = daysFolder

			TimeStore:SetAsync(player.UserId, currentTime)
			labelGui.Visible = false 
			wait(labelCooldown)
			labelGui.Visible = true 
		end
	end
end

client side


local player = game.Players.LocalPlayer
local gui = script.Parent


local function claimButtonClicked()
	print("Claim button has been clicked!")

	local plrName = player.Name
	local giveMoneyEvent = game.ReplicatedStorage.GiveMoney

	if giveMoneyEvent then
		print("Attempting to fire event")
		print("Player:", player)
		print("Player Name:", plrName)
		giveMoneyEvent:FireServer(player, plrName)
		print("Event fired to server")
	else
		print("GiveMoney event not found")
	end

end

gui.Activated:Connect(claimButtonClicked)


There are quite a few things in your code that you might consider changing. Whenever you get and save data async, you should put it inside of a pcall(). You also shouldn’t be saving the player’s time so quickly because it can overwhelm the data store. You should usually autosave data around every 5 minutes or so.

As for the main problem, you never told the client to hide the button. What you should do is send some data to the client to tell it if it should hide the button when the player joins. This can be done with this code.

-- when a player joins, fire this remote to tell the client to hide the button
remoteEvent.OnClientEvent:Connect(function(hideButton)
	button.Visible = hideButton
end)

Also, when you fire the GiveMoney remote event, that’s when you would check on the server if the player is allowed to get the money or not. I’m guessing you’re new to scripting, and if so, then I’d recommend looking up tutorials for saving data.

yes, I am new to scripting and will try your suggestion. Thank you