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

Leaderboard Problem

Hello! I am trying to make a script that gives you a point when you are between 100 and -5 on the y axis. There is no error in the output, but I still dont get any points. The only scoreboard objectives are Points and High Score. Here is the code I am using.

while wait(2.1) do
if Workspace.Running.Value == true then
for _,Player in pairs(game.Players:GetPlayers()) do
		for i,v in pairs(Workspace:GetChildren()) do
			if v.Name == Player.Character then
				player = v 		
		if Player:FindFirstChild("leaderstats") then
			leaderstats = Player.leaderstats
			points = leaderstats:FindFirstChild("Points")
			highscore = leaderstats:FindFirstChild("High Score")
			if player.Torso.Position.y < 100 and player.Torso.Position.y > -5 then
            points.Value = points.Value + 1
			if points.Value > highscore.Value then
			highscore.Value = points.Value
end end end end end end end end

Hopefully you can help!

1 answer

✓ Accepted answer

Your problem is on line 5 of that code. Player.Character is an Instance and v.Name is a string, so that if statement will never be true.

As well, there's no reason to nest the loops like. It's more efficient to just access the Player's Character directly.

Here is your code, modified to work better:

while wait(2.1) do
	if Workspace.Running.Value == true then
		for _,Player in pairs(game.Players:GetPlayers()) do
			local player = Player.Character
			if player then
				leaderstats = Player:WaitForChild("leaderstats")
				points = leaderstats:WaitForChild("Points")
				highscore = leaderstats:WaitForChild("High Score")
				if player.Torso.Position.y < 100 and player.Torso.Position.y > -5 then
					points.Value = points.Value + 1
					if points.Value > highscore.Value then
						highscore.Value = points.Value
					end
				end
			end
		end
	end
end

Quick question, why do you work the ends into one line like that? It detracts from the code's readability.

3 comments

0

I forgot that you could do that... :P Thanks though! And I don't really know why I did all those ends...

0

The ends were necessary, I was just asking why they were all on a single line. :P

0

yeah, thats what I meant. I don't know why I put all those ends on one line

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