How to make a part spawn on the floor when I click?

I’m trying to make a part spawn on the ground or the floor relative to the mouse when I click. Right now it works sometimes but other times the part spawns above the ground or on top of something. I want the part to always spawn right on the ground.

1 Like

We need to see the code to help solve the problem.

3 Likes

Yes, can you please show your code so that we can help you.

local mouse = game.Players.LocalPlayer:GetMouse()
local part = Instance.new("Part")
part.CFrame = CFrame.new(mouse.Hit.p)
part.Parent = workspace

Alright I kind of misunderstood your question.

In order to make the part spawn on the ground and not, for example, in midair or on a wall, you need to use RayCasting to find the place on the ground to place the part.

If you think you can do it yourself, give it a try. But if you are unfamiliar and would like my help, let me know and I will try to construct a script that does this for you.

I got bored, so I made it myself. I added some other important things to it as well.

Now, if you’re inside a building and you click on the ceiling, the part will spawn on the floor directly below it.

Make a LocalScript inside the tool with the following code:

local mouse = game.Players.LocalPlayer:GetMouse()
local equipped=false
local function fixpos(pos,part)
	local y = pos.Y
	local cutoff = part.Size.Y/2
	return Vector3.new(pos.X,y+cutoff,pos.Z)
end
mouse.Button1Down:Connect(function()
	if equipped then
		local p = mouse.Hit.p
		local ray = workspace:Raycast(p,Vector3.new(0, -1000, 0))
		local part = Instance.new("Part")
		if not ray then
			part.Position=fixpos(p,part)
		else
			part.Position=fixpos(ray.Position,part)
		end
		part.Anchored=true
		part.Parent=workspace
	end
end)

local tool = script.Parent
tool.Equipped:Connect(function()
	equipped=true
end)
tool.Unequipped:Connect(function()
	equipped=false
end)
5 Likes

Okay, it’s working now thanks.

The only issue with this is that you are able to click into the sky, and a part will spawn up in the sky.

if not mouse.Target then return end