I have recently posted this regarding the same concept. I’ve asked a follow-up question somewhere in the comments, however I didn’t get an answer, so now I’m posting it here.
I have been trying to make a drag and drop system, much like how it is in Half-Blox 2.
Pressing E on an object would cause the object to be picked up and moved. When the item is picked up, pressing E again would drop the item.
(In Half-Blox 2 you pick the item up by pressing E on the object. For now I’m making it so that the player has to directly click on the item instead, for testing purposes)
The issue is that when the item is clicked, the item disappears instead of being positioned 6 studs away from the player’s camera in the direction they are facing.
This is the script:
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer --the local player
local part = script.Parent
local cd = part.ClickDetector
local Key = Enum.KeyCode.E
local itemModel = script.Parent.Parent
script.Parent.ClickDetector.MouseClick:Connect(function()
while true do
local camCFrame = workspace.CurrentCamera.CFrame
local itemPosition = camCFrame + camCFrame.lookVector * 6
itemModel:SetPrimaryPartCFrame(itemPosition)
wait()
end
end)
Can someone explain why this is happening and how I can fix this? Any help would be great.
You didn’t say CFrame.new, so it didn’t create a new CFrame.
Try putting : local itemPostion = CFrame.new(camCFrame.Position) + camCFrame.lookVector * 6
No, it is supposed to be positioned a few studs away from the player’s camera in the direction they are facing. If you have ever played Half-Blox 2 you might get what I mean.
Could you maybe send a video of what happens when you click on it?
And whether the script is a local script or server script, and where it is placed in the hierarchy.
Oddly enough, there wasn’t much of a difference when I used either of those scripts. The part teleports behind the player. See the video I posted in the comments.
In that case, it should be erroring when you do Players.LocalPlayer, and for your other statement, in a server script you can use game:GetService(“RunService”).Stepped