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

Unable to add a leaderstat?

Asked by 5 years ago
Edited 5 years ago

Hi there!

I've been trying for hours to add the leaderstat "Rebirths". I've tried everything, and it seems I cannot possibly fix it or add it into leaderstats, I'm not the best scripter, but I've tried.

local storage = require(script:WaitForChild("Storage"))

for i,v in pairs(game.Players:GetPlayers()) do
    if storage.GetData(v.UserId) == nil then
        storage.LoadData(v.UserId)
    end
end

HeightBase = 6
game.Players.PlayerAdded:connect(function(player)
    data = nil
    repeat 
        pcall(function() data = storage.LoadData(player.UserId) end)
        wait() 
    until data
    spawn(function()
        repeat wait() until player.Character
        if data.Fuel[1] <= 100 then
            game.ReplicatedStorage.Events.Tutorial:FireClient(player)
        end
    end)
    local F = Instance.new('Folder'); F.Name = 'leaderstats'; F.Parent = player
    local LastRec = math.floor(data.Height / 10) * 10
    local Extra = player:IsInGroup(4301807) and 25 or 0
    for i,v in pairs(data) do
        if i == 'Fuel' or i == 'Height' or i == 'Speed' or i == 'Coins' or i == "Rebirths" then
            local new = Instance.new("NumberValue")
            new.Name = i; new.Value = i == 'Fuel' and (v[1] + Extra) or v
            new.Parent = F
            spawn(function()

                while wait() do
                    if player and player.Character then
                        data = storage.GetData(player.UserId)
                        new.Value = i == 'Fuel' and data[i][1] + Extra or data[i]
                    end
                end
            end)
        end
    end
    spawn(function()
        while wait() do
            if player.Character and player.Character:FindFirstChild('HumanoidRootPart') and player.leaderstats:FindFirstChild('Height') then
                local Height = math.floor(player.Character.HumanoidRootPart.Position.Y - HeightBase)
                if player.leaderstats.Height.Value < Height then
                    data.Height = Height
                    local new = data.Height
                    if new % 10 == 0 then
                        local PPAwrd = (new - LastRec) / 10
                        spawn(function()
                            pcall(function() game:GetService("PointsService"):AwardPoints(player.UserId, PPAwrd);LastRec = new end)
                        end)
                    end
                end
            end
        end
    end)
    wait()
    data.Fuel[2] = false
end)

game.Players.PlayerRemoving:connect(function(player)
    local Data = storage.GetData(player.UserId)
    storage.UpdateData(Data)
    storage.SaveData(player.UserId)
end)

The "Storage" script:

local storage = {}
local settings = require(script.Parent:WaitForChild("Settings"))

local DataStoreService = game:GetService("DataStoreService")
local Datastore = DataStoreService:GetDataStore(settings.Key..settings.Version)

function getS(player, cmd)
    local Stats = {
        name = player.Name;
        userId = player.UserId;
        Fuel = {50,false};
        Height = 0;
        Speed = 1;
        Walkspeed = 16;
        JumpPower = 50;
        FuelClick = 1;
        Regen = 1;
        Coins = 0;
        Rebirths = 0;
        Trails ={ {Name = 'Fire'}};
        EquippedT = 'Fire';
        }
    return Stats
end

local function createData(id, cmd)
    local player = nil
    for i,v in pairs(game.Players:GetPlayers()) do
        if v.UserId == id then
            player = v
        end
    end

    local tab = getS(player, cmd)

    local success, save = pcall(function()
        Datastore:SetAsync(tab.userId,tab)  
    end)
    if not success then
        warn("An error occurred with saving " ..id.. " data: "..save)
    else
        warn("Successfully created " ..id.. " data: "..game:GetService("HttpService"):JSONEncode(tab))
    end
    return tab
end

local module = {
    GetData = function(id)
        local data = nil
        for i,v in pairs(storage) do
            if v.userId == id then
                data = v
            end
        end

        return data
    end,

    UpdateData = function(tab)
        for i,v in pairs(storage) do
            if v.userId == tab.userId then
                table.remove(storage,i)
            end
        end

        table.insert(storage,tab)
    end,    
    SaveData = function(id)
        local data = nil
        for i,v in pairs(storage) do
            if v.userId == id then
                data = v
                table.remove(storage,i)
            end
        end
        local success, save = pcall(function()
            --Datastore:SetAsync(data.userId,data)  
            Datastore:UpdateAsync(data.userId, function(oldData)
                local newValue = data
                return newValue
            end)
        end)
        if not success then
            warn("An error occurred with saving " ..id.. " data: "..save)
        else
            warn("Successfully saved " ..id.. " data: "..game:GetService("HttpService"):JSONEncode(data))
        end
    end,

    LoadData = function(id)
        local data = nil
        data = Datastore:GetAsync(id)
        local cmd = 'update'
        if not data then
            data = createData(id, cmd)
        end
        table.insert(storage,data)
        return data
    end,

    DeleteData = function(id)
        local data = nil
        data = Datastore:RemoveAsync(id)


        return data
    end
}

return module
0
Do indent your code. Thanks. User#19524 175 — 5y
0
The code has been indented. Sorry. MelonRBLX 0 — 5y
0
make sure its not a local script. ZapherosX 43 — 5y

Answer this question