Help subtracting leaderstats from client-server

having trouble with subtracting leaderstats on client-server here is local script in the button

local plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if plr.leaderstats.Coins.Value >= 100 then
		game.ReplicatedStorage.Sphere2.Parent = plr.PlayerScripts
		local coins = plr.leaderstats.Coins.Value
		game.ReplicatedStorage.BuyRemotes.BoughtSphere2:FireServer(coins)
	end
end)

here is script in serverscriptservice

game.ReplicatedStorage.BuyRemotes.BoughtSphere2.OnServerEvent:Connect(function(coins)
	coins -= 100
end)

giving me this error

ServerScriptService.Script:2: attempt to perform arithmetic (sub) on Instance and number

although im not sure why any help is appreciated thank in advance

1 Like

the error is self explanatory, you’re trying to substract an object

coins.Value -= 100
2 Likes

They are 2 numbers though bc the coins is linked to plr leaderstats coins value

The first argument received by OnServerEvent is always the player, change it to this:

(Server)

game.ReplicatedStorage.BuyRemotes.BoughtSphere2.OnServerEvent:Connect(function(player, coins)
	coins -= 100
end)

EDIT: You are using a lot of bad practices… you don’t have a check on the server side (hackers) and you are passing the leaderstat value into the remote even though you have the player on the server side, I recommend changing your scripts to these instead:

Client:

local plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if plr.leaderstats.Coins.Value >= 100 then
		game.ReplicatedStorage.Sphere2.Parent = plr.PlayerScripts
		game.ReplicatedStorage.BuyRemotes.BoughtSphere2:FireServer()
	end
end)

Server:

game.ReplicatedStorage.BuyRemotes.BoughtSphere2.OnServerEvent:Connect(function(player)
	local coins = player.leaderstats.Coins
    if coins.Value >= 100 then
        coins.Value = coins.Value - 100
    end
end)

Hope this helps! :smile:

2 Likes

Ok thanks I will try this right now

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