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

I tried to make a leaderstat script, but it won't even make the leaderstat folder. Why?

Asked by 4 years ago

hi. i'm trying to set up a leaderstat script, but for some reason, and as stated in the title, the leaderstat folder itself isn't even being made at all. i've only gotten it to work once, and i have no idea why it's acting up like this. if someone could explain this oddity to me, or point out any mistakes in the code that might be causing it that isn't just "this is deprecated, don't use it", i'd appreciate it. some extra information: i've put it both in workspace and serverscriptservice. neither locations worked.

code:

wait()
local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("LevelData")

game.Players.PlayerAdded:connect(function(player)
    print("a")
    local leader = Instance.new("Folder")
    leader.Parent = player
    leader.Name = "leaderstats"
    print("stat")
    local lvl = Instance.new("DoubleConstrainedValue",leader) --i know this is deprecated, but i still prefer using it.
    lvl.Name = "Level"
    lvl.MaxValue = 50
    lvl.MinValue = 1
    lvl.Value = ds1:GetAsync(player.UserId) or 1
    ds1:SetAsync(player.UserId, lvl.Value)
    lvl.Changed:connect(function()
        ds1:SetAsync(player.UserId, lvl.Value)
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    ds1:SetAsync(player.UserId, player:WaitForChild("leaderstats"):WaitForChild("Level").Value)
end)
0
I see no issue lol u should try removing what’s deprecated(idk what’s deprecated in the code lol) St_vnC 330 — 4y

1 answer

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

Ok, lets try it!

First Step : First of all we need to know how to acces to the Player. And create values inside a folder named "leaderstats".

Example...

--> Code <--

game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats" -->You can't change this!
end

--> We already added a folder to the player!

We already added a folder, now we can add some values so we can use it like leaderstats. Can be IntValues or StringValues [For ranks].

Second Step : We can learn how to use a DataStore, but... How we acces to the DataStore of a player?

Easy!

local ds = game:GetService("DataStoreService")
local moneyDs = ds:GetDataStore("moneyData")

See? Easy, right? We don't need more than 2 lines of code to acces to the DataStore, but now... How we can start creating a correct leaderstats script?

Easy!

Third Step : Enjoy coding! You aways have to enjoy what are you doing! Play some music while you script or even play a game before to be relaxed.

Lets get into the code!

--> Locals <--

local ds = game:GetService("DataStoreService")
local moneyDs = ds:GetDataStore("moneyData")

--> Code <--

--> Player added
game.Players.PlayerAdded:Connect(function(plr)
        local folder = Instance.new("Folder", plr)
        folder.Name = "leaderstats" -->You can't change this!

        local currency = Instance.new("IntValue", folder)
        currency.Name = "Money"
        currency.Value = moneyDs:GetAsync(plr.UserId) or 0 -->If the player doesn't have data   already saved he will take the value of 0
        currency.Changed:Connect(function()
            moneyDs:SetAsync(plr.UserId, currency.Value)
    end)
        --> And that's all!
end)

--> Player removed or kicked 
game.Players.PlayerRemoving:Connect(function(plr)
        moneyDs:SetAsync(plr.UserId, plr.leaderstats.Money)
end)

--> Error Message <--

local warnMessage = "The code is incorrect!"
pcall(warnMessage)

That's how we make a leaderstats with DataStore and easily.

To make a Leveling System [Easy]...

--> Locals <--

local ds1 = game:GetService("DataStoreService")
local levelDs = ds1:GetDataStore("levelData")
local ds2 = game:GetService("DataStoreService")
local xpDs = ds2:GetDataStore("xpData")
local ds3 = game:GetService("DataStoreService")
local rxpDs = ds3:GetDataStore("rpxData")

--> Code <--

--> Player added
game.Players.PlayerAdded:Connect(function(plr)
        local folder = Instance.new("Folder", plr)
        folder.Name = "leaderstats" -->You can't change this!

        local level = Instance.new("IntValue", folder)
        level.Name = "Level"
        level.Value = levelDs:GetAsync(plr.UserId) or 1 -->If the player doesn't have data already saved he will take the value of 0
        level.Changed:Connect(function()
            levelDs:SetAsync(plr.UserId, level.Value)
        end)

        local xp = Instance.new("IntValue", folder) --> xp
        xp.Name = "XP"
        xp.Value = xpDs:GetAsync(plr.UserId) or 0
        xp.Changed:Connect(function()
            xpDs:SetAsync(plr.UserId, xp.Value)
    end)

        local rxp = Instance.new("IntValue", folder) --> Required xp
    rxp.Name = "RequiredXP"
        rpx.Value = rxpDs:GetAsync(plr.UserId) or 1000
        rpx.Changed:Connect(function()
            rpx:SetAsync(plr.UserId, rpx.Value)
        end)

        if xp.Value >= rxp.Value then
            xp.Value = xp.Value - rxp.Value
            level.Value = level.Value + 1
            rxp.Value = rxp.Value + 2000
        elseif xp.Value <= rpx.Value then
            print("You don't have enough experience! Get more!")
     end
        --> And that's all!
end)

--> Player removed or kicked 
game.Players.PlayerRemoving:Connect(function(plr)
     levelDs:SetAsync(plr.UserId, plr.leaderstats.Level.Value)
     xpDs:SetAsync(plr.UserId, plr.leaderstats.XP.Value)
     rxpDs:SetAsync(plr.UserId, plr.leaderstats.RequiredXP.Value)
end)

--> Error Message <--

local warnMessage = "The code is incorrect!"
pcall(warnMessage)

That's all you need to know.

I hope my answer already solve your question. If it did, please accept my answer. That will help me a lot!

Keep scripting!

Ad

Answer this question