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

RPG leaderboard not working?

I was following a tutorial that required making a leaderboard, however the leaderboard is not showing up in game when I test in roblox studio. Can anyone help? Here is the script.

function onXPChanged(player, XP, level)
	if XP.Value>=level.Value * 25 then
		XP.Value = XP.Value - level.Value * 25
		level.Value = level.Value + 1
	end
end

function onLevelUp(player, XP, level)
	print("LEVELED UP")
end

function onPlayerRespawned(player)
	wait(0.5)
	player.Character.Humanoid.Health    = player.leaderstats.Level + 2.5 + .5
	player.Character.Humanoid.MaxHealth = player.leaderstats.Level + 2.5 + .5
end

function onPlayerEntered(newPlayer)
	local stats = Instance.new("IntValue")
	stats.Name  = "leaderstats"
	
	local xp = Instance.new("IntValue")
	xp.Name  = "EXP"
	xp.Value = 0
	
	local level = Instance.new("IntValue")
	level.Name  = "Level"
	level.Value = 1
	
	local gold = Instance.new("IntValue")
	gold.Name  = "Gold"
	gold.Value = 0
	
	xp.Parent    = stats
	level.Parent = stats
	gold.Parent  = stats
	
	stats.Parent = newPlayer
	
	xp.Changed:connect(function() onXPChanged(newPlayer, xp, level) end)
	level.Changed:connect(function() onLevelUp(newPlayer, xp, level) end)
	
	newPlayer.Changed:connect(function(property)
		if (property == "Character") then
			onPlayerRespawned(newPlayer)
		end
	end)
end

1 answer

✓ Accepted answer

The problem is quite simple to understand.

SOLUTION:

In the code, you have functions developed, but you never call them, so the script will basically do nothing until the functions are called.

So what you want to do is add a line of code that connects the onPlayerEntered function to the PlayerAdded event that fires when a player joins.

At the end of the script we'll connect the PlayerAdded event to the onPlayerEntered function..

game.Players.PlayerAdded:connect(onPlayerEntered)

EXTRA:

Also, for efficiency, and the sake of saving lines of code, you can write the onPlayerEntered function with the connection on the same line, as shown below.

game.Players.PlayerAdded:connect(function()
     -- Function code here (The contents of the onPlayerEntered function)
end)

For further ":Connect" details, see this wiki page.. http://wiki.roblox.com/index.php?title=RBXScriptSignal#Methods

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