NumberValue subtracting more then it should

Hello, fellows. I’ve coded a shop system that when you buy an item, it’s supposed to subtract your number of funds, which is a NumberValue inside a folder called “Status” inside of the player. However it seems like it is subtracting more then it really should. How can I fix this?
I’ve used

game.Players.LocalPlayer:WaitForChild("Status").funds.Value = game.Players.LocalPlayer:WaitForChild("Status").funds.Value - game.ReplicatedStorage.Modules.Prices[v.Name].Value

and

game.Players.LocalPlayer:WaitForChild("Status").funds.Value -= game.ReplicatedStorage.Modules.Prices[v.Name].Value

I guess to get a little bit of a better understanding of the problem, When I set my funds value to 10 and buy an item for $5 in the shop, it sets it to 0, even though it should just subtract 5.

I’ve noticed that this is in a local script. You should handle things like this in the server because the server won’t know that you took money from the player, and most likely give you lots of problems.

1 Like

Alright, well I’m using a RemoteEvent to remove funds from the player Server-Sided, however it just seems like it’s subtracting more.

Client:

game.ReplicatedStorage.Remotes.changeFunds:FireServer(game.ReplicatedStorage.Modules.Prices[v.Name].Value)

Server:

game.ReplicatedStorage.Remotes.changeFunds.OnServerEvent:Connect(function(plr, numberoffunds)
	plr:WaitForChild("Status").funds.Value = plr:WaitForChild("Status").funds.Value - numberoffunds
end)

I don’t know if this is how you do it efficiently, I’m fairly new to this.

Maybe because you’re subtracting twice?

Well I don’t really see where I’m subtracting it twice, so I don’t think so.

You’re subtracting in both server and client side

I’m confused, it should only subtract when the remote event is fired, and in the server code I don’t know how it would subtract itself twice. Can you please elaborate?

In this local script, you subtracted and you subracted again in the server side by firing the remote event here

Which does this:

maybe add a debounce variable that could prevent it from happening twice

local Subtracted = false
game.ReplicatedStorage.Remotes.changeFunds.OnServerEvent:Connect(function(plr, numberoffunds)
    if not Subtracted then
	    Subtracted = true
	    plr:WaitForChild("Status").funds.Value = plr:WaitForChild("Status").funds.Value - numberoffunds
	    wait(0.1)
	    Subtracted = false
    end
end)

Yes however it should only fire itself once, therefore it should subtract just once on the server

Also Hiroki, I’ll try that script you sent

1 Like

Yes, but you have a local script which subtracts also. Does the event fire first and then the local script after?

(this local script:)

Oh, I removed that and replaced it with the remote event.

Btw, Hiroki your script seems to work. Thanks!

2 Likes

no problem! Glad I could help out with your problem!

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