Daily reward system gives multiple rewards on click

hi, i’m having trouble with my daily reward script. when you click the button to click your reward, its possible that you can spam-click the button to get the same reward over and over (for a few seconds).
here’s some of the script:


local Rewards = {
	["Coins"] = {
		[1] = 5,
		[2] = 10,
		[3] = 20
	},
	["Gems"] = {
		[1] = 5,
		[2] = 10,
	}
}

game.Players.PlayerAdded:Connect(function(Player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	
	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Parent = leaderstats
	
	local CurrentTime = os.time()
	
	local Data
	pcall(function()
		Data = DataStore:GetAsync(Player.UserId.."_DailyReward")
		print("Getting data for ID number "..Player.UserId)
	end)
	
	if Data then
		local TimePassedSinceClaim = CurrentTime - Data
		
		print("Time passed: "..TimePassedSinceClaim)
		
		if (TimePassedSinceClaim / 3600) >= DailyRewardWait then
			-- Eligible for reward
			local RewardValue = RewardType[math.random(1,#RewardType)]
			
			local ValueToReward
			if RewardType == Rewards.Coins then
				ValueToReward = "Coins"
			else
				ValueToReward = "Gems"
			end
			
			game.ReplicatedStorage.SendRewardData:FireClient(Player, DailyRewardWait, RewardValue, ValueToReward)
			local Connection
			Connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(TriggerPlayer)
				if TriggerPlayer == Player then
					print("Reward claimed")
					Player.leaderstats:FindFirstChild(ValueToReward).Value += RewardValue
					DataStore:SetAsync(Player.UserId.."_DailyReward", os.time())
					Connection:Disconnect()
				end
			end)
		else
			print("Player isn't eligible")
			game.ReplicatedStorage.UneligibleText:FireClient(Player, DailyRewardWait)
		end
	else
		print("Player's first join")
		local RewardValue = RewardType[math.random(1,#RewardType)]

		local ValueToReward
		if RewardType == Rewards.Coins then
			ValueToReward = "Coins"
		else
			ValueToReward = "Gems"
		end

		game.ReplicatedStorage.SendRewardData:FireClient(Player, DailyRewardWait, RewardValue, ValueToReward)
		local Connection
		Connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(TriggerPlayer)
			Connection:Disconnect()
			if TriggerPlayer == Player then	
				print("Reward claimed")
				Player.leaderstats:FindFirstChild(ValueToReward).Value += RewardValue
				DataStore:SetAsync(Player.UserId.."_DailyReward", os.time())
				Connection:Disconnect()
			end
		end)
	end
end)

any help would be really appreciated!

1 Like
Connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(TriggerPlayer)
	Connection:Disconnect()
	if TriggerPlayer == Player and TriggerPlayer:FindFirstChild("DRCD") == nil then	--Added if there is no daily reward cd in player check.
		
		local cd = Instance.new("BoolValue") --Creating cooldown value.
		cd.Parent = TriggerPlayer
		cd.Name = "DRCD"
		game.Debris:AddItem(cd, 10)
		
		print("Reward claimed")
		Player.leaderstats:FindFirstChild(ValueToReward).Value += RewardValue
		DataStore:SetAsync(Player.UserId.."_DailyReward", os.time())
		Connection:Disconnect()
	end
end)

I searched for “click” and “button” but couldn’t find any mention of either. However what you can do is add a debounce to prevent the button from being spammed/rewards from being awarded too frequently.

More info:

Could I see the code that senses when the button is clicked and sends the remote event.

tried this in studio but didn’t work. maybe it’s a problem on roblox’s end?

game.ReplicatedStorage.SendRewardData.OnClientEvent:Connect(function(Hours, RewardValue, ValueType)
	script.Parent.Frame.ClaimReward.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.ClaimReward:FireServer()
		script.Parent.Frame.ClaimReward.Text = RewardValue.." "..ValueType.." received!"
		script.Parent.Frame.RewardStatus.Text = "Today's reward has been claimed!"
		wait(2)
		script.Parent.Frame.Visible = false
	end)
end)
game.ReplicatedStorage.UneligibleText.OnClientEvent:Connect(function(Hours)
	script.Parent.Frame.ClaimReward.Text = "OK"
	script.Parent.Frame.RewardStatus.Text = "Today's reward has been claimed! Come back in ".. Hours .." hours to claim your next reward."
	script.Parent.Frame.ClaimReward.MouseButton1Click:Connect(function()
		script.Parent.Frame.Visible = false
	end)
end)

hope this helps.

You should add an if statement to detect if the button has already been clicked.

local AlreadyClicked = false

game.ReplicatedStorage.SendRewardData.OnClientEvent:Connect(function(Hours, RewardValue, ValueType)
   script.Parent.Frame.ClaimReward.MouseButton1Click:Connect(function()
      if AlreadyClicked == false then
           AlreadyClicked = true
		   game.ReplicatedStorage.ClaimReward:FireServer()
		   script.Parent.Frame.ClaimReward.Text = RewardValue.." "..ValueType.." received!"
		   script.Parent.Frame.RewardStatus.Text = "Today's reward has been claimed!"
		   wait(2)
		   script.Parent.Frame.Visible = false
	   end
    end)
end)
game.ReplicatedStorage.UneligibleText.OnClientEvent:Connect(function(Hours)
	script.Parent.Frame.ClaimReward.Text = "OK"
	script.Parent.Frame.RewardStatus.Text = "Today's reward has been claimed! Come back in ".. Hours .." hours to claim your next reward."
	script.Parent.Frame.ClaimReward.MouseButton1Click:Connect(function()
		script.Parent.Frame.Visible = false
	end)
end)

Try this. ^

1 Like

thanks! i was losing my mind tryna make this work lol.

1 Like