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

How I can save my XP and LVL in this script ? [closed]

Asked by 4 years ago
Edited by M39a9am3R 4 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.
local Properties = Game.Workspace.GAME_PROPERTIES

function onXPChanged(player, XP, level) 
    if string.lower(Properties.XPType.Value) == "linear" then --Linear (1, 2, 3, 4, 5, etc)(High level cap recommended.)
        if XP.Value>= Properties.XPPerLevel.Value * (level.Value) + Properties.AdditionalXPNeeded.Value and level.Value < Properties.LevelCap.Value then 
            XP.Value = XP.Value - (Properties.XPPerLevel.Value*(level.Value) + Properties.AdditionalXPNeeded.Value) 
            level.Value = level.Value + 1 
        end 
    elseif string.lower(Properties.XPType.Value) == "square" then --Square (1, 4, 9, 16, 25, etc)(Medium level cap recommended.)
        if XP.Value>= Properties.XPPerLevel.Value * (level.Value * level.Value) + Properties.AdditionalXPNeeded.Value and level.Value < Properties.LevelCap.Value then 
            XP.Value = XP.Value - (Properties.XPPerLevel.Value * (level.Value * level.Value) + Properties.AdditionalXPNeeded.Value)
            level.Value = level.Value + 1 
        end
    elseif string.lower(Properties.XPType.Value) == "exponent" then --Exponent (1, 2, 4, 8, 16, etc)(Low level cap recommended. Exponent rate dictated by XPPerLevel divided by 100.)
        if XP.Value>= math.pow(Properties.XPExponentMagnitude.Value/100, level.Value-1) * Properties.XPPerLevel.Value + Properties.AdditionalXPNeeded.Value and level.Value < Properties.LevelCap.Value then 
            XP.Value = XP.Value - math.pow(Properties.XPExponentMagnitude.Value/100, level.Value-1) * Properties.XPPerLevel.Value + Properties.AdditionalXPNeeded.Value
            level.Value = level.Value + 1 
        end         
    end
end 

function onLevelUp(player, XP, level) 
    if player.Character~=nil then 
        for i = 1,5 do 
            local fireworks = Instance.new("Part") 
            fireworks.Shape = 0 
            fireworks.formFactor = "Symmetric" 
            fireworks.Size = Vector3.new(1,1,1) 
            fireworks.BrickColor = BrickColor.Random() 
            fireworks.CFrame = player.Character.Head.CFrame + Vector3.new(0,2,0) 
            fireworks.Parent = game.Workspace 
            game:GetService("Debris"):AddItem(fireworks, 2) 
            fireworks.Velocity = Vector3.new(math.random(-30,30),math.random(-30,30),math.random(-30,30)) 
        end 
    end 
    local m = Instance.new("Hint") 
    m.Parent = game.Workspace 
    m.Text = player.Name .. " has gained a level!" 
    wait(5) 
    m.Parent = nil 
end 

function SaveData(Player)
    if Player.DataReady == false then
        warn("Could not save, DataReady is false.")
        return
    end
    --Stats
    local Stats = Player.leaderstats:GetChildren()
    for i = 1, #Stats do
        if Stats[i]:IsA("IntValue") or Stats[i]:IsA("NumberValue") then
            Player:SaveNumber(Stats[i].Name, Stats[i].Value)
        elseif Stats[i]:IsA("BoolValue") then
            Player:SaveBoolean(Stats[i].Name, Stats[i].Value)
        elseif Stats[i]:IsA("StringValue") then
            Player:SaveString(Stats[i].Name, Stats[i].Value)
        end
    end
    --Attributes
    local Attributes = Player.attributes:GetChildren()
    for i = 1, #Attributes do
        Player:SaveNumber(Attributes[i].Name, Attributes[i].Value)
    end
    --Armors
    local Armors = Player.UnlockedArmor:GetChildren()
    for i = 1, #Armors do
        if Armors[i]:IsA("IntValue") or Armors[i]:IsA("NumberValue") then
            Player:SaveNumber(Armors[i].Name, Armors[i].Value)
        elseif Armors[i]:IsA("BoolValue") then
            Player:SaveBoolean(Armors[i].Name, Armors[i].Value)
        elseif Armors[i]:IsA("StringValue") then
            Player:SaveString(Armors[i].Name, Armors[i].Value)
        end
    end
    print("Data has been saved successfully for " .. Player.Name .. "!")
end

function LoadData(Player)
    if Player.DataReady == false then
        warn("Could not load, DataReady is false.")
        return
    end
    --Stats
    local Stats = Player.leaderstats:GetChildren()
    for i = 1, #Stats do
        if Stats[i]:IsA("IntValue") or Stats[i]:IsA("NumberValue") then
            local temp = Player:LoadNumber(Stats[i].Name)
            if temp >= 0 then --Prevents the loading of negative stats.
                Stats[i].Value = temp
            end
        elseif Stats[i]:IsA("BoolValue") then
            local temp = Player:LoadBoolean(Stats[i].Name)
            Stats[i].Value = temp
        elseif Stats[i]:IsA("StringValue") then
            local temp = Player:LoadString(Stats[i].Name)
            Stats[i].Value = temp
        end
    end
    --Attributes
    local Attributes = Player.attributes:GetChildren()
    for i = 1, #Attributes do
        local temp = Player:LoadNumber(Attributes[i].Name)
        if temp >= 0 then
            Attributes[i].Value = temp
        end
    end
    --Armors
    local Armors = Player.UnlockedArmor:GetChildren()
    for i = 1, #Armors do
        if Armors[i]:IsA("IntValue") or Armors[i]:IsA("NumberValue") then
            local temp = Player:LoadNumber(Armors[i].Name)
            if temp >= 0 then
                Armors[i].Value = temp
            end
        elseif Armors[i]:IsA("BoolValue") then
            local temp = Player:LoadBoolean(Armors[i].Name)
            Armors[i].Value = temp
        elseif Armors[i]:IsA("StringValue") then
            local temp = Player:LoadString(Armors[i].Name)
            Armors[i].Value = temp
        end
    end
end

function onPlayerEntered(newPlayer)

        --Variables folders
        local stats = Instance.new("Model")
        stats.Name = "leaderstats"

        local attrb = Instance.new("Model")
        attrb.Name = "attributes"

        local misc = Instance.new("Model")
        misc.Name = "Stats"

        local armor = Instance.new("Model")
        armor.Name = "UnlockedArmor"

        --Leaderstats variables
        local clicks = Instance.new("IntValue", stats)
        clicks.Name = "Lvl"
        clicks.Value = 1

        local clicks2 = Instance.new("IntValue", stats)
        clicks2.Name = "XP"
        clicks2.Value = 0

        local clicks3 = Instance.new("IntValue", stats)
        clicks3.Name = "Gold"
        clicks3.Value = 0

        --Attributes
        local attrb1 = Instance.new("IntValue", attrb)
        attrb1.Name = "Strength"
        attrb1.Value = Properties.StartingAttributes.Value

        local attrb2 = Instance.new("IntValue", attrb)
        attrb2.Name = "Constitution"
        attrb2.Value = Properties.StartingAttributes.Value

        local attrb3 = Instance.new("IntValue", attrb)
        attrb3.Name = "Intelligence"
        attrb3.Value = Properties.StartingAttributes.Value

        local attrb4 = Instance.new("IntValue", attrb)
        attrb4.Name = "Dexterity"
        attrb4.Value = Properties.StartingAttributes.Value

        --Other stats
        local mana = Instance.new("IntValue", misc)
        mana.Name = "Mana"
        mana.Value = 100

        local maxmana = Instance.new("IntValue", misc)
        maxmana.Name = "MaxMana"
        maxmana.Value = 100

        local equippedArmor = Instance.new("StringValue", misc)
        equippedArmor.Name = "EquippedArmor"
        equippedArmor.Value = "None"

        local defense = Instance.new("IntValue", misc)
        defense.Name = "Defense"
        defense.Value = 0

        --Unlocked armors
        local unlockedArmorLeather = Instance.new("BoolValue", armor)
        unlockedArmorLeather.Name = "UnlockedLeather"
        unlockedArmorLeather.Value = false

        local unlockedArmorChainmail = Instance.new("BoolValue", armor)
        unlockedArmorChainmail.Name = "UnlockedChainmail"
        unlockedArmorChainmail.Value = false

        local unlockedArmorBronze = Instance.new("BoolValue", armor)
        unlockedArmorBronze.Name = "UnlockedBronze"
        unlockedArmorBronze.Value = false

        local unlockedArmorIron = Instance.new("BoolValue", armor)
        unlockedArmorIron.Name = "UnlockedIron"
        unlockedArmorIron.Value = false

        local unlockedArmorSteel = Instance.new("BoolValue", armor)
        unlockedArmorSteel.Name = "UnlockedSteel"
        unlockedArmorSteel.Value = false

        local unlockedArmorMithril = Instance.new("BoolValue", armor)
        unlockedArmorMithril.Name = "UnlockedMithril"
        unlockedArmorMithril.Value = false

        local unlockedArmorAdamant = Instance.new("BoolValue", armor)
        unlockedArmorAdamant.Name = "UnlockedAdamant"
        unlockedArmorAdamant.Value = false

        local unlockedArmorDemonic = Instance.new("BoolValue", armor)
        unlockedArmorDemonic.Name = "UnlockedDemonic"
        unlockedArmorDemonic.Value = false

        local unlockedArmorBone = Instance.new("BoolValue", armor)
        unlockedArmorBone.Name = "UnlockedBone"
        unlockedArmorBone.Value = false

        --PvP       
        if Properties.HasPvP.Value == true then
            local clicksPvP = Instance.new("IntValue", stats)
            clicksPvP.Name = "PvP Points"
            clicksPvP.Value = 0

            local inPvP = Instance.new("BoolValue", misc)
            inPvP.Name = "InPvP"
            inPvP.Value = game.Workspace.GAME_PROPERTIES.GlobalPvP.Value
        end

        --Assign the folders to player.
        stats.Parent = newPlayer
        attrb.Parent = newPlayer
        misc.Parent = newPlayer
        armor.Parent = newPlayer

        clicks2.Changed:connect(function() onXPChanged(newPlayer, clicks2, clicks) end) 
        clicks.Changed:connect(function() onLevelUp(newPlayer, clicks2, clicks) end) 

        newPlayer:WaitForDataReady(true) 

        LoadData(newPlayer)

        newPlayer.CharacterAdded:connect(function(c) 
            c.Humanoid.MaxHealth = (Game.Workspace.GAME_PROPERTIES.StartingHP.Value) + (Game.Workspace.GAME_PROPERTIES.HPPerLevel.Value * newPlayer.leaderstats.Lvl.Value) + (Game.Workspace.GAME_PROPERTIES.HPPerConstitution.Value * newPlayer.attributes.Constitution.Value)
            c.Humanoid.Health = c.Humanoid.MaxHealth
        end) 
end 

function onPlayerRemoving(player)
    print("Attempting to save score for " .. player.Name)
    SaveData(player)
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerRemoving)
0
B R U H M O M E N T you just reposted your answer because you don't know how to format stuff BashGuy10 384 — 4y
0
bro gloveshun 119 — 4y

Closed as Not Constructive by M39a9am3R

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?