How do I make VR Tool Grabbing?

I’m making a VR game with tools, and I can’t figure out how to make it so that you can grab them with your hand.

I took the hand script from a dev-forum post, but here's the script;
VRService = game:GetService('VRService')
UIS = game:GetService('UserInputService')

if not VRService.VREnabled then return end
StarterGui:SetCore("TopbarEnabled", false)
StarterGui:SetCore("VRLaserPointerMode", 0)
StarterGui:SetCore("VREnableControllerModels", false)

local Left = script:WaitForChild('L')
Left.Parent = workspace.Camera
local Right = script:WaitForChild('R')
Right.Parent = workspace.Camera

HeadScale = 2
Cam = workspace.CurrentCamera
Cam.HeadScale = HeadScale
Cam.CameraType = Enum.CameraType.Scriptable
Cam.CFrame = CFrame.new(0,6.5,-1.2)

game:GetService('RunService').RenderStepped:Connect(function()
	local LC = VRService:GetUserCFrame(Enum.UserCFrame.LeftHand)
	local RC = VRService:GetUserCFrame(Enum.UserCFrame.RightHand)
	Left.CFrame = (Cam.CFrame*CFrame.new(LC.p*2))*CFrame.fromEulerAnglesXYZ(LC:ToEulerAnglesXYZ())
	Right.CFrame = (Cam.CFrame*CFrame.new(RC.p*2))*CFrame.fromEulerAnglesXYZ(RC:ToEulerAnglesXYZ())
end)

Left.Touched:Connect(function(hit)
	if hit.Parent:isA(Instance.new("Tool")) then
		UIS.InputBegan:Connect(function(input, processed)
			if input.UserInputType == Enum.KeyCode.ButtonL2 then
				local tool = hit.Parent
				
				tool.CFrame = Left.CFrame
			end
	    end)
	end
end)

Right.Touched:Connect(function(hit)
	if hit.Parent:isA(Instance.new("Tool")) then
		UIS.InputBegan:Connect(function(input, processed)
			if input.UserInputType == Enum.KeyCode.ButtonR2 then
				local tool = hit.Parent

				tool.CFrame = Right.CFrame
			end
		end)
	end
end)

Here’s the part where I tried to make the hand grab the tools;

while wait() do
	Left.Touched:Connect(function(hit)
		if hit.Parent:isA(Instance.new("Tool")) then
			UIS.InputBegan:Connect(function(input, processed)
				if input.UserInputType == Enum.KeyCode.ButtonL2 then
					local tool = hit.Parent

					tool.CFrame = Left.CFrame
				end
			end)
		end
	end)

	Right.Touched:Connect(function(hit)
		if hit.Parent:isA(Instance.new("Tool")) then
			UIS.InputBegan:Connect(function(input, processed)
				if input.UserInputType == Enum.KeyCode.ButtonR2 then
					local tool = hit.Parent

					tool.CFrame = Right.CFrame
				end
			end)
		end
	end)
end

It doesn’t even detect the tool being grabbed.
I tried to make it print that it hit something, but nothing ever showed up.

1 Like

I’m going to be blunt, this whole script is a mess.

  • You’re making a touched connection every wait()
  • You’re making an input connection every Touched
  • You’re not at all using IsA correctly
  • CFrame is not a property of Tool

Even if those issues were fixed, this script doesn’t make any sense.

Before you dive into VR games, I would recommend you learn the basics of Lua and Roblox API.

2 Likes

Sorry, I wasn’t really using my brain while making the script.
I was having a hard time understanding VR, and that was the only thing I could come up with.

Plus, it was night and I was tired, so yeah.

Gonna try to fix it now, I should be better today since I got better rest.

Update; Still isn’t working, I completely remade the grabbing script, I’m gonna keep messing around though.

Here’s the new script I made;

local grabTrue = true

local tool = workspace.ClassicSword.Handle
	local keyPress = UIS:GetKeysPressed()
	for _, key in ipairs(keyPress) do
		if key.KeyCode == Enum.KeyCode.ButtonR2 then
			script.test:Play()
			tool.CFrame = Right.CFrame
		elseif key.KeyCode == Enum.KeyCode.ButtonL2 then
			script.test:Play()
			tool.CFrame = Left.CFrame
   end
end

You seem to make the grabTrue variable, but never using it.

Yeah, I used that for some other type of script that didn’t work, and I accidentally left it there.

You may want to utilize it, to prevent spam equip and unequip.

you have to program it yourself

1 Like

unhelpful, plus i already figured out a solution 8 months ago.

Do you have any advice on how I could do this? I am trying to do this for my game but I cant find any info about it online.

2 Likes