What Am I Doing Wrong? (Leaderstats Question)

I am trying to decrease the value of a player’s fat, but the script just seems to fail and not do anything, even the output has no response on it, besides “leaderstats is not a valid member of player”, but I do not know how to solve this.

Here are some scripts that connect to this

Food Cloning Script:

 game.Players.PlayerAdded:Connect(function(player)
		local Fat = player.leaderstats.Fat
    game.ReplicatedStorage.Burger.OnServerEvent:Connect(function()
    	if Fat >= 2000 then
    	local Burger = game.ServerStorage.Tools.Burger:Clone()
    	Burger.Parent = player.StarterGear
    	Burger.Parent = player.Backpack
    	Fat = Fat - 2000
    	end
    end)
-- more code here

The Fire:

script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.Burger:FireServer()
end)

Part Of The DataStore:

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local Fat = Instance.new("NumberValue")
Fat.Name = "Fat"
Fat.Parent = leaderstats

local rebirth = Instance.new("IntValue")
rebirth.Name = "Rebirths"
rebirth.Parent = leaderstats

local Fatbux = Instance.new("IntValue")
Fatbux.Name = "Fatbux"
Fatbux.Parent = player
--rest of the code here

`
Tell me if I am wrong, but I am pretty sure that when you set the PlayerAdded Event it refers to the local player that has joined right? And yes I did test it with more then 2000 fat. If anyone could help me on this that would be awesome thanks!:grin:

This is a common scripting problem.

.Value is the number of the value at the time. It is static and does not change.

Instead of indexing the Value, index the actual object

local Fat = player.leaderstats.Fat
if Fat.Value >= 2000 then
 -- do stuff
end
1 Like

OOF, I did not even realize that, silly me, thanks and I will let you know if something else goes wrong.

I am checking it first bfore I solve it, also there is another problem that will probably need resolving in it remember:

Try parenting leaderstats to player once you’ve created all your values.

Also, does the leaderstats appear in the playerlist, and inside the player in studio?

yes that is why I am confused. (extra words so I can post this)

Can you send me the code snippet where the error is occuring?

sorry for the long wait, here is the code it refers too:

game.Players.PlayerAdded:Connect(function(player)
	local Fat = player.leaderstats.Fat
--rest of the code here

It has also been having problems with other buying scripts.

Are you trying to run local Fat = player.leaderstats.Fat before you create leaderstats?

If both run immediately on PlayerAdded, it’s a recipe for disaster. I recommend this:

local Fat = player:WaitForChild("leaderstats"):WaitForChild("Fat")

oh that could be the problem, let me go check it out and comment down when I am back, thanks

Okay, I see many things wrong with this. The first: “Fat >= 2000,” and the other is the last line of the script.

 game.Players.PlayerAdded:Connect(function(player)
		local Fat.Value = player.leaderstats.Fat
    game.ReplicatedStorage.Burger.OnServerEvent:Connect(function()
    	if Fat >= 2000 then
    	local Burger = game.ServerStorage.Tools.Burger:Clone()
    	Burger.Parent = player.StarterGear
    	Burger.Parent = player.Backpack
    	Fat.Value = Fat.Value - 2000
    	end
    end)

I changed the last line of the script to Fat.Value = Fat.Value - 2000, and the few other lines to if Fat.Value >=2000. This should work now.

Hope this helps.

– Drinkinix :smile:

Yes I have already figured that out, but thanks for the help.:grin:

yep that was the problem it was defining “Fat” before it was created thanks.:grin: