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

Leaderboard script won't work?

Here's the Script :

local Part = script.Parent
local LB = Instance.new("IntValue")
local Score = Instance.new("IntValue")
function onEnter(Player)
	LB.Name = "Leader Stats"
	Score.Name = "Score"
	Score.Value = 0
	LB.Parent = Player
	Score.Parent = LB
end
function onTouch(Brick)
	local Plr =
	Brick.Parent:findFirstChild("Humanoid")
	if (Plr ~= nil)then
		local Loc = 
		game:GetService('Players'):GetPlayerFromCharacter(Plr.Parent)
		local Part = Loc.LB.Score
		Score.Value = Score.Value + 1
	end
end
game.Players.ChildAdded:connect(onEnter)
Part.Touched:connect(onTouch)

1 answer

✓ Accepted answer

So, I see a couple of problems with your script. To create a leaderstat, you first need to have an IntValue named "leaderstats"! After that, any leaderstat you want would have to have it as a descendant. Also, you set the IntValues outside of the functions. You'd need to create a new one each time a player is added!!! Lastly, it's recommended you use the PlayerAdded event, in case something happens to go to the Players service. With that being said, allow me to fix up your script to be a bit more organized:

Part = script.Parent -- Doesn't need to be local, since it's not within a function

game.Players.PlayerAdded:connect(function(Player)
	local LB = Instance.new("IntValue", Player)
	LB.Name = "leaderstats"
	local Score = Instance.new("IntValue", LB)
	Score.Name = "Score"
	Score.Value = 0 -- By default, it's 0. This line isn't necessary
end)

Part.Touched:connect(function(hit)
	if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then -- Checks if it is a player
		local Loc = game.Players:GetPlayerFromCharacter(hit.Parent) 
		Loc.leaderstats.Score.Value = Loc.leaderstats.Score.Value + 1 -- Since "LB" was a variable in another function, it's only available for use in that function, unless you define it again in this one.
		-- Part = Loc.leaderstats.Score
		-- I was confused by the above line. What are you trying to set?
	end
end)

If you have any questions, be sure to comment!

2 comments

0

I have no idea xD, Thanks!

0

Lol, no problem!

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