Model follows mouse but I want it to stop when it touches models in folder

Local Script in a text button

local debounce = true
local org = game:GetService('ReplicatedStorage').Thingthatfollowsmouse
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character


script.Parent.MouseButton1Down:Connect(function(plr)
	if debounce == true then
		debounce = false
		local copy = org:Clone()
		copy.Parent = game.Workspace.Taegetfilt
		repeat
			local mousepos = mouse.Hit.p
			copy:SetPrimaryPartCFrame(CFrame.new(mousepos)+ Vector3.new(0,0.5,0)) 
			print(mousepos)
			local humanoidroot = char.HumanoidRootPart.Position
			local mag = (humanoidroot - mousepos).Magnitude
			mouse.TargetFilter = game.Workspace.Taegetfilt
			wait()
		until mag >= 50
		copy:Remove()
		print("too far")
		debounce = true
	end
end)

Script works fine so far its just, I don’t know to to stop the model from passing through other models. I’m sorry if I’m asking to much I just don’t know what to do.
image

local model1 = workspace:WaitForChild("Model1")

for i, v in pairs(model1:GetDescendants()) do --loop through parts of main model
	if v:IsA("Part") then
		v.Touched:Connect(function(hit) --if any of those parts touched another part check if the other part belongs to a particular model
			if hit.Parent.Name == "" then --change to name of other model
				--do code
			end
		end)
	end
end

It doesn’t work for me. dw about it i’ll try to figure it out myself

You’re not supposed to copy & paste it, it needs changing to fit your script.

yeah I know, it still doesn’t work

Raycast towards the front, if anything hits then disconnect the mouse.

You must’ve implemented it incorrectly then.

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local storage = game:GetService("ReplicatedStorage")
local org = storage:WaitForChild("Thingthatfollowsmouse")
local debounce = false

script.Parent.MouseButton1Down:Connect(function(plr)
	if debounce then
		return
	end
	debounce = true
	local copy = org:Clone()
	copy.Parent = workspace
	for i, v in pairs(copy:GetChildren()) do
		if v:IsA("MeshPart") or v:IsA("Part") then
			v.Touched:Connect(function(hit)
				if hit.Name == "Some" then
					copy:Destroy()
					--mouse.Move:Disconnect()
				end
			end)
		end
	end
	mouse.Move:Connect(function()
		local mousePos = mouse.Hit.p
		copy:SetPrimaryPartCFrame(CFrame.new(mousePos)+ Vector3.new(0,0.5,0))
	end)
	task.wait(3)
	debounce = false
end)

This is a little janky at the moment.

1 Like

This works perfectly, but the touched event only works if something moves on the part, Like the player. It doesn’t work on other parts. I don’t want you writing anything else bro, you are a chad for just writing that. I’m just gonna mess around with magnitude until it works, thanks for all the help bro