How can I make a drag and drop system which can allow me to move items? Like in Half-Blox 2?

I’m trying to script a system for my game, in which 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.
Half-Blox 2 has this same system.

I haven’t found any helpful resources, apart from this post. This post is slightly different though, since it shows how to drag an item with the mouse, and not by pressing E.

What I’m saying is that I’m lost in how I would make this drag and drop system I described in the first paragraph. Does anyone know any tutorials that might be able to help me, or perhaps scripting tips, pointers, and examples? Any help would be great.
I also only have a month of scripting experience. I might ask further questions if someone decides to help out.

Thanks in advance.

You can continually position the item in front of the player’s look vector (assuming they are zoomed in) by doing this:

local camCFrame = workspace.CurrentCamera.CFrame
local itemPosition = camCFrame + camCFrame.lookVector * 6

That sets itemPosition to the position 6 studs away from the player’s camera in the direction they are looking. If you put that in a loop, and position the item via BodyPosition or just setting it’s CFrame, you’ll achieve the effect you’re looking for.

I know my script is incorrect, but this is where I’m getting at based on your comment. Mind elaborating so I can understand a bit better?

while true do
  local camCFrame = workspace.CurrentCamera.CFrame
  local itemPosition = camCFrame + camCFrame.lookVector * 6
end

Oh no don’t create an infinite loop! You’ll need to add a wait statement somewhere in there.

while true do
  local camCFrame = workspace.CurrentCamera.CFrame
  local itemPosition = camCFrame + camCFrame.lookVector * 6
  itemModel:SetPrimaryPartCFrame(itemPosition)--this will actually set your item model to the proper position
  wait()
end

You’ll have to define itemModel to whatever model you’re trying to move

Edit: You can make it smoother by using Heartbeat instead of a while loop as well

game:GetService("RunService").Heartbeat:Connect(function()
    --put the code here
end)
1 Like

ohhh ok, sorry :grimacing: That makes sense now.

1 Like

and one more thing… how can I make sure the player can only pick up specific items? I don’t want the player to pick up, for example, the baseplate or any other items that I don’t want to be picked up.

I would suggest CollectionService.

1 Like

ok… so now when I use that script, the object disappears.

Sorry if you consider this reply a bump but here’s a full script for it
Use CollectionService to tag the stuff you want to be grabbable with a tag called Grabbable
Put a RemoteEvent in ReplicatedStorage called Ungrab

LocalScript in StarterCharacter

local CS = game:GetService("CollectionService")
local UIS = game:GetService("UserInputService")
local Plr = game.Players.LocalPlayer
local SelectedItem

UIS.InputBegan:Connect(function(Input, GPE)
	if not GPE then
		if Input.KeyCode == Enum.KeyCode.E then
			local Mouse = Plr:GetMouse()
			if Mouse.Target and Mouse.Target:IsA("BasePart") then
				if CS:HasTag(Mouse.Target, "Grabbable") then
					SelectedItem = Mouse.Target
				end
			end
		end
	end
end)

UIS.InputEnded:Connect(function(Input, GPE)
	if not GPE then
		if Input.KeyCode == Enum.KeyCode.E then
			if SelectedItem then
				game.ReplicatedStorage.Ungrab:FireServer(SelectedItem, SelectedItem.Position)
				SelectedItem = nil
			end
		end
	end
end)

game:GetService("RunService").Heartbeat:Connect(function()
	if SelectedItem then
		local camCFrame = workspace.CurrentCamera.CFrame
		local itemPosition = camCFrame + camCFrame.lookVector * 6
		SelectedItem.CFrame = itemPosition
	end
end)

This fires a remote when you drop the item so the server knows you moved the part and can place it where you dropped it (also physics are weird if you don’t do this)
This is exploitable but it shouldn’t really be a problem if your game is single player

Here’s the server Script (in ServerScriptService)

game.ReplicatedStorage.Ungrab.OnServerEvent:Connect(function(Plr, Part, Pos)
    Part.Position = Pos
end)
7 Likes

Oh my god, thank you thank you. I’ve been trying to implement this in my game for a week. Thank you once again.

1 Like