Heartbeat Crashing Studio

Hello, I’m writing a script, however when it gets to a heartbeat function it immediately crashes. I was messing with my script to fix a bug earlier, and it wasn’t happening then, but I reverted it and it is still crashing. I’m not sure what the problem is, here’s the script:

local RepStorage = game:GetService(“ReplicatedStorage”)

local RunService = game:GetService(“RunService”)
local RemoteEvent = RepStorage.SwordRemote
local Heartbeat = RunService.Heartbeat
local Debounce = false
local Debris = game:GetService(“Debris”)

RemoteEvent.OnServerEvent:Connect(function(Player, Character, Action)
if Action == 1 and not Debounce then
local Dashing = false
local RunTimes = 0
Debounce = true
local TreeSword = RepStorage.TreeSword:Clone()
local Leaf = TreeSword.Handle.Leaf
local Roots1 = TreeSword.Handle.Roots1
local Roots2 = TreeSword.Handle.Roots2
local Tree = TreeSword.Handle.Tree
local Leaves = TreeSword.Blade.Leaves
local Blade = TreeSword.Blade.Bottom.Blade
local Trail = TreeSword.Blade.Trail
local SpawnAnim = Character.Humanoid.Animator:LoadAnimation(TreeSword.SpawnAnim)
local DashAnim = Character.Humanoid.Animator:LoadAnimation(TreeSword.DashAnim)
local AimPart
TreeSword.Parent = Character
TreeSword:SetPrimaryPartCFrame(Character.RightHand.CFrame + TreeSword:GetPrimaryPartCFrame().UpVector/2)
TreeSword:SetPrimaryPartCFrame(TreeSword:GetPrimaryPartCFrame() * CFrame.Angles(1.5, 90, 0))
local TreeToHand = Instance.new(“WeldConstraint”)
TreeToHand.Part0 = Tree
TreeToHand.Part1 = Character.RightHand
TreeToHand.Parent = Tree
Character.HumanoidRootPart.Anchored = true
SpawnAnim:Play()
Tree.Transparency = 1
Roots1.Transparency = 1
Roots2.Transparency = 1
Leaf.Transparency = 1
Blade.Enabled = false
Trail.Enabled = false
task.wait(0.075)
repeat
Tree.Transparency -= 0.05
Roots1.Transparency -= 0.05
Roots2.Transparency -= 0.05
Leaf.Transparency -= 0.05
task.wait()
until Leaf.Transparency <= 0
Blade.Enabled = true
RemoteEvent.OnServerEvent:Connect(function(Player, Character, Action, Origin, Direction, Camera)
if Action == 2 and not AimPart then
local RaycastParamaters = RaycastParams.new()
RaycastParamaters.FilterType = Enum.RaycastFilterType.Exclude
RaycastParamaters.FilterDescendantsInstances = {Character:GetChildren(), TreeSword}
local HumanoidRootPart = Character.HumanoidRootPart

			local RayHitboxParams = RaycastParams.new()
			RayHitboxParams.FilterType = Enum.RaycastFilterType.Exclude

			local distance = (HumanoidRootPart.Position - Camera.Position)
			local maxDistance = (HumanoidRootPart.Position - Camera.Position).Magnitude + 150

			local result = workspace:Raycast(Origin, Direction * maxDistance, RaycastParamaters)
			if result then
				local hitPart = result.Instance
				local hitPos = result.Position

				AimPart = Instance.new("Part")
				AimPart.Size = Vector3.new(1, 1, 1)
				AimPart.Anchored = true
				AimPart.Position = hitPos
				AimPart.Parent = workspace.AimParts
			else
				local rayEnd = Origin + Direction * maxDistance
				AimPart = Instance.new("Part")
				AimPart.Size = Vector3.new(1, 1, 1)
				AimPart.Anchored = true
				AimPart.Position = rayEnd
				AimPart.Parent = workspace.AimParts
			end
			task.wait(0.1)
			HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position, Vector3.new(AimPart.Position.X, HumanoidRootPart.Position.Y, AimPart.Position.Z))
			HumanoidRootPart.Anchored = true
			DashAnim:Play()
			local OrigonalPos = HumanoidRootPart.Position
			RunTimes = 0
			Leaves.Enabled = true
			Leaves.Parent = HumanoidRootPart
			Trail.Enabled = true
			Trail.Parent = HumanoidRootPart
			Trail.Attachment0 = HumanoidRootPart.RootAttachment
			Trail.Attachment1 = HumanoidRootPart.RootRigAttachment
			Dashing = true
			Heartbeat:Connect(function(DeltaTime)
				print(RunTimes)
				local CurrentPos = HumanoidRootPart.Position
				if RunTimes > 14 then HumanoidRootPart.Anchored = false  Character.Humanoid.WalkSpeed = 16 Character.Humanoid.JumpHeight = 7.2 Debris:AddItem(TreeSword, 0.5) Trail.Enabled = false Leaves.Enabled = false Dashing = false task.wait(1) Debounce = false return else HumanoidRootPart.Anchored = true Character.Humanoid.WalkSpeed = 0 Character.Humanoid.JumpHeight = 0 end 
				local MoveDistance = 10 * (60 * DeltaTime)
				HumanoidRootPart.CFrame = HumanoidRootPart.CFrame + HumanoidRootPart.CFrame.LookVector * MoveDistance
				RunTimes += 1
			end)
		end
	end)
end

end)

1 Like

Upon further inspection, it seems that task.wait() is crashing the game, unsure why, normal wait() is doing the same thing. If I remove the wait 0.1 it crashes later

Ok apparently I accidentally copy pasted the script earlier and having it run at exactly the same time crashed it. I feel really dumb

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.