My leaderstats script isn't working

I don’t get this error message I’m very new to scripting

game.Players.PlayerAdded:Connect(function(player) local stats = Instance.new("Folder", player);stats.Name = "stats";local bux = Instance.new("NumberValue", stats);bux.Name = "bux";bux.Value = 0;local	multiplyer = Instance.new("NumberValue", stats);multiplyer.Name = "Multiplier";multiplyer.Value = 1;while task.wait(1) do player.leaderstats.Bux.Value += 1 * player.multiplier.Value end end)

But then I get this error message when I play the game:

leaderstats is not a valid member of Player "Players.spacevur"

2 Likes

It’s because “stats” is a keywords in Lua. Change the variable name to something else, like “leaderstats”


You also named the folder “stats” and not “leaderstats”

Not to mention the “stats” keyword is deprecated as well lol. Don’t even remember what it was ever used for.

I would also advise to organise your code (don’t have it on one line).

1 Like

Condensing the code here, a couple nitpicks:

  • Please do not use the Instance.new() parameter, as it takes more performance than what you’d expect it would, make sure to always set the .Parent property last when you can

  • Consider indenting your code, so that it looks proper & it’s way easier having to look at this:

local Variable = 20
local Question = "What is the Bee Movie?"

Rather than this:

local Variable = 20 local Question = "What is the Bee Movie?"
  • As someone mentioned before, you’re attempting to index something that’s non-existent within the Player Object that you’re searching for

What you typed:

local stats = Instance.new("Folder")
stats.Name = "stats"

What the script’s also trying to index/find:

while task.wait(1) do 
	player.leaderstats.Bux.Value += 1 * player.multiplier.Value
end

This alone contradicts what you defined earlier, as the script is unable to detect that name within the Player, which returns back as an error

  • ^ Adding onto this, you don’t need to keep indexing your “Just newly made Instances” from the player instance again while you can obtain them by referencing their variable instead:
while task.wait(1) do
    bux.Value += 1 * multiplyer.Value
end

This should be the fix for it here:

game.Players.PlayerAdded:Connect(function(player) 
	local stats = Instance.new("Folder")
	stats.Name = "stats"
	stats.Parent = player
	
	local bux = Instance.new("NumberValue")
	bux.Name = "bux"
	bux.Value = 0
	bux.Parent = stats
	
	local multiplyer = Instance.new("NumberValue")
	multiplyer.Name = "Multiplier"
	multiplyer.Value = 1
	multiplyer.Parent = plr
	
	while task.wait(1) do 
		bux.Value += 1 * multiplyer.Value 
	end
end)
1 Like

I think you are looking for this:


local Players = game:GetService("Players")

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

	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

It’s from here:

Didn’t even notice half of these mistakes bruh :sob:

I can’t read code on one line at all. My brain shuts down and says “nope. Ain’t reading it properly”

Yeah I agree, probably it wa one of his first topics, but at least now he knows he made a mistake an next time he’s gonna fix it

Summary

By the way I didn’t even try to read the code because the brain was just “Sorry, I need a break, see yah” :rofl::rofl::rofl:

1 Like

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