Part that follows my mouse constantly fly's closer towards it?

In this script, I am trying to move this “Outline” to the position of my mouse on the baseplate, but everytime I go to do this, it flies up, following my mouses position on the screen.

Visual example of what I mean:
(this is super hard to explain, sorry.)
Gyazo link

I’ve tried just setting the position of the block to the x axis, but it always says that it can’t do that, “X cannot be assigned to”. I’ve also tried Cancollide and CanTouch to false but neither works.

Full script:

(Just look until you see my notes)

--variables

local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnFolder = ReplicatedStorage.Spawnables

local Switch = false
local debounce = false
local OnexButton = script.Parent.MiniHouse
local OnexTButton = script.Parent.LongHouse
local TwoxOButton = script.Parent.ScrapperHouse
local Outline = game.Workspace:WaitForChild("MiniLong")
--detection 
OnexButton.MouseButton1Down:Connect(function()
	if Switch == false then
		Switch = true
		
		print("Started placing 1x1")
		UIS.InputBegan:Connect(function(key, processed)
			if processed then
				return
			end
			
			if key.UserInputType == Enum.UserInputType.MouseButton1 then -- Detects if mouse is clicked
				if Switch == true then
					if debounce == false then
						debounce = true
						local SpawnableHouse = ReplicatedStorage.Spawnables.MiniSquare:Clone()
						local MousePos = game.Players.LocalPlayer:GetMouse().Hit.p
						
						
						while true do
							--no clue why this doesnt work, flikers in and out. Can't transofrm to vector2. If put on mouse position it will follow cursor including to the Z (up and away)
	
							Outline.Position = game.Players.LocalPlayer:GetMouse().Hit.p
							print(Outline.Position)
							task.wait(0.05)
						end
						
						
						SpawnableHouse.PrimaryPart:PivotTo(CFrame.new(MousePos.X,MousePos.Y + SpawnableHouse.PrimaryPart.ExtentsSize.Y / 2, MousePos.Z)) 
						
						SpawnableHouse.Parent = workspace
						print("Right mouse button clicked! (1x1)")

						debounce = false
						Switch = false
				end
				end
			end
		end)
		
		
	end

	
end)
OnexTButton.MouseButton1Down:Connect(function()
	if Switch == false then
		Switch = true
		
		print("Started placing 2x1")
		UIS.InputBegan:Connect(function(key, processed)
			if processed then
				return
			end

			if key.UserInputType == Enum.UserInputType.MouseButton1 then -- Detects if mouse is clicked
				if Switch == true then
					if debounce == false then
						debounce = true
						local SpawnableHouse = ReplicatedStorage.Spawnables.MiniLong:Clone()
						local MousePos = game.Players.LocalPlayer:GetMouse().Hit.p
						SpawnableHouse.PrimaryPart:PivotTo(CFrame.new(MousePos.X,MousePos.Y + SpawnableHouse.PrimaryPart.ExtentsSize.Y / 2, MousePos.Z)) 
						SpawnableHouse.Parent = workspace
						print("Right mouse button clicked! (2x1)")

						debounce = false
						Switch = false
					end
				end
			end
		end)
		
		
	end

end)
TwoxOButton.MouseButton1Down:Connect(function()
	if Switch == false then
		Switch = true
		
		print("Started placing 1x2")
		UIS.InputBegan:Connect(function(key, processed)
			if processed then
				return
			end

			if key.UserInputType == Enum.UserInputType.MouseButton1 then -- Detects if mouse is clicked
				if Switch == true then
					if debounce == false then
						debounce = true
						local SpawnableHouse = ReplicatedStorage.Spawnables.MiniTall:Clone()
						local MousePos = game.Players.LocalPlayer:GetMouse().Hit.p
						SpawnableHouse.PrimaryPart:PivotTo(CFrame.new(MousePos.X,MousePos.Y + SpawnableHouse.PrimaryPart.ExtentsSize.Y / 2, MousePos.Z)) 						
						SpawnableHouse.Parent = workspace
						print("Right mouse button clicked! (1x2)")

						debounce = false
						Switch = false
					end
				end
			end
		end)
	end

end)

 

Thanks for your help, sorry for the long script.

Here’s the main part we’re looking at:

while true do

      Outline.Position = game.Players.LocalPlayer:GetMouse().Hit.p
      print(Outline.Position)
      task.wait(0.05)
end

You have to set the model your building with to the mouse TargetFilter im pretty sure

1 Like

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