Preventing flashing when using mouse to move items

Video of the problem

com-video-to-gif%20(3)

Now, the video ain’t capturing the glitch entirely. What’s really happening is the model is rapidly flashing, and moving between the 2 positions you can see in the video. Video ain’t capturing enough frames/second, so it looks like the part is just moving, but either way, you can see in the video the part ‘glitching’ between 2 positions, even though the mouse ain’t even moving.

Moves the model wherever the mouses position is

local renderStepped	
	renderStepped = runService.RenderStepped:Connect(function()
		local cframe = placement:Calculate(
			playersPlot.PrimaryPart,
			wallClone, 
			mouse.Hit.p, 
			var.Rotation
		)
		wallClone:SetPrimaryPartCFrame(cframe)
	end)

Placement module (used for position the model)

function placement:Calculate(basePart, model, position, rotation)
	local cframe, size = baseSize:Calculate(basePart)

	local modelSize = CFrame.fromEulerAnglesYXZ(
		0, 
		rotation, 
		0
	)*model.PrimaryPart.Size
	
	modelSize = Vector3.new(
		math.abs(
			modelSize.x
		), 
		math.abs(
			modelSize.y
		), 
		math.abs(
			modelSize.z
		)
	)

	local lpos = cframe:PointToObjectSpace(position)
	local size2 = (
		size - Vector2.new(
			modelSize.x, 
			modelSize.z
		)
	)/2

	local x = math.clamp(
		lpos.x, 
		-size2.x, 
		size2.x
	)
	
	local y = math.clamp(
		lpos.y,
		-size2.y, 
		size2.y
	)

	local grid = var.Wall_Grid
	
	if grid > 0 then
		x = math.sign(x)*((math.abs(x) - math.abs(x)%grid) + (size2.x%grid))
		y = math.sign(y)*((math.abs(y) - math.abs(y)%grid) + (size2.y%grid))
	end
	
	return cframe*CFrame.new(x, y, -modelSize.y/2)*CFrame.Angles(-math.pi/2, rotation, 0)
end


Added a print(mouse.Hit.p) and it’s printing a different number rapidly, even tho in that SS the mouse was not moving.

2 Likes

Looks like your mouse is going on the model so it’s positioning it based on that and then the model’s been moved so it tries to go back where the mouse is now.

Try setting the mouse’s TargetFilter to the model you’re moving, I believe that’ll ignore it when looking for Hit

8 Likes

Sorry for the late response. I put it inside a ModuleScript that is being required by a LocalScript, still seems to occur tho

EDIT Managed to fix it by setting it to the Folder that the models are cloned too :+1:

1 Like