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)