Bizarre leaderstat subtraction

hey all,

i have this glitch here:

The code for the script is identical to another script that experiences no issues. The only difference between the two is that the one that is causing problems is a Localscript whereas the other one is a Serverscript.

Why might this be happening?
Code:

local button = script.Parent
local bruh = button.bruh
local investment = script.Parent.investment.Value
local price = script.Parent.price.Value
local fixedprice = script.Parent.fixedprice.Value
local name = "Slingshot"
local team = "Red"
local class = "Tools"

button.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local cash = player.leaderstats.Cash
	bruh.BackgroundTransparency = 0
	if (cash.Value >= 1 and investment + cash.Value < price or investment + cash.Value < fixedprice) then
			investment = investment + cash.Value
			cash.Value = 0
			price = fixedprice - investment
			local ShopColor = Color3.fromRGB(255,255,0):Lerp(Color3.fromRGB(0, 170, 0), investment/fixedprice)
			bruh.BackgroundColor3 = ShopColor		
			bruh:TweenSize(UDim2.new(investment/fixedprice,0,1,0), "In", "Linear", 1)
			button.TextLabel.Text = fixedprice-investment
	else
		if (cash.Value + investment >= price) then
			price = fixedprice - investment
			cash.Value = cash.Value - price
			price = 0
			investment = 250
			bruh.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			bruh.BackgroundTransparency = 1
			button.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			button.TextLabel.Text = 0
			game.ReplicatedStorage.Menuevent:FireServer(team,name,class)
			script.Disabled = true
		end	
	end
end)

Thanks!

I don’t believe that LocalScripts have the ability to change leaderstat values. Any changes to those values should be done on the server.

So I have to use a remotevent?

Yes, you would use a remote event to trigger the purchase (or whatever it is) on the server, and then either have a LocalScript listen for the changes through .Changed or through another remote event.

1 Like

Fantastic. Are you maybe able to help me with this question as well?

uhhhhhhhhh so i was trying to send cash.Value on the remote event as the player’s money, but that wasn’t working so i was trying to replace cash.Value with a variable and send that and this happens
image

Anyways how do i do this

What’s the code for that it errored in? The codefrom the error message is attempting to subtract a number and an `Instance.

What’s the value of the variable price?

a number value. it is just an int value that is a sibling of the local script. that is out of the quesiton because everytime i mess with the cash part the error changes

Can you be more specific that everytime you mess with cash part, the error changes?
What and how does the error change from?

it doesn’t change by itself, it got different errors only because i was changing things. it thinks that cash is a boolean??

image

game.ReplicatedStorage.Money.OnServerEvent:Connect(function(investment,player,cash,price)
	local target = game.Players:FindFirstChild(player)
	local e = target.leaderstats.Cash.Value
	if investment + e < price then
		target.leaderstats.Cash.Value = 0
	end
	if investment + e >= price then
		target.leaderstats.Cash.Value = target.leaderstats.Cash.Value - price
	end
end)
if (cash.Value >= 1 and investment + cash.Value < price or investment + cash.Value < fixedprice) then
		game.ReplicatedStorage.Money:FireServer(investment,player,cash,price)

What’s the value of the variable player? The variable target is nil.

and :FireServer implicitally passes the player instance as the first argument.

image

woah

how do i get the player from fire server

The first argument is the player, parameter names don’t matter. So passing :FireServer(1, 2) is just the same as passing LocalPlayer, 1, 2

-- Client
Event:FireServer(1, 2)

-- Server
Event.OnServerEvent:Connect(function(player, value0, value1)
    print(player, value0, value1) --> Player, 1, 2
end

So you can just do player.leaderstats.Cash.Value to get the value
Server:

game.ReplicatedStorage.Money.OnServerEvent:Connect(function(player,investment,cash,price)
	local e = player.leaderstats.Cash.Value
	if investment + e < price then
		target.leaderstats.Cash.Value = 0
	elseif investment + e >= price then
		player.leaderstats.Cash.Value -= price
	end
end)

Client:

if (cash.Value >= 1 and investment + cash.Value < price or investment + cash.Value < fixedprice) then
		game.ReplicatedStorage.Money:FireServer(investment,cash,price)
2 Likes