My script doesn't work when I have 100 cash and try to buy the sword

When I have 100 or more cash and try to buy the sword, the script doesn’t give it to me and prints ‘Not Enough Cash’ even though I have 100 or more cash.

Server Script:

game.ReplicatedStorage.Remotes.BuySword.OnServerEvent:Connect(function(player, cost)
		
		local sword = game.ReplicatedStorage.Stuff:WaitForChild("ClassicSword"):Clone()
		
		if player.leaderstats.Cash.Value >= cost then
			
			player.leaderstats.Cash.Value  = player.leaderstats.Cash.Value - cost
			
			sword.Parent = player.Backpack
			
		else
			
			print("Not Enough  Cash!!")
			
	end
end)

Local Script:


local player = game.Players.LocalPlayer
local 	BuySwordRemoteEvent = game.ReplicatedStorage.Remotes.BuySword
local cost = 100

script.Parent.MouseButton1Click:Connect(function()
	BuySwordRemoteEvent:FireServer(cost)
end)

1 Like

How are you giving yourself cash?

Client or Server?

1 Like

In the Local Script, I am assigning myself a random cash value.

but yea Client!

1 Like

Thats your issue. Cash needs to be server sided if the cash is being checked on the server

2 Likes

You need to give yourself cash through the server, because leaderstat changes made on client will not replicate to the server.

2 Likes

Try passing the player in the local script:

script.Parent.MouseButton1Click:Connect(function()
	BuySwordRemoteEvent:FireServer(player, cost)
end)


FireServer doesnt need a player, It returns a player from the client to the server…

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