Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

My leaderstats script is not working properly?

Asked by 4 years ago
Edited 4 years ago

It says line:76 attempt to index local 'PlayersFolder' (a nil value)

and also when the player leaves the folder doesnt delete or go it stays in the server

Additional Info: The stats script is in Workspace and I make the players stats folder go in ServerStorage

local DataStore = game:GetService("DataStoreService"):GetDataStore("OnePieceUltimateTreasurePreAlpha")

game.Players.PlayerAdded:connect(function(plr)
    local PlayerStats = Instance.new("Folder", game.ServerStorage)
    PlayerStats.Name = plr.Name
    local Level = Instance.new("IntValue", PlayerStats)
    Level.Name = "Level"
    Level.Value = 1
    local LevelEXP = Instance.new("IntValue", PlayerStats)
    LevelEXP.Name = "LevelEXP"
    LevelEXP.Value = 0
    local Melee = Instance.new("IntValue", PlayerStats)
    Melee.Name = "Melee"
    Melee.Value = 1
    local MeleeEXP = Instance.new("IntValue", PlayerStats)
    MeleeEXP.Name = "MeleeEXP"
    MeleeEXP.Value = 0
    local Sword = Instance.new("IntValue", PlayerStats)
    Sword.Name = "Sword"
    Sword.Value = 1
    local SwordEXP = Instance.new("IntValue", PlayerStats)
    SwordEXP.Name = "SwordEXP"
    SwordEXP.Value = 0
    local Sniper = Instance.new("IntValue", PlayerStats)
    Sniper.Name = "Sniper"
    Sniper.Value = 1
    local SniperEXP = Instance.new("IntValue", PlayerStats)
    SniperEXP.Name = "SniperEXP"
    SniperEXP.Value = 0
    local Armament = Instance.new("IntValue", PlayerStats)
    Armament.Name = "Armament"
    Armament.Value = 0
    local ArmamentEXP = Instance.new("IntValue", PlayerStats)
    ArmamentEXP.Name = "ArmamentEXP"
    ArmamentEXP.Value = 0
    local Conqueror = Instance.new("IntValue", PlayerStats)
    Conqueror.Name = "Conqueror"
    Conqueror.Value = 0
    local ConquerorEXP = Instance.new("IntValue", PlayerStats)
    ConquerorEXP.Name = "ConquerorEXP"
    ConquerorEXP.Value = 20
    local Berry = Instance.new("IntValue", PlayerStats)
    Berry.Name = "Berry"
    Berry.Value = 1


    local specialkey = "player-"..plr.userId

    local GetSaved = DataStore:GetAsync(specialkey)

    if GetSaved then
        Level.Value = GetSaved[1]
        LevelEXP.Value = GetSaved[2]
        Melee.Value = GetSaved[3]
        MeleeEXP.Value = GetSaved[4]
        Sword.Value = GetSaved[5]
        SwordEXP.Value = GetSaved[6]
        Sniper.Value = GetSaved[7]
        SniperEXP.Value = GetSaved[8]
        Armament.Value = GetSaved[9]
        ArmamentEXP.Value = GetSaved[10]
        Conqueror.Value = GetSaved[11]
        ConquerorEXP.Value = GetSaved[12]
        Berry.Value = GetSaved[13]
    else
        local ForSaving = {Level.Value, LevelEXP.Value, Melee.Value, MeleeEXP.Value, Sword.Value, SwordEXP.Value, Sniper.Value, SniperEXP.Value, Armament.Value, ArmamentEXP.Value, Conqueror.Value, ConquerorEXP.Value, Berry.Value}
        DataStore:SetAsync(specialkey, ForSaving)

    end
end)


game.Players.PlayerRemoving:connect(function(plr)
    local PlayersFolder = game.ServerStorage:FindFirstChild(plr.Name)
    local specialkey = "player-"..plr.userId
    local SaveTable = {PlayersFolder.Level.Value, PlayersFolder.LevelEXP.Value, PlayersFolder.Melee.Value, PlayersFolder.MeleeEXP.Value, PlayersFolder.Sword.Value, PlayersFolder.SwordEXP.Value, PlayersFolder.Sniper.Value, PlayersFolder.SniperEXP.Value, PlayersFolder.Armament.Value, PlayersFolder.ArmamentEXP.Value, PlayersFolder.Conqueror.Value, PlayersFolder.ConquerorEXP.Value, PlayersFolder.Berry.Value}
    DataStore:SetAsync(specialkey, SaveTable)
end)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

For starters, the reason the player's folder doesn't remove is because you never made it remove when they leave.

You'll need to add:

PlayersFolder:Destroy()

Right before end when they're leaving.

As for the main issue, no idea, sorry.

EDIT1:

Found out the issue!

Since in studio, it is still there, you need to add a wait() at the beginning of the anon. function, to show that they're not equal in times of running. I don't know any other way to explain it, but add wait() first.

Ad

Answer this question