Part gets removed from workspace randomly without reason

I have a part ("LaserEnd) located in the workspace that has an attachment and a ParticleEmitter inside. I have a raycast script to move its position to the mouse if the parent of the hit part has a child named “Minable” inside it. After mining for a while, the beam that is connected to the LaserEnd attachment stops working. When checking the beam, it went from workspace.LaserEnd.Attachment to LaserEnd.Attachment. I honestly have no idea why this would happen. Here’s my raycast script.

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local laserEnd = game.Workspace.LaserEnd
local mouse = player:GetMouse()
mouse.TargetFilter = workspace.LaserEnd
local hold = false

local function MouseRaycast(blacklist)
	local mousePosition = UIS:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
	local raycastParams = RaycastParams.new()
	
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist
	
	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
	
	return raycastResult
end

UIS.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	local inputType = input.UserInputType
	if inputType == Enum.UserInputType.MouseButton1 then
		hold = true
	end
end)    

UIS.InputEnded:Connect(function(input)
	local inputType = input.UserInputType
	if inputType == Enum.UserInputType.MouseButton1 then
		hold = false
	end
end)

RunService.Heartbeat:Connect(function()
	
	if hold == true then
		local result = MouseRaycast({laserEnd})
		if result and result.Instance then
			for i, laser in pairs(workspace.Lasers:WaitForChild(player.Name):GetChildren()) do
				if result.Instance.Parent:FindFirstChild("Minable") then
					local x = result.Position.X
					local y = result.Position.Y
					local z = result.Position.Z



					local cframe = CFrame.new(x,y,z)
					laserEnd.CFrame = cframe

					local char = player.Character or player.CharacterAdded:Wait()
					local pos = char:GetPrimaryPartCFrame().p
					local distance = (pos - result.Instance.Parent.PrimaryPart.Position).Magnitude

					if distance <= 30 then
						if laser.LaserBall["Meshes/BallExtra"].Beam.Enabled == false then
							laser.LaserBall["Meshes/BallExtra"].Beam.Enabled = true
							laserEnd.ParticleEmitter.Enabled = true
						end
					else
						if laser.LaserBall["Meshes/BallExtra"].Beam.Enabled == true then
							laser.LaserBall["Meshes/BallExtra"].Beam.Enabled = false
							laserEnd.ParticleEmitter.Enabled = false
						end
					end
				else
					if laser.LaserBall["Meshes/BallExtra"].Beam.Enabled == true then
						laser.LaserBall["Meshes/BallExtra"].Beam.Enabled = false
						laserEnd.ParticleEmitter.Enabled = false
					end
				end
			end
		end
	else
		for i, laser in pairs(workspace.Lasers:WaitForChild(player.Name):GetChildren()) do
			if laser.LaserBall["Meshes/BallExtra"].Beam.Enabled == true then
				laser.LaserBall["Meshes/BallExtra"].Beam.Enabled = false
				laserEnd.ParticleEmitter.Enabled = false
			end
		end
	end
end)

nvm i think its because the part wasn’t anchored im dumb

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