Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Stats / Attributes
- lvl=game:GetService("DataStoreService"):GetOrderedDataStore("Lvl")
- xp=game:GetService("DataStoreService"):GetOrderedDataStore("XP")
- gp=game:GetService("DataStoreService"):GetOrderedDataStore("Gold")
- str=game:GetService("DataStoreService"):GetOrderedDataStore("Strength")
- con=game:GetService("DataStoreService"):GetOrderedDataStore("Constitution")
- dex=game:GetService("DataStoreService"):GetOrderedDataStore("Dexterity")
- int=game:GetService("DataStoreService"):GetOrderedDataStore("Intelligence")
- -- Armors
- leather=game:GetService("DataStoreService"):GetDataStore("UnlockedLeather")
- chainmail=game:GetService("DataStoreService"):GetDataStore("UnlockedChainmail")
- iron=game:GetService("DataStoreService"):GetDataStore("UnlockedIron")
- steel=game:GetService("DataStoreService"):GetDataStore("UnlockedSteel")
- mithril=game:GetService("DataStoreService"):GetDataStore("UnlockedMithril")
- demonic=game:GetService("DataStoreService"):GetDataStore("UnlockedDemonic")
- bone=game:GetService("DataStoreService"):GetDataStore("UnlockedBone")
- bronze=game:GetService("DataStoreService"):GetDataStore("UnlockedBronze")
- adamant=game:GetService("DataStoreService"):GetDataStore("UnlockedAdamant")
- equipped=game:GetService("DataStoreService"):GetDataStore("EquippedArmor")
- -- PvP
- pvp=game:GetService("DataStoreService"):GetOrderedDataStore("PvP Points")
- 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! He/she is now level " .. level.Value .. "!"
- wait(5)
- m.Parent = nil
- end
- function saveScores(player)
- lvl:SetAsync(player.userId, player.leaderstats.Lvl.Value)
- xp:SetAsync(player.userId, player.leaderstats.XP.Value)
- gp:SetAsync(player.userId, player.leaderstats.Gold.Value)
- print("Stats saved!")
- end
- function saveScorePvP(player)
- pvp:SetAsync(player.userId, player.leaderstats:FindFirstChild("PvP Points").Value)
- end
- function saveAttributes(player)
- str:SetAsync(player.userId, player.attributes.Strength.Value)
- con:SetAsync(player.userId, player.attributes.Constitution.Value)
- dex:SetAsync(player.userId, player.attributes.Dexterity.Value)
- int:SetAsync(player.userId, player.attributes.Intelligence.Value)
- print("Attributes saved!")
- end
- function saveArmor(player)
- leather:SetAsync(player.userId, player.UnlockedArmor.UnlockedLeather.Value)
- chainmail:SetAsync(player.userId, player.UnlockedArmor.UnlockedChainmail.Value)
- iron:SetAsync(player.userId, player.UnlockedArmor.UnlockedIron.Value)
- steel:SetAsync(player.userId, player.UnlockedArmor.UnlockedSteel.Value)
- mithril:SetAsync(player.userId, player.UnlockedArmor.UnlockedMithril.Value)
- demonic:SetAsync(player.userId, player.UnlockedArmor.UnlockedDemonic.Value)
- bone:SetAsync(player.userId, player.UnlockedArmor.UnlockedBone.Value)
- bronze:SetAsync(player.userId, player.UnlockedArmor.UnlockedBronze.Value)
- adamant:SetAsync(player.userId, player.UnlockedArmor.UnlockedAdamant.Value)
- equipped:SetAsync(player.userId, player.stats.EquippedArmor.Value)
- print("Armor saved successfully!")
- end
- function loadScores(player)
- player.leaderstats.Lvl.Value = lvl:GetAsync(player.userId)
- player.leaderstats.XP.Value = xp:GetAsync(player.userId)
- player.leaderstats.Gold.Value = gp:GetAsync(player.userId)
- if (player.leaderstats.Lvl.Value == 0) then
- player.leaderstats.Lvl.Value = 1 -- Makes sure always level 1.
- end
- end
- function loadScorePvP(player)
- player.leaderstats:FindFirstChild("PvP Points").Value = pvp:GetAsync(player.userId)
- end
- function loadAttributes(player)
- local attrb1 = str:getAsync(player.userId)
- local attrb2 = con:getAsync(player.userId)
- local attrb3 = int:getAsync(player.userId)
- local attrb4 = dex:getAsync(player.userId)
- if attrb1 >= Properties.StartingAttributes.Value then
- player.attributes.Strength.Value = attrb1
- else
- player.attributes.Strength.Value = Properties.StartingAttributes.Value
- end
- if attrb2 >= Properties.StartingAttributes.Value then
- player.attributes.Constitution.Value = attrb2
- else
- player.attributes.Constitution.Value = Properties.StartingAttributes.Value
- end
- if attrb3 >= Properties.StartingAttributes.Value then
- player.attributes.Intelligence.Value = attrb3
- else
- player.attributes.Intelligence.Value = Properties.StartingAttributes.Value
- end
- if attrb4 >= Properties.StartingAttributes.Value then
- player.attributes.Dexterity.Value = attrb4
- else
- player.attributes.Dexterity.Value = Properties.StartingAttributes.Value
- end
- end
- function loadArmors(player)
- local armor1 = leather:getAsync(player.userId)
- local armor2 = chainmail:getAsync(player.userId)
- local armor3 = iron:getAsync(player.userId)
- local armor4 = steel:getAsync(player.userId)
- local armor5 = mithril:getAsync(player.userId)
- local armor6 = demonic:getAsync(player.userId)
- local armor7 = bone:getAsync(player.userId)
- local armor8 = bronze:getAsync(player.userId)
- local armor9 = adamant:getAsync(player.userId)
- local armorEquipped = equipped:getAsync(player.userId)
- if armor1 == true then
- player.UnlockedArmor.UnlockedLeather.Value = armor1
- end
- if armor2 == true then
- player.UnlockedArmor.UnlockedChainmail.Value = armor2
- end
- if armor3 == true then
- player.UnlockedArmor.UnlockedIron.Value = armor3
- end
- if armor4 == true then
- player.UnlockedArmor.UnlockedSteel.Value = armor4
- end
- if armor5 == true then
- player.UnlockedArmor.UnlockedMithril.Value = armor5
- end
- if armor6 == true then
- player.UnlockedArmor.UnlockedDemonic.Value = armor6
- end
- if armor7 == true then
- player.UnlockedArmor.UnlockedBone.Value = armor7
- end
- if armor8 == true then
- player.UnlockedArmor.UnlockedBronze.Value = armor8
- end
- if armor9 == true then
- player.UnlockedArmor.UnlockedAdamant.Value = armor9
- end
- if armorEquipped ~= nil then
- player.Stats.EquippedArmor.Value = armorEquipped
- 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 unlockedArmor1 = Instance.new("BoolValue", armor)
- unlockedArmor1.Name = "UnlockedLeather"
- unlockedArmor1.Value = false
- local unlockedArmor2 = Instance.new("BoolValue", armor)
- unlockedArmor2.Name = "UnlockedChainmail"
- unlockedArmor2.Value = false
- local unlockedArmor3 = Instance.new("BoolValue", armor)
- unlockedArmor3.Name = "UnlockedIron"
- unlockedArmor3.Value = false
- local unlockedArmor4 = Instance.new("BoolValue", armor)
- unlockedArmor4.Name = "UnlockedSteel"
- unlockedArmor4.Value = false
- local unlockedArmor5 = Instance.new("BoolValue", armor)
- unlockedArmor5.Name = "UnlockedMithril"
- unlockedArmor5.Value = false
- local unlockedArmor6 = Instance.new("BoolValue", armor)
- unlockedArmor6.Name = "UnlockedDemonic"
- unlockedArmor6.Value = false
- local unlockedArmor7 = Instance.new("BoolValue", armor)
- unlockedArmor7.Name = "UnlockedBone"
- unlockedArmor7.Value = false
- local unlockedArmor8 = Instance.new("BoolValue", armor)
- unlockedArmor8.Name = "UnlockedBronze"
- unlockedArmor8.Value = false
- local unlockedArmor9 = Instance.new("BoolValue", armor)
- unlockedArmor9.Name = "UnlockedAdamant"
- unlockedArmor9.Value = false
- --PvP Points
- if Properties.HasPvP.Value == true then
- local clicksPvP = Instance.new("IntValue", stats)
- clicksPvP.Name = "PvP Points"
- clicksPvP.Value = 0
- end
- --Assign the folders to player.
- stats.Parent = newPlayer
- attrb.Parent = newPlayer
- misc.Parent = newPlayer
- armor.Parent = newPlayer
- clicks3.Changed:connect(function() saveScores(newPlayer) end)
- clicks2.Changed:connect(function() onXPChanged(newPlayer, clicks2, clicks) end)
- clicks.Changed:connect(function() onLevelUp(newPlayer, clicks2, clicks) saveScores(newPlayer) saveAttributes(newPlayer) saveArmor(newPlayer) end)
- newPlayer:WaitForDataReady() -- IMPORTANT: after player.DataReady = true, it won't become false again. You need DataReady to be true for a player before you can load or save data.
- loadAttributes(newPlayer)
- loadScores(newPlayer)
- loadArmors(newPlayer)
- if Properties.HasPvP.Value == true then
- loadScorePvP(newPlayer)
- end
- 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)
- local stats = player:FindFirstChild("leaderstats")
- local attrb = player:FindFirstChild("attributes")
- local misc = player:FindFirstChild("Stats")
- local armor = player:FindFirstChild("UnlockedArmor")
- --Save core stats.
- if (stats ~= nil) then
- local level = stats:FindFirstChild("Lvl")
- local experience = stats:FindFirstChild("XP")
- local gold = stats:FindFirstChild("Gold")
- if (level ~= nil)and(experience ~= nil)and(gold ~= nil) then
- saveScores(player)
- end
- local pointsPvP = stats:FindFirstChild("PvP Points")
- if (pointsPvP ~= nil) then
- saveScorePvP(player)
- end
- end
- --Save attributes.
- if (attrb ~= nil) then
- local attrb1 = attrb:FindFirstChild("Strength")
- local attrb2 = attrb:FindFirstChild("Constitution")
- local attrb3 = attrb:FindFirstChild("Intelligence")
- local attrb4 = attrb:FindFirstChild("Dexterity")
- if (attrb1 ~= nil)and(attrb2 ~= nil)and(attrb3 ~= nil)and(attrb4 ~= nil) then
- saveAttributes(player)
- end
- end
- --Save unlocked armor.
- if (armor ~= nil) then
- local armor1 = armor:FindFirstChild("UnlockedLeather")
- local armor2 = armor:FindFirstChild("UnlockedChainmail")
- local armor3 = armor:FindFirstChild("UnlockedIron")
- local armor4 = armor:FindFirstChild("UnlockedSteel")
- local armor5 = armor:FindFirstChild("UnlockedMithril")
- local armor6 = armor:FindFirstChild("UnlockedDemonic")
- local armor7 = armor:FindFirstChild("UnlockedBone")
- local armor8 = armor:FindFirstChild("UnlockedBronze")
- local armor9 = armor:FindFirstChild("UnlockedAdamant")
- local armorEquipped = misc:FindFirstChild("EquippedArmor")
- if (armor1 ~= nil)and(armor2 ~= nil)and(armor3 ~= nil)and(armor4 ~= nil)and(armor5 ~= nil)and(armor6 ~= nil)and(armor7 ~= nil)and(armor8 ~= nil)and(armor9 ~= nil)and(armorEquipped ~= nil) then
- saveArmor(player)
- end
- end
- end
- game.Players.PlayerAdded:connect(onPlayerEntered)
- game.Players.PlayerRemoving:connect(onPlayerRemoving)
Advertisement
Add Comment
Please, Sign In to add comment