How to make the script take away money everytime I buy an item

Hi,

I have made a simulator shop but I was struggling how to make it only eqiup only one item.
Which I have done now, but now it doesn’t take away the money when I buy the item.
This is the code when buying an item:

local Sets = require(game.ServerScriptService:WaitForChild("Settings"))
local Datas = game:GetService("DataStoreService")
game.ReplicatedStorage.ShopBuy.OnServerEvent:Connect(function(player,page)
	local character = player.Character
	local shop = game.Workspace.BackpackShop
	local coins = player:WaitForChild("leaderstats")[Sets.Currency]
		if coins.Value >= shop.Parts:FindFirstChild("Part"..page).ItemPrice.Value then
	player.BackpackSave:FindFirstChild("Part"..page).Value = true
	for i,v in pairs(game.Workspace.BackpackShop.Parts:GetChildren()) do
		local data = Datas:GetDataStore(v.Name)
		data:SetAsync(player.UserId, player.BackpackSave[v.Name].Value)
	end
	coins.Value = coins.Value - shop.Parts:FindFirstChild("Part"..page).ItemPrice.Value
	end
end)

I cant really find out why it is not taking off money when I buy an item.
Help needed.

Thanks for reading!

1 Like

I’m a bit confused by your method… The datastore you use for the player items is the player name, but if it changes, it wont load at all and they’ll lose all their data. You should save all data to one data store but all the players to a unique key (userId)

Anyways, on to the actual problem. Are you sure the player currency is above the price? Do they have enough to be buying the item? If you want them to have enough for testing, you have to change the value on the server. Not the client. Because it checks on the server and if you change it on the client side, the FE barrier stops it from going through and the server seeing it. Does that make sense? Reply if this isn’t the case

Please add object hierarchy as it is related to this problem. If there is a problem, it is likely in the hierarchy.

You can’t really throw a script at us and expect us to fix it for you. There’s not enough information on this thread for us to do anything. There are no screenshots of your object hierarchy or output, for example.

I literally can’t see a problem.

value = value - increment

1 Like

I think you should take away money before set player data

local Sets = require(game.ServerScriptService:WaitForChild("Settings"))
local Datas = game:GetService("DataStoreService")
game.ReplicatedStorage.ShopBuy.OnServerEvent:Connect(function(player,page)
	local character = player.Character
	local shop = game.Workspace.BackpackShop
	local coins = player:WaitForChild("leaderstats")[Sets.Currency]
		if coins.Value >= shop.Parts:FindFirstChild("Part"..page).ItemPrice.Value then
coins.Value = coins.Value - shop.Parts["Part"..page].ItemPrice.Value	player.BackpackSave:FindFirstChild("Part"..page).Value = true
	for i,v in pairs(game.Workspace.BackpackShop.Parts:GetChildren()) do
		local data = Datas:GetDataStore(v.Name)
		data:SetAsync(player.UserId, player.BackpackSave[v.Name].Value)
	end
	end
end)
player.BackpackSave:FindFirstChild("Part"..page).Value = true 
for i,v in pairs(game.Workspace.BackpackShop.Parts:GetChildren()) do
 local data = Datas:GetDataStore(v.Name) 
data:SetAsync(player.UserId, player.BackpackSave[v.Name].Value) 
end 
coins.Value = coins.Value shop.Parts:FindFirstChild("Part"..page).ItemPrice.Value end end)

The fact that you’re saving every peice of data oneby one and having it wverwrite is very, very dangerous. Not only can DataStores be used once every few seconds, but the choice to set the data store so often will literally overwrite the data, meaning only the last thing in the loop would save

1 Like