I’m trying to make simple wall placement system for my game, but sometimes the walls would spawn underneath the baseplate. With further explanation I realized that the mouse.Hit in the below script is sometimes in the negative Y coordinates (which is not what I want). How can I properly place the wall without it noclipping underground? Note: The issue only happens 50% of the time I attempt to build, and sometimes it spawns halfway aboveground.
Local Script:
local folder = game.ReplicatedStorage:WaitForChild("WallFolder")
local plr = game.Players.LocalPlayer
local wall = folder:WaitForChild("Wall")
local preview = folder:WaitForChild("Preview")
local context = game:GetService("ContextActionService")
local runservice = game:GetService("RunService")
local mouse = plr:GetMouse()
local onbuild = game.ReplicatedStorage:WaitForChild("OnBuild")
local isplacingobj = false
local humanoidrootpart = plr.Character:WaitForChild("HumanoidRootPart")
local function onT ()
if not isplacingobj then
isplacingobj = true
local pclone = preview:Clone()
pclone.Parent = workspace
local rootpart = pclone.RootPart
pclone.PrimaryPart = rootpart
runservice.RenderStepped:Connect(function()
if isplacingobj then
mouse.TargetFilter = pclone
local rawposition = CFrame.new(mouse.Hit.Position.X,mouse.Hit.Position.Y +rootpart.Size.Y/2, mouse.Hit.Position.Z) * CFrame.Angles(math.rad(0),math.rad(humanoidrootpart.Orientation.Y), math.rad(0))
pclone:PivotTo(rawposition)
end
end)
mouse.Button1Down:Connect(function()
if isplacingobj then isplacingobj = false
print(mouse.Hit)
onbuild:FireServer(pclone.RootPart.CFrame)
pclone:Destroy()
end
end)
end
end
context:BindAction("Tpressed", onT, false, Enum.KeyCode.T)
Server Side Code:
local repstorage = game:GetService("ReplicatedStorage")
local onbuild = repstorage.OnBuild
local wall = repstorage.WallFolder.Wall
onbuild.OnServerEvent:Connect(function(plr, cframe)
local clone = wall:Clone()
clone.Parent = workspace
clone:PivotTo(cframe)
end)
```lua
