Coin script only gives to one person in a server

My friend made a daily coin giver and for some reason it only works for one person per sever

EDIT: I’m new to scripting lua so much questions may sound dumb

currency = "Coins"	
amnt = 20					
debounce = false		

	function onTouch(hit)
		if hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == false then
			if game.Players:findFirstChild(hit.Parent.Name) ~= nil then
			ThisPlayer = game.Players:findFirstChild(hit.Parent.Name)
			ThisPlayer.leaderstats:findFirstChild(currency).Value = >ThisPlayer.leaderstats:findFirstChild(currency).Value + amnt
			script.Parent.Transparency = 1
			script.Parent.CanCollide = false
			debounce = true
			script.Sound:Play()
			wait(86400)		-- Respawn time for the cash giver
			script.Parent.Transparency = 0
			script.Parent.CanCollide = true
			debounce = false
			end
		end
	end


script.Parent.Touched:connect(onTouch)

https://www.loom.com/share/e6677e32d0ef46b0ae56d1303de3ea4d

1 Like

I mean, consider the fact that this function is debounced for 86400 seconds (24 hour yield). It’s not that it only works for one person, the delay is absurdly large. You need to find another way to do this.

Consider this: handle the coin’s appearance locally. The server and the client will work together here. The server will determine if the player is eligible for reward or not (has not claimed a reward in the last 24h) and tell the client. The client will then accordingly make the coin visible or not for that game session alone and then that’s the end of that.

2 Likes

If you don’t care about players being able to hop servers to get coins you only need to store the player’s UserId and the tick() they last used the coin giver.

currency = "Coins"	
amnt = 20					
WaitTime = 86400		
local PlayerArray = {}

local function getPlayerFromCharacter(character)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character == character then
			return player
		end
	end
	return nil
end

function onTouch(hit)
	if hit.Parent:findFirstChild("Humanoid") == nil then -- Check that its Humanoid
		return false
	end
	local Player = getPlayerFromCharacter(hit.Parent)-- Make sure it was a player and not some sort of npc
	if(Player == nil) then
		print("Humanoid wasn't player")
		return false
	end
	if(PlayerArray[Player.UserId] == nil or PlayerArray[Player.UserId] <= tick()-WaitTime) then -- Check if the player needs to wait
		PlayerArray[Player.UserId] = tick()
	else
		print("Player:"..Player.Name.." has to wait ".. (PlayerArray[Player.UserId]+WaitTime)-tick().." more seconds.")
		return false
	end
	Player.leaderstats:findFirstChild(currency).Value = Player.leaderstats:findFirstChild(currency).Value + amnt
	script.Parent.Transparency = 1
	script.Parent.CanCollide = false
	script.Sound:Play()
	wait(1)
	script.Parent.Transparency = 0
	script.Parent.CanCollide = true
end

script.Parent.Touched:connect(onTouch)

You would be better off using Data Stores but this should work for a temporary solution.

1 Like

I noticed that you made an error in your script.
Error: ThisPlayer.leaderstats:findFirstChild(currency).Value = >ThisPlayer.leaderstats:findFirstChild(currency).Value + amnt
Correct: ThisPlayer.leaderstats:findFirstChild(currency).Value = ThisPlayer.leaderstats:findFirstChild(currency).Value + amnt

currency = “Coins”
amnt = 20
debounce = false

function onTouch(hit)
	if hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == false then
		if game.Players:findFirstChild(hit.Parent.Name) ~= nil then
		ThisPlayer = game.Players:findFirstChild(hit.Parent.Name)
		ThisPlayer.leaderstats:findFirstChild(currency).Value = ThisPlayer.leaderstats:findFirstChild(currency).Value + amnt
		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		debounce = true
		script.Sound:Play()
		wait(86400)		-- Respawn time for the cash giver
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		debounce = false
		end
	end
end

script.Parent.Touched:connect(onTouch)

The problem is that you are handing this globally, that means the debouce will be replicated to all clients!

What you can do is handle this in a local script in StarterPlayerScripts:

local Wspace = game:GetService("Workspace")
local RepStorage = game:GetService("ReplicatedStorage")
local PartToTouch = Wspace:FindFirstChild("DailyRewardBlock")

function onTouch(hit)
    if hit:FindFirstChild("Humanoid") then
        local DailyEvent = RepStorage:FindFirstChild("DailyEvent") --You must add a remote event in replicated storage!
        DailyEvent:FireServer()
    end
end

PartToTouch.Touched:Connect(onTouch)

And now you must set a normal script, i would recommend you set this is ServerScriptService:

local RepStorage = game:GetService("ReplicatedStorage")
local DailyEvent = RepStorage:FindFirstCshild("DailyEvent")

local DailyRewardValue = 10

DailyEvent.OnServerEvent:Connect(function(player)
    local Leaderstats = player:FindFirstChild("Leaderstats")
    if (Leaderstats ~= nil) then
        local Debounce = Leaderstats:FindFirstChild("Debounce") --Add a Debounce BoolValue
        local DebounceTime = Leaderstats:FindFirstChild("DebounceTime") --Add an IntValue
        local Coins = Leaderstats:FindFirstChild("Coins")
        if (Coins ~= nil) and (Debounce ~= nil) and (DebouceTime ~= nil) then
            if not Debounce then
                Debounce.Value = true
                Coins.Value = Coins.Value + DailyRewardValue
                DebounceTime.Value = 86400
            end
        end
    end
end)

And, add this script in serverscriptservice too:

game.Players.PlayerAdded:connect(function(player)
    local Leaderstats = player:FindFirstChild("Leaderstats")
    if Leaderstats ~= nil then
        local Debounce = Leaderstats:FindFirstChild("Debounce")
        local DebounceTime = Leaderstats:FindFirstChild("DebounceTime") -- Add an IntValue in Leaderstats
        if (Debounce ~= nil) and (DebounceTime) ~= nil then

            while true do
                DebounceTime.Value = DebounceTime.Value - 1
                wait(1)
            end

            DebounceTime.Changed:Connect(function()
                if DebounceTime.Value <= 0 then
                    Debounce.Value = false
                end
            end)
        end
    end
end)

This script took 10 minutes to do lol, because im lagging, and yeah, i didn’t test it, so if you find any error tell me and i will try to fix it, because i made this very rushed. Thanks for reading :+1::+1::+1::+1:

For the last script would I need to make it a local script in serverscriptservice?

Also thanks for making the script for me. For some reason learning lua has been hard compared to learning html and css which I learned all in a week. :+1:

No, it’s a script in serverscriptservice

And no problem, html is harder actually.

Sorry about my dumb question

For the last script would I need to make it a local script in serverscriptservice?

I didnt read what you said correctly

And, add this script in serverscriptservice too:

So for my final question, the middle script and the last script are two different scripts that both go into serverscriptservice?

And what I meant buy learning html and css in a week is learning it, and also making one of those responsive websites you see professionals doing as well, and publishing it to github(I could only upload it to github because im broke and I dont have a paypal) But learning lua on the other hand took me about 6 months just to like figure it out and edit parts of the script. So rn I can only read and edit scripts to fit what I need them to do, but for some reason I cant make a script from scratch to do what I want it to do. :sweat_smile:

I have edited it, sorry!

Yeah

30CHAAAAAAAAAARS

1 Like

So you said I should add these

local Debounce = Leaderstats:FindFirstChild("Debounce") --Add a Debounce BoolValue
local DebounceTime = Leaderstats:FindFirstChild("DebounceTime") --Add an IntValue
local DebounceTime = Leaderstats:FindFirstChild("DebounceTime") -- Add an IntValue in Leaderstats

and that i should add a BoolValue, Intvalue
Where would I add those?

Btw the first 2 lines are from the second scripts
and the last one is from the bottom script

Could you show me your leaderstats script?

Here is my leaderstats script

local datastore = game:GetService("DataStoreService")
local singleStorage = datastore:GetDataStore("PlayerSaves")
local DefaultStats = {
	coins = 0;
	gems = 0;
	kills = 0;
	rank = "Default";
}

game.Players.PlayerAdded:connect(function(plr)
   local folder = Instance.new("Folder")
   folder.Name = "leaderstats"
   folder.Parent = plr
   
   local coins = Instance.new("IntValue")
   coins.Name = "Coins"
   coins.Parent = folder
	coins.Value = 50
   
   local gems = Instance.new("IntValue")
   gems.Name = "Gems"
   gems.Parent = folder
	gems.Value = 10
   
   local kills = Instance.new("IntValue") 
   kills.Name = "Kills"
   kills.Parent = folder    
   
   local rank = Instance.new("IntValue")
   rank.Name = "Rank"
   rank.Value = 1
   rank.Parent = folder
	
	local Success, data = pcall(function() 
		return singleStorage:GetAsync(plr.UserId)
	end)
	
	if not Success then
		print("Failed to load Data")
		if data then 
           coins.Value = data[1]
	       gems.Value = data[2] 
           kills.Value = data[3] 
           rank.Value = data[4]
		else
		    coins.Value = DefaultStats.coins
		    gems.Value = DefaultStats.gems
		    kills.Value = DefaultStats.kills
		    rank.Value = DefaultStats.rank
   	    end 
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
   local leaderstats = plr:FindFirstChild('leaderstats')
	
	if leaderstats then
		local saveData = {
           leaderstats['Coins'].Value,
           leaderstats['Gems'].Value,
           leaderstats['Kills'].Value,
           leaderstats['Rank'].Value
       }
		
		local Success, Error = pcall(function()
			singleStorage:UpdateAsync(plr.UserId, function(OldData)
				return saveData
			end)
		end)
		
		if not Success then
			print("Failed to load Data")
		end
	end
end)

So, add those values in the script:

local Debounce = Instance.new("BoolValue")
Debounce.Name = "Debounce"
Debounce.Parent = folder
Debounce.Value = true

local DebounceTime = Instance.new("IntValue")
DebounceTime.Name = "DebounceTime"
DebounceTime.Parent = folder
DebounceTime.Value = 86400

That’s it, add that :+1:

Would that go into your scripts that you gave me earlier or will that go into the leaderstats script

Into leaderstats. 30charssssss