i made a preview feature for my block placement system. when i join the game, the block falls through the ground and if it gets close to any other objects including my character it also falls through too.
local collectionService = game:GetService("CollectionService")
local tweenService = game:GetService("TweenService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character
local humanoid : Humanoid = char:FindFirstChild("Humanoid")
local humanoidRootPart = char:FindFirstChild("HumanoidRootPart")
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local assets = replicatedStorage.Assets
local Temp = workspace.Temp
local Pillars = assets.Pillars
local pillar_3 = Pillars.pillar_3
local tweenInfo = TweenInfo.new(0.25)
local objToRender = nil
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {char, objToRender}
--raycastParams:AddToFilter(char)
local function preparePreviewPart(object : Model)
objToRender = object:Clone()
objToRender.Parent = Temp
for _, part in pairs(objToRender:GetDescendants()) do
if part:IsA("BasePart") then
local weld = Instance.new("WeldConstraint")
weld.Parent = part
weld.Part0 = objToRender.PrimaryPart
weld.Part1 = part
part.Transparency = 0.5
part.CanCollide = false
part.CanQuery = false
end
end
end
local function snapToGrid(position : Vector3, gridSize : number)
return Vector3.new(
math.floor(position.X / gridSize + 0.5) * gridSize,
math.floor(position.Y / gridSize + 0.5) * gridSize,
math.floor(position.Z / gridSize + 0.5) * gridSize
)
end
local function renderPreview()
local mouseLocation = userInputService:GetMouseLocation()
local unitRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * 200, raycastParams)
local canBuild = char and humanoid and humanoid.Health > 0 and humanoidRootPart
if canBuild and raycastResult and raycastResult.Instance and objToRender then
local targetPos = raycastResult.Position + raycastResult.Normal * (objToRender.PrimaryPart.Size.Y / 2)
local tweenObj = tweenService:Create(
objToRender.PrimaryPart,
tweenInfo,
{
CFrame = CFrame.new(
snapToGrid(targetPos, 1)
)
}
)
tweenObj:Play()
tweenObj.Completed:Wait()
end
end
preparePreviewPart(pillar_3)
runService:BindToRenderStep("Preview", Enum.RenderPriority.Camera.Value, renderPreview)