I made a localscript that creates a part in front of the player in case there was nothing in front when I clicked on the button, but the part is being created in the map spawn, here is the code and a video:
Code
btn.MouseButton1Down:Connect(function()
local char = game.Players.LocalPlayer.Character
local result = workspace:Raycast(char.HumanoidRootPart.Position, char.HumanoidRootPart.CFrame.LookVector * 40)
if result == nil then
part = game.Workspace.Model:Clone()
part.Parent = game.Workspace
part:SetPrimaryPartCFrame(CFrame.new(char.HumanoidRootPart.CFrame.LookVector * 18))
print(char.HumanoidRootPart.CFrame.LookVector * 18)
else
print("No")
end
btn:TweenSize(UDim2.new(0.265, 0, 0.13, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.2, true)
end)
That is happening because CFrame.lookVecotr is not the position of where the player is looking at, but only a direction. You’ll have to use raycaring instead
Ray casting tutorial
You don’t have to use raycasting actually, just add the character position to the multiplied lookvector
btn.MouseButton1Down:Connect(function()
local char = game.Players.LocalPlayer.Character
local result = workspace:Raycast(char.HumanoidRootPart.Position, char.HumanoidRootPart.CFrame.LookVector * 40)
if result == nil then
part = game.Workspace.Model:Clone()
part.Parent = game.Workspace
part:SetPrimaryPartCFrame(CFrame.new(char.HumanoidRootPart.Position + (char.HumanoidRootPart.CFrame.LookVector * 18)))
else
print("No")
end
btn:TweenSize(UDim2.new(0.265, 0, 0.13, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.2, true)
end)
You’re right. Although it would ignore objects such as walls in front so the parts might end up inside other parts. But thats a design choice I guess.
One thing that caught my attention is that your original code is creating a part locally. I dont know if that is intended or not, but a local part will not be visible to other players.It will only exist in your own comupter.
o never notice it’s a local script, I just edited OP’s script to work as they wanted it. I have thought about spawning behind walls but I just want to make it easier for OP to implement.
(OP being original poster)