Ok so I made this level up script, but every time when a person enters the game they would explode. After they explode everything would be fine, but I was just wondering if someone could help me fix it. I'll put the script at the bottom.
scoreKey = "PlayerScore" levelKey = "PlayerLevel" function onPlayerEntered(newPlayer) if newPlayer.Name ~= "Player1" then newPlayer:WaitForDataReady() end local stats = Instance.new("IntValue") stats.Name = "supercool" local xp = Instance.new("IntValue") xp.Name = "XP" local level = Instance.new("IntValue") level.Name = "Lvl" xp.Parent = stats level.Parent = stats stats.Parent = newPlayer xp.Changed:connect(function() onXPChanged(newPlayer, xp, level) end) level.Changed:connect(function() onlevelChanged(newPlayer, xp, level) end) newPlayer.Changed:connect(function (property) end) local ok, ret = pcall(function() return newPlayer:LoadNumber(scoreKey) end) if ok then if ret ~= 0 then xp.Value = ret else print("Nothing to load/score was 0") end else print("Error:", ret) end local ok2, ret1 = pcall(function() return newPlayer:LoadNumber(levelKey) end) if ok2 then if ret1 ~= 0 then level.Value = ret1 else print("Nothing to load/score was 0") level.Value = 1 end else print("Error:", ret1) end wait(2) if newPlayer.Character~=nil then newPlayer.Character.Humanoid.MaxHealth = level.Value * 25 + 100 newPlayer.Character.Humanoid.Health = 0 else wait() end end function onXPChanged(player, XP, level) if player.Character~=nil then if XP.Value>=level.Value * 125 then XP.Value = (XP.Value - (level.Value * 125)) level.Value = level.Value + 1 else wait() end end end function onlevelChanged(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.new(Color3.new(10, 1, 10)) 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 if player.Character ~= nil then player.Character.Humanoid.MaxHealth = level.Value * 25 + 100 player.Character.Humanoid.Health = level.Value * 25 + 100 end local m = Instance.new("Hint") m.Parent = game.Workspace m.Text = player.Name .. " has leveled up!" wait(2) m.Parent = nil end local respawnTime = .5 local Players = Game:GetService("Players") Players.CharacterAutoLoads = false Players.PlayerAdded:connect(function(Player) Player.CharacterAdded:connect(function(Character) -- find the humanoid, and detect when it dies local Humanoid = Character:FindFirstChild("Humanoid") if Humanoid then Humanoid.Died:connect(function() -- delay, then respawn the character wait(respawnTime) Player:LoadCharacter() Player.Character.Humanoid.MaxHealth = Player.supercool.Lvl.Value * 25 + 100 Player.Character.Humanoid.Health = Player.supercool.Lvl.Value * 25 + 100 end) end end) Player:LoadCharacter() end) game.Players.ChildAdded:connect(onPlayerEntered)