Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[SOLVED] Leaderstats not showing any changes?

Explanation: So I am trying to make a sword shop system. When someone buys a sword, it will fire a RemoteEvent to the Server to check if they have enough Coins[currency for the game]. If they do, the server will update their leaderstats and clone the sword, they can only equip 1 sword at a time. However, the Coins value is not decreasing when the player buys a sword..

There is no error shown in the output. Output: "ClassicSword: 250 Coins"

Here's the script:

local BuyEvent = game:GetService("ReplicatedStorage"):WaitForChild("EventsFolder"):WaitForChild("BuyEvent")
BuyEvent.OnServerEvent:Connect(function(Player, BuyPrice, SwordName)
	print(SwordName..": "..BuyPrice.." Coins")
	local Coins = Player:WaitForChild("leaderstats"):WaitForChild("Coins").Value
	if not Coins then return end
	if Coins >= BuyPrice then
		for i, v in pairs(Player:WaitForChild("Backpack"):GetChildren()) do
			if Player:WaitForChild("Backpack"):GetChildren() == nil then return end
			v:Destroy()
		end
		Coins = Coins - BuyPrice -- Problem Here
		local SwordClone = game:GetService("ServerStorage"):WaitForChild(SwordName):Clone()
		SwordClone.Parent = Player:WaitForChild("Backpack")
	end
end)

Any help will be appreciated

1 answer

✓ Accepted answer

This is because you didn’t actually allocate the IntValue, instead, you declared it’s current state into a variable. This of which interprets like so:

local Coins = 50
Coins = (Coins - 50)

It is no-longer modifying the Object, but performing a redundant integer calculation. This is a classic semantic error, meaning it is a logical issue within your code rather than a problem with the instruction.

To solve this, we have to call .Value inline to actually read/write:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local EventsFolder = ReplicatedStorage:WaitForChild("EventsFolder")
local BuyEvent = EventsFolder:WaitForChild("BuyEvent")

BuyEvent.OnServerEvent:Connect(function(Player, BuyPrice, SwordName)
    local Leaderstats = Player:WaitForChild("leaderstats")
    local Coins = Leaderstats:WaitForChild("Coins")
    if (Coins.Value >= BuyPrice) then
        local Backpack = Player:WaitForChild("Backpack")
        for _,Tool in pairs(Backpack:GetChildren()) do
            Tool:Destroy()
        end
        Coins.Value = (Coins.Value - BuyPrice)
        local SwordClone = ServerStorage:WaitForChild(SwordName):Clone()
        SwordClone.Parent = Backpack
    end
end)

I also cleaned up your program a bit, I suggest taking a look at the differences to get a feel for some more efficient practices! There were about 3 redundant instructions in total.

If tis helped, don’t forget to accept this answer!

2 comments

0

Thanks man

0

No problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.