Trouble with Placement System

I’m having some trouble with my placement system script. What is suppose to happen is when I click a textbutton it adds the object I have in replicated storage to my mouse so i can move it but when I press T on my keyboard to enable stacking which allows the object to go on top of other objects it has this weird jittering effect where it bounces up and down really quickly.

I have a hunch that the problem exists within the updatePlacement Function. When I call the function later in the script I used runService.RenderStepped:Connect(updatePlacement) to call it so it fires more quickly. I need to use Run Service so the object follows my mouse I just don’t know how to stop this effect.

-- This is an example Lua code block
local replicatedStorage = game:GetService("ReplicatedStorage")
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local players = game:GetService("Players")

local placementFolder = replicatedStorage:WaitForChild("PlacementObjects")
local placementObject = placementFolder:WaitForChild("PlacementObject") -- The part to be placed

local player = players.LocalPlayer
local mouse = player:GetMouse()

local placingObject = nil
local rotation = 0
local gridSnap = 1
local stackable = true

local function createOutline()
	local outline = Instance.new("SelectionBox")
	outline.Adornee = placingObject
	outline.LineThickness = 0.05
	outline.Color3 = Color3.new(0, 1, 0)
	outline.Parent = placingObject
	return outline
end

local function getSurfacePosition(hit, target)
	return Vector3.new(
		hit.X,
		target.Position.Y + target.Size.Y / 2 + placingObject.Size.Y / 2,
		hit.Z
	)
end

local function updatePlacement()
	if placingObject then
		local mousePos = mouse.Hit.Position
		local target = mouse.Target

		if stackable and target and target:IsA("BasePart") then
			mousePos = getSurfacePosition(mousePos, target)
		else
			mousePos = Vector3.new(
				math.floor(mousePos.X / gridSnap + 0.5) * gridSnap,
				placingObject.Position.Y,  -- Maintain the current Y position
				math.floor(mousePos.Z / gridSnap + 0.5) * gridSnap
			)
		end

		placingObject.CFrame = CFrame.new(mousePos) * CFrame.Angles(0, math.rad(rotation), 0)
	end
end

local function placeObject()
	if placingObject then
		local newObject = placementObject:Clone()
		newObject.Position = placingObject.Position
		newObject.Orientation = placingObject.Orientation
		newObject.Parent = workspace
	end
end

local function rotateObject()
	rotation = (rotation + 90) % 360
end

local function onMouseButton1Down()
	placeObject()
end

local function onKeyPress(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.R then
			rotateObject()
		elseif input.KeyCode == Enum.KeyCode.T then
			stackable = not stackable
		end
	end
end

local function ButtonClick()
	if not placingObject then
		placingObject = placementObject:Clone()
		placingObject.Parent = workspace
		createOutline()
		runService.RenderStepped:Connect(updatePlacement)
		mouse.Button1Down:Connect(onMouseButton1Down)
		userInputService.InputBegan:Connect(onKeyPress)
	end
end

local textbutton = script.Parent
textbutton.MouseButton1Click:Connect(ButtonClick)

1 Like

Hello, i testing it in my studio.
And i just turn off collision on part, and it work properly!

problem solving:

Initially make it so that your part does not have a collision.
Then write in the script that when you click the left mouse button, your part becomes anchored and has collision.

If you want to stop RenderStepped, here’s what you need:

local RS = game:GetService("RunService")
local Connection
Connection = RS.RenderStepped:Connect(function()
	-- ur loop code here!
	print(tick)
end)

wait(1)

Connection:Disconnect()

Connection:Disconnect() - disables the loop function

That is, you need to make Connection:Disconnect() line when you click the left mouse button.
I think you can do that yourself :slight_smile:

These are problems I’ve encountered over the years with building systems with their solution:

  • Don’t add the 0.5 when gridding the position.
  • Mouse.Hit isn’t accurate so add a hitbox to the blocks you wan’t to be placed that is 0.1 studs largen than the block
1 Like

I’ll try what you said, and let you know how it goes, Thanks.

Did you try it with the bool value stackable set to true? I know it works with stackable == false but not when it’s true. I tried your suggestion It doesn’t work.

Then I think the script needs to be rewritten to work properly :slight_smile:
If I can, I’ll try to help you after a while

I think I’ve helped you now at least with how to stop RenderStepped. or no?

I tried using the code you suggest to disconnect the RenderStepped after a second but to no avail the same thing happened as before not really sure what it is but I know it’s got to be related to the RenderStepped.