Leaderstats.Value not being subtracted from

  1. What do you want to achieve?

When the player presses the GUI button, if they have enough coins, they can purchase the item, and the cost of the item is then subtracted from their coin value.

How I went about this is… firing a remote event if conditions for purchasing an item are met, then it takes the “itemcost” data with the fire and is supposed to remove the value from the player’s Coins.Value

  1. What is the issue?

The coins are not being removed from the player’s Coins.Value.

https://vimeo.com/814343705

  1. What solutions have you tried so far?

I tried using print(itemcost) to see if it’s nil, but it isn’t nil and has the correct value. Not sure what I’m doing wrong here ._.

Part of the ClientScript w/in GUI

	slot.ImageButton.MouseButton1Click:Connect(function(player)
			-- Get the first child of the ViewportFrame.Model and set the itemName variable to its Name property
			local itemModel = slot.ViewportFrame.Model:GetChildren()[1]
			local itemName = itemModel.Name
					
					local player = game.Players.LocalPlayer
					print(player) -- This will print the value of player in the console
					print(player.leaderstats)
					
					-- Check if the slot has an item equipped
			local shopitem = objDisplay:GetChildren()[1]
			local itemcost = shopitem:FindFirstChild("Cost").Value
					
					if equipped == true and itemcost > player.leaderstats:FindFirstChild("Coins").Value then
						
						player.PlayerGui.GameInventory.DeleteOneMsg.Visible = true
						player.PlayerGui.GameInventory.DeleteOneMsg.Msg.Text = "You don't have enough coins."
						--player.PlayerGui.GameInventory.Frame.Visible = true
						wait(2)
						player.PlayerGui.GameInventory.DeleteOneMsg.Visible = false
						
					else
					
				if equipped == true and itemcost <= player.leaderstats:FindFirstChild("Coins").Value then

				local event = game.ReplicatedStorage.InvEvents.PickUp
				local purchaseitem = game.ReplicatedStorage.ShopEvents.Purchase	
				local item = itemName
				local firstChild = itemModel
				local itemdescription = firstChild:FindFirstChild("Desc").Value
				event:FireServer(item, itemdescription)
				purchaseitem:FireServer(itemcost)	

PurchaseScript within ServerScriptService

game.ReplicatedStorage.ShopEvents.Purchase.OnServerEvent:Connect(function(player, itemcost)
	print (itemcost)
	if player.leaderstats.Coins.Value == itemcost then
		player.leaderstats.Coins.Value -= itemcost
		
	else
		
		player.PlayerGui.GameInventory.DeleteOneMsg.Visible = true
		player.PlayerGui.GameInventory.DeleteOneMsg.Msg.Text = "You don't have enough coins."
		--player.PlayerGui.GameInventory.Frame.Visible = true
		wait(2)
		player.PlayerGui.GameInventory.DeleteOneMsg.Visible = false
		
	end
end)

Probably because it will only work if the amount of coins you have is the exact same price as the item to buy.

Use this instead:

player.leaderstats.Coins.Value >= itemcost

This basically means if you have more coins as the cost of the item, or the same amount.

1 Like

Thank you for your extremely helpful reply!

I changed that line in the PurchaseScript as you explained; however, it is still not working. In the video I shared, perhaps you can see if I’m doing something wrong?

shopissues.mov on Vimeo

1 Like

I don’t think you can edit GUI’s on the serverside.

1 Like

I’m not changing anything with the GUI’s… at least, I don’t think I am?

I’m wondering if it’s an issue with the fact I’m changing the Leaderstats.Coins.Value when I’m running the game as opposed to actually “earning” the coins through gameplay?

Do you have any other counters of coins aside from leaderstats?

Also yeah, you are changing GUI.

1 Like

If you could create a duplicate of the game and give me access, I could fix this.

1 Like

Oh yes! When I change the Coins.Value, the coins GUI is changing—thank you for pointing that out!

I can share my leaderstats ServerScript & the LocalScript within the coins GUI if that could help:

CoinsDataStore ServerScript
*Basically saves the amount of coins the player has

local statList = {
	{Name = "Coins", Type = "IntValue"};

}

local Players = game:GetService("Players")
local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

local function SavePlayer(player)
	local dataToSave = {};

	for _, stat in pairs(player.leaderstats:GetChildren()) do
		dataToSave[stat.Name] = stat.Value
	end

	local success, err = pcall(function()
		Datastore:SetAsync(player.UserId, dataToSave)
	end)
	if not success then
		warn("[!] Error saving data for " .. player.Name)
	end
end

local function NewStat(statType, statName, statParent)
	local new = Instance.new(statType)
	new.Name = statName
	new.Parent = statParent
	return new
end

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	for _, statInfo in pairs(statList) do
		local success, err = pcall(function()
			NewStat(statInfo.Type, statInfo.Name, leaderstats)
		end)
		if not success then
			warn("Error creating stat " .. tostring(statInfo.Name) .. ": " .. tostring(err))
		end
	end

	local data; local success, err = pcall(function()
		data = Datastore:GetAsync(player.UserId)
	end)

	if success and data then
		for statName, statValue in pairs(data) do
			if leaderstats:FindFirstChild(statName) then
				leaderstats[statName].Value = statValue
			end
		end
	elseif err then
		warn("[!] Error loading data for " .. player.Name .. ": " .. tostring(err))
	end
end)

Players.PlayerRemoving:Connect(SavePlayer)

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		SavePlayer(player)
	end
end)

LocalScript in CoinGUI

local player = game.Players.LocalPlayer
local text = script.Parent

while true do
	text.Text = "Coins: ".. player.leaderstats:WaitForChild("Coins").Value
	wait(1)
	
end

Sure, I can do that now, I’ll send you a friend request :smiling_face:

1 Like

if you change your coins value from the client it won’t show on the server when you check it

1 Like

I sent you a request, can you accept?

1 Like

I accepted the request and gave you access to the game!

That’s what I was thinking… how do you propose I go about fixing this issue?

Im chatting you in the roblox studio chat

1 Like

Not sure how to access the chat feature, hold on :sweat_smile:

try to go to view → chat box

[][][][]

1 Like

do you see the view → chat box?

1 Like

make the default coin value to 100 in the server script that makes the Leaderstats to test it

1 Like

i think because you change the coins value in the explorer and it only changes for the client because it was client side window and the server still sees 0 coins but if you switch to server side by click here
image
then you can change the coin value in explorer for server and it will probably fix the problem

1 Like

The problem is that you are changing the money on the client side, the current one appears at the top (client or server) and you put it on the server and that’s when you change the money, then you go back to the client and test it

1 Like