Cash system not adding to intvalue in remote event

I have a remote event, which is supposed to add cash to the player. Cash is stored as an intvalue inside a data folder in the player. here is the code to the event (the event fires properly from client):

local addCashEvent = Instance.new("RemoteEvent", ReplicatedStorage)
addCashEvent.Name = "AddCashEvent"

local function addCashFired(plr, amount)
	local playerCash = plr:WaitForChild("PlayerData"):WaitForChild("Cash")
	print(playerCash.Value) -- prints the proper value
	--wrapping in tonumber because the intvalue is not changing
	local currentCash = tonumber(playerCash.Value)
	local resultCash = tonumber(currentCash + amount)
	playerCash = resultCash

end

addCashEvent.OnServerEvent:Connect(addCashFired)

when running the following command in command bar, the cash value does not change, but it successfully prints the amount of cash the player has (as put in the script).

game:GetService("ReplicatedStorage"):FindFirstChild("AddCashEvent"):FireServer(100000)

as seen below:

does anyone have ideas?

1 Like

Try adding the remote event manually.

It runs, it just doesn’t add to the intvalue

Try to use print debug to detect which line of code is not working.

It could be a problem with indexing playerCash with Value. Try playerCash.Value. I can see you have written it properly in the print, but not for adding the value in the line right below it.

Because playerCash is an IntValue, it has to be indexed with Value to access or change its value.

1 Like
local addCashEvent = Instance.new("RemoteEvent", ReplicatedStorage)
addCashEvent.Name = "AddCashEvent"

local function addCashFired(plr, amount)
	local playerCash = plr:WaitForChild("PlayerData"):WaitForChild("Cash")
	playerCash.Value += math.abs(amount)
end

addCashEvent.OnServerEvent:Connect(addCashFired)
1 Like

very odd. it prints the value just fine, but still doesn’t add to the value.

when changing the value (without any math) it will work fine.

This script does not print the value so I don’t know what you are talking about… did you use your old script by accident?

Got it to work:

local function addCashFired(plr, amount)
	local playerCash = plr:WaitForChild("PlayerData"):WaitForChild("Cash")
	print(playerCash.Value)
	
	local result = playerCash.Value + amount
	print(tostring(result))
	playerCash.Value = tonumber(result)

end

You can undo the frivolous casting:

local function addCashFired(plr, amount)
	local playerCash = plr:WaitForChild("PlayerData"):WaitForChild("Cash")	
	playerCash.Value += amount
end

Even though I have no clue what game/use you are doing this for, why are you using an event to give cash? That’s a very bad idea and is a potential high security risk as exploiters can abuse this. I recommend adding a check or doing it all on the server if anything.

1 Like

ok. Thanks for the advice, as a starter with remote events, I wasn’t really sure where to use them and where not to use them. I’m thinking of implementing a shop for cash and an inventory system. How would I do a check, or make this system secure (as I’ve done the same client side additions script for adding inventory items)…

I’m wondering if your problem was fixed?

yes, it was. now the issue is security of my methods.

1 Like

Ok, good :grinning: I make the same mistake all the time. Just gotta make sure you keep an eye out for that. I find it odd that it didn’t print an error message though :thinking:

1 Like

Depends on what you are doing, for example, if you just want owners to use this method, firing a remote event to earn cash, you can simply do if plr.Name == yourname then on the SERVER when it’s fired. I don’t know why you want to do this on the client, you should always manage this type of thing on the server.

If you want to do a shop system, let’s say it was a GUI, you could put a server script in the GUI, and when a player purchases an item, it will give them the item + take away the money in that same script. Nothing really has to be local here.

He split it into an event to the server from the client. Am I missing something?

what’s an example of a server script inside a gui element that would get me started on functions like this?

Actually I am mistaken. Just use a remote for purchasing, and when that remote is fired, check if the player has the amount of cash needed, then take away that cash + give the player that item.