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

[Help] leaderboard script wont work and wont run?

This simple leaderboard script won't work at all and I don't know why, please help.

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "Leaderstats"
	
	local XP = Instance.new("IntValue", leaderstats)
	XP.Name = "XP"
	XP.Value = 0
	 
end)

2 comments

0

Debug the code.

0

Never part instances like that. Take a look at this. :) https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296

5 answers

Here, the most efficient leaderstats script:

Most of the answers above are not optimized, this one will make your game run smother:

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

    local leaderstats = Instance.new("Folder") -- The folder and be anything, it can even be a beam or something!
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player -- Dont ever set the parent inside the Instance.new() function, it causes lag!

    local XP = Instance.new("IntValue")
    XP.Name = "XP" -- You dont need to set the value if its going to be 0 at the start of the game.
    XP.Parent = leaderstats

end)

I'm going to assume where you said; "script won't work and won't run" that you expected it to literally show the Roblox leaderboard GUI in the top right corner. Let's start with a couple things:

  1. Ensure that the script that is being used is a server sided Script placed in a place where the script can run; Workspace or ServerScriptService
  1. Next make sure that parent object that stores the stats is a model; roblox will only recognise models named leaderstats in the player to display leaderstats.

It should look like this:

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

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

    local XP = Instance.new("IntValue", leaderstats)
    XP.Name = "XP"
    XP.Value = 0

end)

Improvements:

This script can be improved like so:

local Players = game:GetService("Players")

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

	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Value = 0
	XP.Parent = Player
end)

Notes:

  1. Never use the second argument of Instance.new() as it can cause lag
  1. Always parent last unless you want the player to see the changes

Hope this helped!

Both of the scripts above Wont work, the “leaderstats” has to me named leaderstats or it won’t work, and the parent argument is included in Instance.new()


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

    local leaderstats = Instance.new("Folder", player) -- Instance.new("Folder",player) automatically sets the parent to “player”
    leaderstats.Name = "leaderstats" -- has to be named “leaderstats”

    local XP = Instance.new("IntValue", leaderstats) -- Use “IntValue” for whole numbers, use “NumberValue” for numbers with decimals
    XP.Name = "XP"
    XP.Value = 0

end)

1 comment

0

Dont ever set the parent inside the Instance.new() function, it causes lag!

It's not a folder, it's a model. Recently did this.

game.Players.PlayerAdded:Connect(function(p)
 local StatsParent = Instance.new("Model", p)
 StatsParent.Name = "leaderstats"

 local NewStat = Instance.new("IntValue", StatsParent)
 NewStat.Name = "XP"
end)

4 comments

0

IT DOESN'T HAVE TO BE A MODEL

0

you can have it as what ever you want, it can even be a part for gods sake

0

This makes my mind go crazy, people thinking the leaderstats container has to be a frecking model

0

lmao and some people say you did not set its parent when the main problem is, leaderstat's L is capital lol thats why it does not work. :)

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