RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a level system with xp and data store and that can change players health and weapon damage"
-- main script in ServerScriptService local DataStoreService = game:GetService("DataStoreService") local levelStore = DataStoreService:GetDataStore("PlayerLevels") game.Players.PlayerAdded:Connect(function(player) local folder = Instance.new("Folder") folder.Name = "leaderstats" folder.Parent = player local xp = Instance.new("IntValue") xp.Name = "XP" xp.Value = 0 xp.Parent = folder local level = Instance.new("IntValue") level.Name = "Level" level.Value = 1 level.Parent = folder local success, errorMessage = pcall(function() local data = levelStore:GetAsync(player.UserId) if data then level.Value = data.level xp.Value = data.xp end end) if not success then print("Error loading data: " .. errorMessage) end end) game.Players.PlayerRemoving:Connect(function(player) local success, errorMessage = pcall(function() levelStore:SetAsync(player.UserId, { level = player.leaderstats.Level.Value, xp = player.leaderstats.XP.Value }) end) if not success then print("Error saving data: " .. errorMessage) end end) -- script for adding XP and leveling up local function addXP(player, amount) local stats = player.leaderstats stats.XP.Value = stats.XP.Value + amount local currentLevel = stats.Level.Value local requiredXP = currentLevel * 100 if stats.XP.Value >= requiredXP then stats.Level.Value = stats.Level.Value + 1 stats.XP.Value = stats.XP.Value - requiredXP player.Character.Humanoid.MaxHealth = 100 + (10 * stats.Level.Value) player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth local weaponDamage = player.Backpack:FindFirstChild("Weapon") if weaponDamage then weaponDamage.Damage.Value = 10 + (2 * stats.Level.Value) end end end -- example of usage game.ReplicatedStorage.AddXP.OnServerEvent:Connect(addXP)