Item is disappearing when being clicked on (drag and drop system)

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.

3 Likes

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

2 Likes

I got an error saying [invalid argument #1 to ‘new’ (Vector3 expected, got CFrame)]

Oh sorry my mistake. Put this:
local itemPosition = CFrame.new(camCFrame.Position) + camCFrame.lookVector * 6

1 Like

hm… the part still disappears after clicking on it.

oh, nevermind. It actually teleports below the player.

Oh… That’s very strange. Is that the way you intend it to go?

1 Like

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.

Was your camera facing down on the Player?

1 Like

The game is in first person. I was facing forward, so no.

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.

1 Like

do this instead:

local itemCFrame = CFrame.new(camCFrame.p + camCFrame.LookVector * 6)
itemModel:SetPrimaryPartCFrame(itemCFrame)
game:GetService("RunService").Heartbeat:Wait()

--- or an identical alternate solution would look like:

local itemCFrame = camCFrame * CFrame.new(0, 0, -6)
itemModel:SetPrimaryPartCFrame(itemCFrame)
game:GetService("RunService").Heartbeat:Wait()
2 Likes

robloxapp-20201028-1444351.wmv (1.8 MB)

C__Users_adila_Desktop_Roblox Projects_thing.rbxl - Roblox Studio 10_28_2020 2_46_38 PM (2)

1 Like

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, do CFrame.new(camera.CFrame.p - camera.CFrame.LookVector * 6)

You can’t use RunService because you don’t have a local script.

at the top of the script

local player = game.Players.LocalPlayer --the local player

look at the post I replied to, it showed that he put it in a server script, or atleast I assume he did.

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

I was thinking of that too, but @ZJ987 had shown me a screenshot of a server script.

1 Like