Need help giving player coins

Hey, I’m working on a button that rewards a player if they have a coin bag in their inventory but nothing happens when I click the button. I’ve provided the script below it is not a local script, any help is appreciated.

local Players = game:GetService("Players")

local function onPlayerAdded(player)
local amount = 100
script.Parent.MouseButton1Click:Connect(function()
	local cooldown = false
		for i,v in pairs(player.Backpack:GetChildren() or player:GetChildren()) do if v:IsA("Tool") and v.Name == "Small Coin Bag" then
			if cooldown == false then
				v:Destroy()
				cooldown = true
				player:WaitForChild("stats").Coins.Value += amount
				script.Parent.Parent.CashOut:Play()

				end
				
		end
	end
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

If this isn’t a LocalScript, this wouldn’t work.

You need to use a RemoteEvent for client-server comnections.

Also, for making a leaderstats, you need to name the folder as “leaderstats”. You can’t name it anything else (assuming you wanted to make a leaderstats)

1 Like

I’ve made something that could help you with this. You could tune it however you’d like and to whatever your preferences are but from my experiments, it works pretty well.

Local Script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RemoteEvent = ReplicatedStorage.RemoteEvents:WaitForChild("Rewards") -- Change the path to whatever you have set

local localPlayer = Players.LocalPlayer

local cooldown = false

-- Once the button is activated (same as mousebutton1click but for all devices), loop through the players backpack and character for the tool
-- Named "Small Coin Bag" then if there's no cooldown we fire the request to the server
script.Parent.Activated:Connect(function()
	
	for index, value in pairs(localPlayer.Backpack:GetChildren() or localPlayer.Character:GetChildren()) do
		
		if value:IsA("Tool") and value.Name == "Small Coin Bag" then
			
			if cooldown == false then
				RemoteEvent:FireServer("CoinBagReward")
				
				cooldown = true
				task.wait(1) -- Change to however long you'd want
				cooldown = false
				
			end
			
		end
		
	end
	
end)

-- Received message back from the server that the coins were given
RemoteEvent.OnClientEvent:Connect(function(receivedMessage: string)
	
	if receivedMessage == "CoinBagRewarded" then
		script.Parent.Parent.Cashout:Play()
	end
	
end)

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RemoteEvent = ReplicatedStorage.RemoteEvents:WaitForChild("Rewards")

local AMOUNT_TO_REWARD = 100

-- Received the request from the client and we do some checks to make sure it's legitimate before adding the coin value and sending
-- the request back to the client confirming the player has received the reward
RemoteEvent.OnServerEvent:Connect(function(player: Player, request: string)
	if typeof(request) ~= "string" then
		return
	end
	
	if typeof(player) ~= "Instance" then
		return
	end
	
	if request == "CoinBagReward" then
		
		for index, value in pairs(player.Backpack:GetChildren() or player.Character:GetChildren()) do
			if value:IsA("Tool") and value.Name == "Small Coin Bag" then
				value:Destroy()
			end
		end
		
		player:WaitForChild("leaderstats").Coins.Value += AMOUNT_TO_REWARD
		
		RemoteEvent:FireClient(player, "CoinBagRewarded")
	end
	
	
end)

I know you have leaderstats set up, but you named it incorrectly and have it as “stats” which would not work. Leaderstats only registers if you type it as it’s name but in lowercase “leaderstats”.

Example:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player: Player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 0
	Coins.Parent = leaderstats
	
end)

I hope this helps or gives you an understanding of how to use it!

Here are some mini-resources you can check out for more information:
RemoteEvents
Client-Server Communication
TypeOf
Leaderstats

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.