How to fix cube being spawned in the wall?

hello,

so for my game, players have to spawn cubes.
and it was going really well until i placed one on the wall.
it spawned half inside the wall and i don’t know how to fix it…
how do i do that?

Localscript:

local Folder = script.Parent
local Tool = Folder.Parent
local Event = Folder:WaitForChild("Task")

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local BlockName = Tool.Name.."Block"
local Ghosts = game.ReplicatedStorage.GhostBlocks
local RealGhost = Ghosts:FindFirstChild(BlockName)
local RunService = game:GetService("RunService")
local Clone = nil
local Connection = nil

Tool.Activated:Connect(function()
	Event:FireServer(BlockName,Mouse.Hit.Position)
end)
Tool.Equipped:Connect(function()
	 Clone = RealGhost:Clone()
        Clone.Parent = workspace
	Connection = RunService.Heartbeat:Connect(function(t)
		Clone.Position = Mouse.Hit.Position + Vector3.new(0,2,0)
	end)
end)
Tool.Unequipped:Connect(function()
	Clone:Destroy()
	Connection:Disconnect()
end)

script:

local Folder = script.Parent
local Event = Folder:WaitForChild("Task")
local Tool = Folder.Parent

Event.OnServerEvent:Connect(function(plr,Name,Position)
	local Cube = game.ServerStorage.Blocks:FindFirstChild(Name):Clone()
	Cube.Parent = workspace
    Cube.Position = Position + Vector3.new(0,2,0)
	Tool:Destroy()
end)

Thanks in advance!

1 Like

The simplest solution (since the blocks doens’t seem to be rotateable in this case) would be to check if mouse.Hit.Position’s X and Y values are higher or lower then mouse.Target’s X and Y values, and then offset the block accordingly

This does rely on the mouse.Target to not be rotated (and does not account for any other potential intersections nearby)

You could also spawn in the part, use getpartsinpart, and then offset the part until it isn’t colliding with anything. This would cause issues if its placed in a box where it cant go anywhere though

1 Like

how would i be able to know where to offset the part if i use GetTouchingParts()?
(edit: nvm i figured it out thank you!!!)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.