Player purchase system not working

when player clicks the button it should take the price out of the tokens

---/// locals
local PurchaseItem = game.ReplicatedStorage.Events.PurchaseItem
local PurchaseButton = script.Parent
local PricesFolder = script.Parent.Parent.Parent.Parent.Prices
local Item = PricesFolder.Speed
local Price = Item.Value
local player = game.Players.LocalPlayer

PurchaseButton.MouseButton1Click:Connect(function(plr)
	
	if player.leaderstats.Tokens.Value >= Price then
		player.leaderstats.Tokens.Value = player.leaderstats.Tokens.Value - Price
		
	end 
end)

The Bug

player.leaderstats.Tokens.Value = player.leaderstats.Tokens.Value - Price
1 Like

You should put this in a script, not a local script. This is because only the player would be able to see the tokens change.

1 Like

This won’t work because you are trying to do this locally.

You need to fire a remote event to the server and make the purchase there.

Create a remote event and place it in ReplicatedService

local script

PurchaseButton.MouseButton1Click:Connect(function(plr)
        if player.leaderstats.Tokens.Value >= Price then	 
              game.ReplicatedStorage.BuySomething:Fire()
       end 
end)

Server script:

game.ReplicatedStorage.BuySomething.OnServerEvent:Connect(function(player)
        if player.leaderstats.Tokens.Value >= Price then
 	player.leaderstats.Tokens.Value = player.leaderstats.Tokens.Value - Price
	
    end 
end)

I typed this out quick so I may have made a mistake :upside_down_face:

1 Like