Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I go about creating a snap placement system, this is what I have so far?

So far I've got an object to move to my mouse location, and place on click, but what I can't seem to figure out is how to get rid of the jittery mess the part looks like as it moves, seems like every time you move your mouse, while the parts moving it goes up 1/5th of a stud, then back down to where it should be, the following code shows what I've done.

local lastHit = nil
local lastTarget = nil
local x,y,z = playerMouse.Hit.x,playerMouse.Hit.y,playerMouse.Hit.z

tool.Equipped:connect(function(eventMouse)
	model = Instance.new("Model", game.Workspace)
	model.Name = "PartM"
	part = Instance.new("Part", game.Workspace.PartM)
	part.Anchored = true
	part.CanCollide = false
	part.Position = eventMouse.Hit.p
	
	eventMouse.Move:connect(function()
		eventMouse.TargetFilter = eventMouse.Target
		if (playerMouse.Hit ~= lastHit) then
			lastHit = playerMouse.Hit
			lastTarget = playerMouse.Target
			print(lastHit)
			print(lastTarget)
			
			model:MoveTo(eventMouse.Hit.p)

		end
	end)
end)

1 answer

✓ Accepted answer

The "jitteriness" that you are experiencing is being cause by setting the mouse's TargetFilter every time the mouse moves. When the TargetFilter is set to a specific part, it gets ignored. Therefore the next time you access the mouse's Target property, it will not include the part set as the TargetFilter. Everytime the mouse moves, you try to reset the mouse's TargetFilter except this time the target is whatever is behind the part you are ignoring. You want to only set the TargetFilter once, to the model you are moving.

Additionally, you are going to want to disconnect the move event or else it will continue to fire even after the tool is unequipped (assuming you haven't already).

local moveEvent

tool.Equipped:connect(function(eventMouse)
	local model = Instance.new("Model", game.Workspace)
	model.Name = "PartM"
 	local part = Instance.new("Part", game.Workspace.PartM)
 	part.Anchored = true
	part.CanCollide = false
	part.Position = eventMouse.Hit.p

	eventMouse.TargetFilter = model -- Now all descendants of model will be ignored

	moveEvent = eventMouse.Move:connect(function()
		model:MoveTo(eventMouse.Hit.p)
	end)
end)

tool.Unequipped:connect(function()
	if moveEvent then
		moveEvent:disconnect()
	end
end)

2 comments

0

Had no idea that mouse.move stayed active after the tool was unequipped, thanks for the advice, also thanks for explaining why what I was doing was wrong, I swear i tried it this way before but I guess I did something wrong.

0

No problem man! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.