Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Leaderboard script won't work?

Asked by
KenzaXI 166
9 years ago

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

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

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!

0
I have no idea xD, Thanks! KenzaXI 166 — 9y
0
Lol, no problem! Shawnyg 4330 — 9y
Ad

Answer this question