Number Value not decreasing on Server Script

Hi I’m trying to make a Shop Gui that decreases you’re Leaderstats Currency if you have bought an item but the script somehow bugs and decreases the wrong number:

This is the script I use:

local Player = script:FindFirstAncestorWhichIsA("Player")
local Tool = game.ReplicatedStorage.BusinessItems.JokeGun

game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent.OnServerEvent:Connect(function()
	Player.leaderstats.Rubles.Value = Player.leaderstats.Rubles.Value - 10
	Tool:Clone().Parent = Player.Backpack
end)
1 Like

Make sure the way the player gets the currency on the server side

1 Like

As @Cyber_Designer said, try to change the currency on the server instead of firing to the client. Client still picks up server changes.

try this this should work

local Tool = game.ReplicatedStorage.BusinessItems.JokeGun

game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent.OnServerEvent:Connect(function(Player)
	local Leaderstats = Player:FindFirstChild('leaderstats')
	if Leaderstats and Tool then
		Player.leaderstats.Rubles.Value -= 10
		Tool:Clone().Parent = Player.Backpack
	end
end)
1 Like

=- is just a shortcut to what the original script did. While it does help write code, it doesn’t solve the problem.

I will try the old code again just to make sure

Okay, I tried the old code. Both work perfectly fine. So there is obviously something else going on here.

(only thing I removed from the old code was local Player = script:FindFirstAncestorWhichIsA("Player"))
(replaced with OnServerEvent:Connect(Function(Player)
1 Like

Wait like this?

local Player = OnServerEvent:Connect(Function(Player)

Oh you meant

local Tool = game.ReplicatedStorage.BusinessItems.JokeGun

game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent.OnServerEvent:Connect(function(Player)
	Player.leaderstats.Rubles.Value -= 10
	Tool:Clone().Parent = Player.Backpack
end)

Still doesn’t work.

Kindly switch to server side while testing and find the path location of your NumberValue. As per your issue, I assume that the server cannot see the NumberValue. In order to fix this, change the location of NumberValue to a leaderstats folder or else into Player (as shown in below code) which then gets transferred to the Player’s folder whenever a new Player joins.

Player.PlayerAdded....(Player)
   local NumberValue = Instance.new("NumberValue",Player)
   --other changes for NumberValue
end)

Sorry I just now read your previous messages before mine and I see that your NumberValue is located in the leaderstats.
The Rubles does get incremented or changed from your serverside but you have to change the NumberValue of your script in the serverscriptservice itself.

local Tool = game.ReplicatedStorage.BusinessItems.JokeGun

game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent.OnServerEvent:Connect(function(Player)
	local Leaderstats = Player:FindFirstChild('leaderstats')
	if Leaderstats and Tool then
        local Decreased = Player.leaderstats.Rubles.Value - 10
		Player.leaderstats.Rubles.Value -= 10
        Player.PlayerGui...Text = tostring(Decreased)
		Tool:Clone().Parent = Player.Backpack
	end
end)

Was the default value of IntValue “Rubles” was 1000? or did you set it from the client?
If you did set the value of IntValue (to 1000), the value SHOULD be -10 because the change doesnt replicate to server. ( you’ve set the value of IntValue on your client, which does not replicate to serverside. )
Also If you didn’t set the value in client, and the game set your Rubles value to 1000, then you should probably check the script that sets the Ruble(IntValue)'s Value to 1000, If that is localScript, the value of Rubles doesn’t replicate to server. which means server will still know the value of Ruble as 0. so it changes the value to -10. ( 0 - 10 = -10 )

So, the problem looks like to be replicating problem, you should check your LocalScripts, and add some remoteEvents to set the value in server.

I did Local Script but people can’t see if the stats has changed.

Hmmm… I’ll try it soon still thinking

Sorry, I meant serverscript in the serverscriptservice. If you do it via localscript then only the player can see his changes.

That’s what I’m trying to do… Server Script…

Is it possible for you to show the local script that fires the event?

1 Like

Yeah sure

Local Script:

local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if Player.leaderstats.Rubles.Value >= 10 then
		game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent:FireServer()
		script.Parent.Parent.Parent.Parent.Parent.Parent.BobGeneralStore.Enabled = false
	end
end)

Server Script:

local Tool = game.ReplicatedStorage.BusinessItems.JokeGun

game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent.OnServerEvent:Connect(function(Player)
	Player.leaderstats.Rubles.Value -= 10
	Tool:Clone().Parent = Player.Backpack
end)

Here’s something that might be useful:

image

image

This is strange. The code is perfectly fine, yet it bugs out.

Did you update your balance to 1000 coins on the client side or the server side? This is the only thing I can think of that could cause this issue.

1 Like

In server like what i did in the video

Anyways did it worked for you? And also if you use client it works and subtracts as the number it should be but since it’s local nobody can see.