Parts move when not supposed to

Hi there! So I recently made a drag script. It works well, BUT when you use auto clicker to drag something, the ground starts opening up! I have NO clue how to fix it. there are no errors too. Is there something I did wrong?

Glitched Pictures:
Image

The Script:

local plr = game.Players.LocalPlayer
local character = game.Players.LocalPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local RootPart = character:WaitForChild("HumanoidRootPart")


local UserInputService = game:GetService("UserInputService")


UserInputService.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		script.Parent.Left.Image = "rbxassetid://6378335571"
		script.Parent.Right.Image = "rbxassetid://6378345574"
	end
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		script.Parent.Left.Image = "rbxassetid://6378304822"
		script.Parent.Right.Image = "rbxassetid://6378339649"
	end
end)

local Mouse = game.Players.LocalPlayer:GetMouse()
local Dragger = script.Parent.Dragger
Dragger.Parent = nil
Shift = false
Y = 0
RotY = 0
X = 0
RotX = 0
local Player = game.Players.LocalPlayer.Character
while Player == nil do
	Player = game.Players.LocalPlayer.Character
	wait()
end
--
YSpeed = 3
XSpeed = 3
--

Mouse.Button1Down:Connect(function()
	if Mouse.Target ~= nil then
		if Mouse.Target:FindFirstChild("CanDrag") ~= nil then
			R = game.ReplicatedStorage.DragEvents.SetDrag:InvokeServer(Mouse.Target, true)
			if R == true then
				game.SoundService.PingHigh.Volume = 1
				game.SoundService.PingHigh:Play()
				Dragger.CFrame = Mouse.Hit
				Dragger.Weld.Part1 = Mouse.Target
				Dragger.Parent = game.Workspace
			end
		end
	end
end)

Mouse.Button1Up:Connect(function()
	R = game.ReplicatedStorage.DragEvents.SetDrag:InvokeServer(Dragger.Weld.Part1, false)
	if R == true then
		game.SoundService.PingLow.Volume = 1
		game.SoundService.PingLow:Play()
		Dragger.Weld.Part1 = nil
		Dragger.Parent = nil
		RotY = 0
		Y = 0
		X = 0
	end
end)

--

while true do
	wait()
	if Dragger.Weld.Part1 ~= nil and (Dragger.Weld.Part1.Position - Player.Head.CFrame.Position).Magnitude <= 11 then
		RotY = RotY + Y
		RotX = RotX + X
		CF1 = CFrame.new(Player.Head.CFrame.Position, Vector3.new(Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z))
		CF2 = CF1 * CFrame.new(0, 0, -4)
		Dragger.BPos.Position = Vector3.new(CF2.X, CF2.Y, CF2.Z)
		Dragger.BGyro.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(RotY), math.rad(RotX), 0)
	elseif Dragger.Weld.Part1 ~= nil and (Dragger.Weld.Part1.Position - Player.Head.CFrame.Position).Magnitude ~= 11 then
		game.SoundService.PingHigh.Volume = 0
		game.SoundService.PingLow.Volume = 0
		Dragger.Weld.Part1 = nil
		Dragger.Parent = nil
		RotY = 0
		Y = 0
		X = 0
	end
end

I think it has something to do with line 42 - 55

1 Like