Coin giver not working correctly

NOTE: I watched a youtube video on how to do this because I gave up trying to script it manually.

I’m trying to make a GUI that only the owner of the game can use which can give anybody coins. Giving it to the owner is specific works as intended. However, the giving coins Gui does not work.

Here is the script:

local person = nil
local amount = nil
local player = nil
script.Parent.MouseButton1Click:Connect(function()
person = script.Parent.Parent.Namebox.Text
amount = script.Parent.Parent.AmountBox.Text
player = game.Players:FindFirstChild(player)
player.leaderstats.BrickCoins.Value = player.leaderstats.BrickCoins.Value + amount
end)

This is the error it gives me:
Players.gumixchan.PlayerGui.GiveCoinsGui.Frame.TextButton.LocalScript:10: attempt to index nil with ‘leaderstats’ - Client - LocalScript:10

1 Like

Yes, cause you’re trying to find “player” without defining it.

local person = nil
local amount = nil
local player = nil
script.Parent.MouseButton1Click:Connect(function()
   person = script.Parent.Parent.Namebox.Text
   amount = script.Parent.Parent.AmountBox.Text
   player = game.Players:FindFirstChild(player) -- you are trying to find player with itself. Also player is nil so nothing is found
player.leaderstats.BrickCoins.Value = player.leaderstats.BrickCoins.Value + amount -- when you try to get the leaderstats folder, it errors since the player is still nil
end)

My apologies, just realised that. My fault, this is the correct answer.

1 Like

Oh, thanks for pointing that out- I didn’t realize that.

1 Like

Uhhh, now it gives me this error:


wait i dont know if u can see that-
Players.gumixchan.PlayerGui.GiveCoinsGui.Frame.TextButton.LocalScript:8: attempt to index nil with ‘leaderstats’ - Client - LocalScript:8

It still gives the same error which means the player is still not found. How are you looking for the player?

You type the name of somebody in a textbox. I don’t know if that works but it’s the only way I could think of to make what I wanted to make.

Oh. Are you doing it on a server script?

The script I showed you was a Local Script.

Oh, then I’m confused too. Also changing the values with a LocalScript don’t replicate so they’re pretty much useless, use a RemoteEvent instead.
I thought the problem was that you’re using a server script, as the server can’t see a textbox text written on the client.

I had the same problem with this when I added a datastore to the leaderstats I am not sure why though.

If I use a RemoteEvent won’t it not know what the player is?
It won’t know what the amount is either.

You will, you just need it in your parameters. When you fire the event from client to server, one of the parameters is automatically there, the player. Then just fire over the player that they want to send the money to. Also, as I said above (which I deleted), maybe your making your leaderstats folder wrong, with a misspelt name or something. This is useless client sided, but would still work.

(just fire over the player they want to send it to, and the amount)

Am I doing this right because It’s giving me an error:

In the first script (Local Script):
game.ReplicatedStorage.CoinEvent:FireServer(player, amount)

In the second script (regular script):
game.ReplicatedStorage.CoinEvent.OnServerEvent:Connect(function(player, amount)
player.leaderstats.BrickCoins.Value = player.leaderstats.BrickCoins.Value + amount
end)

The error it gives me is:
ServerScriptService.Script:2: attempt to perform arithmetic (add) on number and nil - Server - Script:2

Ok, so what your doing is attempting to add the amount, but you send over the amount as a string, as well, you can’t add a string to a number.

This should fix that error

local:

local amountToSend = tonumber(amount)

game.ReplicatedStorage.CoinEvent:FireServer(playerToSend, amountToSend)

Avoid using player, as the server will already pick that up on the server.

Server:

game.ReplicatedStorage.CoinEvent.OnServerEvent:Connect(function(playerToSend, amountToSend)

playerToSend.leaderstats.BrickCoins.Value = player.leaderstats.BrickCoins.Value + amountToSend

end)

I’m a little confused. I just swapped out the names I can’t tell if thats all you wanted me to do because it gives me the same error.

Are you sending over the text of the box, or the box itself?

In other words, could you send your script please :slight_smile:

After that, he assigns it to the textbox text.

local person = nil
local amount = nil -- assigned nil
local player = nil
script.Parent.MouseButton1Click:Connect(function()
person = script.Parent.Parent.Namebox.Text
amount = script.Parent.Parent.AmountBox.Text -- assigned 
player = game.Players:FindFirstChild(player)
player.leaderstats.BrickCoins.Value = player.leaderstats.BrickCoins.Value + amount
end)

By the way, you’ll need to use remotes if your going to send coins to another player from a localscript.

local person = nil
local amount = nil
local player = nil

local button = script.Parent

button.MouseButton1Click:Connect(function()
person = button.Parent.Namebox.Text
amount = button.Parent.AmountBox.Text
player = game.Players:FindFirstChild(person)

if player then
player.leaderstats.BrickCoins.Value += amount
end
end)

Sorry, I was eating dinner and it took longer than I thought.

Anyways, I’m sending over the text inside of the Box when the Button is clicked

This is the script:

In a Local Script:
local person = nil

local amountToSend = nil

local playerToSend = nil

script.Parent.MouseButton1Click:Connect(function()

person = script.Parent.Parent.Namebox.Text

amountToSend = script.Parent.Parent.AmountBox.Text

playerToSend = game.Players:FindFirstChild(person)

game.ReplicatedStorage.CoinEvent:FireServer(playerToSend, amountToSend)

end)

In a Regular Script:
game.ReplicatedStorage.CoinEvent.OnServerEvent:Connect(function(playerToSend, amountToSend)

playerToSend.leaderstats.BrickCoins.Value = playerToSend.leaderstats.BrickCoins.Value + amountToSend

end)