CFrame not changing when if should be changing

local scipt in textbutton

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player:GetMouse()
local storage = game:GetService("ReplicatedStorage")
local org = storage:WaitForChild("Thingthatfollowsmouse")
local debounce = false
local rpressed = script.Parent.RPressed
local char = player.Character
local lol = 0
local unlockthing = 0
local pea = CFrame.Angles(0, 0, 0)

script.Parent.MouseButton1Down:Connect(function()
	if debounce == false then
		debounce = true
		local copy = org:Clone()
		copy.Parent = workspace.Taegetfilt
		mouse.TargetFilter = workspace.Taegetfilt
		while true and wait() do
			local mousepos = mouse.Hit.p
			local touchingparts = copy:GetTouchingParts()
			
			print(unlockthing)
			if unlockthing >= 10 then
				lol = 0
			end
			
			local function meow()
				copy.CFrame = CFrame.new(mousepos + Vector3.new(0, 0.7, 0)) * pea
			end
			
			if lol == 0 then
				meow()
				
				script.Parent.RPressed.Changed:Connect(function()
					if rpressed.Value == 1 then
						pea = CFrame.Angles(0, math.rad(180), 0)
					elseif rpressed.Value == 2 then
						pea = CFrame.Angles(0, math.rad(270), 0)
					elseif rpressed.Value == 3 then
						pea = CFrame.Angles(0, math.rad(360), 0)

					elseif rpressed.Value == 4 then
						pea = CFrame.Angles(0, math.rad(90), 0)
					end
				end)
			end
			
			print(lol)
			
			local mag = (mousepos - char.HumanoidRootPart.Position).Magnitude
			
			if mag >= 40  then
				print("to far")
				copy:Remove()
				debounce = false
				lol = 0
				return
			end			
			
			if #touchingparts <= 0 then
				copy.BrickColor = BrickColor.Green()
			end
			
			for i=1,#touchingparts do
				if #touchingparts > 0  then
					
					unlockthing = (touchingparts[i].Position - mousepos).Magnitude
					
					
					if touchingparts[i].Parent.Parent == workspace.Taegetfilt then
						if touchingparts[i].Name == "Back" then
							if copy.Orientation == touchingparts[i].Parent.Main.Orientation then
								lol = 1
								copy.CFrame = touchingparts[i].Parent.Main.CFrame:ToWorldSpace(CFrame.new(0, 0, 6))
							elseif  copy.Orientation == touchingparts[i].Parent.Main.Orientation - Vector3.new(0, 180, 0) then						
								copy.CFrame = touchingparts[i].Parent.Main.CFrame:ToWorldSpace(CFrame.new(0, 0, 6))
								copy.Orintation = touchingparts[i].Parent.Main.Orientation - Vector3.new(0, 180, 0)
								lol = 1
							end
						elseif touchingparts[i].Name == "Front" then
							if copy.Orientation == touchingparts[i].Parent.Main.Orientation then
								lol = 1
								copy.CFrame = touchingparts[i].Parent.Main.CFrame:ToWorldSpace(CFrame.new(0, 0, -6))
							elseif copy.Orientation == touchingparts[i].Parent.Main.Orientation - Vector3.new(0, 180, 0) then
								copy.CFrame = touchingparts[i].Parent.Main.CFrame:ToWorldSpace(CFrame.new(0, 0, -6))
								copy.Orientation = touchingparts[i].Parent.Main.Orientation - Vector3.new(0, 180, 0)
								lol = 1
							end
						elseif lol == 0 then
							copy.BrickColor = BrickColor.Red()
						end
					end				
				end				
			end
		end
	end
end)

When the copy touches a part, its cframe is changed to that part. When this happens the loop that sets the copy cframe to the mouse is turned off. Once it is turned on again, CFrame doesn’t change but the loop is running. Sorry if I’m explain it poorly, if you need more context please ask.

sorry I don’t quite understand, I modified a bit
bye :sweat_smile:

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player:GetMouse()
local storage = game:GetService("ReplicatedStorage")
local org = storage:WaitForChild("Thingthatfollowsmouse")
local debounce = false
local rpressed = script.Parent.RPressed
local char = player.Character
local lol = 0
local unlockthing = 0
local pea = CFrame.Angles(0, 0, 0)

script.Parent.RPressed.Changed:Connect(function()
	if rpressed.Value == 1 then
		pea = CFrame.Angles(0, math.rad(180), 0)
	elseif rpressed.Value == 2 then
		pea = CFrame.Angles(0, math.rad(270), 0)
	elseif rpressed.Value == 3 then
		pea = CFrame.Angles(0, math.rad(360), 0)

	elseif rpressed.Value == 4 then
		pea = CFrame.Angles(0, math.rad(90), 0)
	end
end)
local function meow()
	copy.CFrame = CFrame.new(mousepos + Vector3.new(0, 0.7, 0)) * pea
end

script.Parent.MouseButton1Down:Connect(function()
	if debounce == false then
		debounce = true
		local copy = org:Clone()
		copy.Parent = workspace.Taegetfilt
		mouse.TargetFilter = workspace.Taegetfilt
		Run()
	end
end)
function Run()
while true and task.wait() do
	local mousepos = mouse.Hit.p
	local touchingparts = copy:GetTouchingParts()
	
	print(unlockthing)
	if unlockthing >= 10 then
		lol = 0
	end
	
	if lol == 0 then
		meow()		
	end
	if lol == 0 or lol == 1 then --I add this guy
		debounce = false
		break
	end
	print(lol)
	
	local mag = (mousepos - char.HumanoidRootPart.Position).Magnitude
	
	if mag >= 40  then
		print("to far")
		copy:Remove()
		debounce = false
		lol = 0
		--return 
		break
	end			
	
	if #touchingparts <= 0 then
		copy.BrickColor = BrickColor.Green()
	end
	
	for i=1,#touchingparts do
		if #touchingparts > 0  then
			
			unlockthing = (touchingparts[i].Position - mousepos).Magnitude
					
			if touchingparts[i].Parent.Parent == workspace.Taegetfilt then
				if touchingparts[i].Name == "Back" then
					if copy.Orientation == touchingparts[i].Parent.Main.Orientation then
						lol = 1
						copy.CFrame = touchingparts[i].Parent.Main.CFrame:ToWorldSpace(CFrame.new(0, 0, 6))
					elseif  copy.Orientation == touchingparts[i].Parent.Main.Orientation - Vector3.new(0, 180, 0) then						
						copy.CFrame = touchingparts[i].Parent.Main.CFrame:ToWorldSpace(CFrame.new(0, 0, 6))
						copy.Orientation = touchingparts[i].Parent.Main.Orientation - Vector3.new(0, 180, 0)
						lol = 1
					end
				elseif touchingparts[i].Name == "Front" then
					if copy.Orientation == touchingparts[i].Parent.Main.Orientation then
						lol = 1
						copy.CFrame = touchingparts[i].Parent.Main.CFrame:ToWorldSpace(CFrame.new(0, 0, -6))
					elseif copy.Orientation == touchingparts[i].Parent.Main.Orientation - Vector3.new(0, 180, 0) then
						copy.CFrame = touchingparts[i].Parent.Main.CFrame:ToWorldSpace(CFrame.new(0, 0, -6))
						copy.Orientation = touchingparts[i].Parent.Main.Orientation - Vector3.new(0, 180, 0)
						lol = 1
					end
				elseif lol == 0 then
					copy.BrickColor = BrickColor.Red()
				end
			end				
		end				
	end
end
end

Could you show where it does not work, no one have patience these days to read big scripts, you know right?

1 Like