Player breaks when using an ability and a player joins at the same time

The script basically just stops when a player joins, so the user ends up being frozen and/or the ability breaks. I’m not sure why this is happening, can anybody help?

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local chr = plr.Character
local hum = chr:WaitForChild("Humanoid")
local hroot = chr:WaitForChild("HumanoidRootPart")
local mouse = plr:GetMouse()

local Server = RS:WaitForChild("Server")

local hitboxes = script.Parent.Hitboxes
local models = script.Parent.Models
local animator = hum:WaitForChild("Animator")
	
local Animation = Instance.new("Animation")
Animation.AnimationId = 'rbxassetid://17213415340'
local AnimationTrack = animator:LoadAnimation(Animation)

local endlag = false
local endlagTime = .5
local db = false

UIS.InputBegan:Connect(function(input, IsTyping)
	if IsTyping then return end
	if input.KeyCode == Enum.KeyCode.Z and endlag == false then endlag = true
		
		AnimationTrack:Play()
		
		Server:InvokeServer("Fire", "Charge", {})
		
		local BG = Instance.new("BodyGyro", hroot)
		BG.CFrame = CFrame.new(hroot.Position, mouse.Hit.Position)
		BG.MaxTorque = Vector3.one * 99999
		BG.P = 3000
		BG.D = 100
		BG.Name = "Align"
		
		local BP = Instance.new("BodyPosition", hroot)
		BP.Position = hroot.Position
		BP.MaxForce = Vector3.one * 99999
		BP.P = 3000
		BP.D = 100
		BP.Name = "Position"
		
		hum.WalkSpeed = 0
		hum.JumpPower = 0
		
		for i = 1,30 do task.wait()
			BG.CFrame = CFrame.new(hroot.Position, mouse.Hit.Position)
		end
		
		hum.WalkSpeed = 16
		hum.JumpPower = 50
		
		Server:InvokeServer("Fire", "Fireball", {CFrame = hroot.CFrame, EndPos = mouse.Hit.Position})
		
		BG:Destroy()
		BP:Destroy()
		task.wait(endlagTime)
		endlag = false
	end
end)