Leaderstats object value always 0

I have “Gold” as an object in my leaderstats, however it’s always 0 when I read from it, here’s the code:

image

local players = game:GetService("Players")

function onTouched(hit)
	if game.Players:FindFirstChild(hit.parent.name) then
		local humanoid = hit.parent:FindFirstChild("Humanoid")
		if humanoid then
			local player = players:GetPlayerFromCharacter(hit.parent)
			local leaderstats = player:FindFirstChild("leaderstats")
			local woodStats = leaderstats:FindFirstChild("Wood")
			local goldStats = leaderstats:FindFirstChild("Gold")
			
			warn(goldStats.Value)
			--warn(player)
			if goldStats.Value >= 250 then
				goldStats.Value -= 250
				humanoid.JumpPower += humanoid.JumpPower * 10/100
			end		
		end
	end
end

script.Parent.Touched:Connect(onTouched)



I’m changing the value of gold object here on gui button click:

local players = game:GetService("Players")
local player = players.LocalPlayer
local screenGui = player.PlayerGui:WaitForChild("ScreenGui")
local goldButton = screenGui:WaitForChild("TestGold")
local jumpPowerButton = screenGui:WaitForChild("BuyJumpHeight")

local humanoid = players.LocalPlayer.Character.Humanoid

local leaderstats = player:FindFirstChild("leaderstats")
local woodStats = leaderstats:FindFirstChild("wood")
local goldStats = leaderstats:FindFirstChild("Gold")


goldButton.Activated:Connect(function()
	print("Added gold")
	goldStats.Value += 525
	warn(goldStats.Name)
end)

jumpPowerButton.Activated:Connect(function()
	print("Jump power added")
	if goldStats.Value >= 250 then
		goldStats.Value -= 250
		humanoid.JumpPower += humanoid.JumpPower * 10/100
	end
end)

is it possible that im writing in a place and reading from a completely different place?

You are changing the gold on the client. The server doesn’t see this change. What you need to do is fire a remote from the client to the server → on the server listen for said remote and update the players gold.

Oh that was completely out of my mind, thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.