Solutions to this Shop problem?

Hello everyone, my name is Spideyalvaro999
Yesterday, I was testing the new shop I created on my game but it has a probelm. I use this leaderstats(they include the leaderstats, the data store and some remote events from another thing, nothing relevant).This is the script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems") -- Where tools are held
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")
game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local points = Instance.new("IntValue",leaderstats)
	points.Name = "Points"
	points.Value = ds:GetAsync(player.UserId)or 0
	ds:SetAsync(player.UserId, points.Value)
	points.Changed:connect(function()
	    ds:SetAsync(player.UserId, points.Value)
	end)
	while true do
	wait(20)
	points.Value = points.value + 1
	end
end)
	

game.Players.PlayerRemoving:Connect(function(player)
	ds:SetAsync(player.UserId, player.leaderstats.point.Value)
end)

replicatedStorage.GetInfo.OnServerInvoke = function(player,item)
	return shopItems[item].Points.Value
end


replicatedStorage.CheckSale.OnServerInvoke = function(player,item)
	
	local price = shopItems[item].Points.Value
	
	if player.leaderstats.Points.Value >= price then
		
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - price
		
		local gear = shopItems[item][item]:Clone()
		gear.Parent = player.StarterGear
		
		local gear = shopItems[item][item]:Clone()
		gear.Parent = player.Backpack
		
		return true
		
	else
		
		return false	
	
	end

	
	
end

So the problem here is this one. When I’m going to buy something on the shop it substract the money from the leaderstats as I programmed, but after 20 seconds, a point is added(as I pogrammed) but instead of adding it to the amount with the money already subtracted from the purchase in the store, it is added to the amount you had before making the purchase in the store.
This is the script of the store. It only use one script that is the buy button:
local player = game:GetService(“Players”).LocalPlayer

script.Parent.MouseButton1Click:Connect(function(click)
	if player.leaderstats.Points.Value >= 400 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 400
		game.ReplicatedStorage.Library.AGodSword:Clone().Parent = player:WaitForChild("Backpack")
	end
end)

I’ve done a lot of things to solve the problem but it didnt worked anything. Do you know how to solve it? Please help me
Thanks for spending your time with this post
Spideyalvaro999

1 Like

Remote Events. You need to fire the server for when someone clicks the button and buys the item. Instead of subtracting it on the client, tell the server to take away the value through FireServer(). Then, in your server script, just have an OnServerEvent function take away how much ever you want taken away.

So at the end of the by button scrip I writte the FireServer event and on the server script I writte other thing or what? I didnt understand that las thing you told

It should look something like this

This goes in your local script. Remove the part that already subtracts it in the local script as well

RemoteEvent:FireServer(400) --The 400 is the value of the item. 

This will go in a server script

RemoteEvent.OnServerEvent:Connect(function(player, Value) --You can name the second parameter to anything you want. I just chose to name it Value, since thats how much the item costs. 
    player.leaderstats.Points.Value = player.leaderstats.Points.Value - Value
end)

And just to clarify, you will need to define the path to the remote event, and not just say RemoteEvent like I did for the example

1 Like

Thank you so much. Im going to test it and if this works, I will give you the solved comment

1 Like