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).
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.
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)
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
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.
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)…
Ok, good 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
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.
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.