How to place a model on click?

  1. What do you want to achieve? I want to be able to place my model on the workspace when I click it.

  2. What is the issue? The model doesn’t seem to load in the workspace when I click it. The code only works for a Part instance.

  3. What solutions have you tried so far? I have no idea how to start. I tried looking for posts, but to no avail.

local model = game.ReplicatedStorage["Basic Cubicle"]
model.Size = Vector3.new(5,5,5)
model.Transparency = 0.5
model.CanCollide = false
model.Anchored = true
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Move:Connect(function()
	local raycastresult = workspace:Raycast(mouse.Hit.Position + Vector3.new(0,150,0),Vector3.new(0,-250,0))
	if raycastresult ~= nil then -- 
		if raycastresult.Instance.Name == "Part" then 
			model.Position = raycastresult.Instance.Position 
		end
	end
end)

mouse.Button1Down:Connect(function()
	local raycastresult = workspace:Raycast(mouse.Hit.Position + Vector3.new(0,150,0),Vector3.new(0,-250,0))
	local p = model:Clone()
	p.Transparency = 0
	p.Parent = workspace
	p.Position = raycastresult.Instance.Position
end)
1 Like

Pretty sure you cant just set the transparency of a model, you have to go through all instances inside the model and set the transparency, Also, you cant use Position on models. You must use :PivotTo().

But, if your code is correct and instead you are moving a part, try this?

mouse.Move:Connect(function()
	local mouseLocation = game:GetService("UserInputService"):GetMouseLocation()
	local raycastresult = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y) 
	if raycastresult then -- doing the ~= nil is unnecessary
		if raycastresult.Instance:IsA("BasePart") then 
			model.Position = raycastresult.Instance.Position 
		end
	end
end)

mouse.Button1Down:Connect(function()
	
	local mouseLocation = game:GetService("UserInputService"):GetMouseLocation()
	local ray = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y) 
	local p = model:Clone()
	p.Transparency = 0
	p.Parent = workspace
	p.Position = ray.Instance.Position
end)
1 Like

Instead of raycasting just use mouse.Hit but offset the by .5

It doesn’t seem to work, sadly.

1 Like

Ohhh? I am so sorry. I forgot to make a ray using the unit rays origin and direction. Replace the old function with this one: (keep in mind im on phone, so you will have to change it a bit if theres typos

local location = game:GetService(“UserInputService”):GetMouseLocation()
local unit = workspace.CurrentCamera:ViewportToRay(location.x, location.y)
local origin = unit.Origin
local direction = unit.Direction * 5000 —distance

local ray = workspace:Raycast(origin, direction)
if ray then
— add your placing code here
end