Help with hold system

Working on a grab system and I’ve found an issue that can break the game:

  • Grabbed item rotates uncontrollably whilst in air.
  • Player can be launched into the sky by putting grabbed item below character
  • Grabbed item sometimes launches uncontrollably.

Local Code
local lplr = game.Players.LocalPlayer
local mouse = lplr:GetMouse()

local cc = workspace.CurrentCamera
local sM : Enum.Material 

local m = 3


local disval = Instance.new('NumberValue')
local cnt : RBXScriptConnection
local auth = game.ReplicatedStorage.AuthorizeNodeControl

local cnode
local bMouse = lplr.PlayerGui:WaitForChild('extra').bMouse

mouse.WheelForward:Connect(function()
	if cnode then
		if m < 5.8 then
			m+= .2
		end
	end
end)
mouse.WheelBackward:Connect(function()
	if cnode then 
		if m > .8 then
			m-= .2
		end
	end
end)

mouse.Move:Connect(function()
	if not cnode and mouse.Target then
		if mouse.Target.Parent == workspace.nodes then
			if mouse.Target.OwnerId.Value == lplr.UserId then
				bMouse.Visible = true
				bMouse.Position = UDim2.fromOffset(mouse.X,mouse.Y)
				return
			end
		end
	end
	bMouse.Visible = false
end)
local correct = function()
	local targetvec = mouse.Origin + mouse.Origin.LookVector*m
	local curvec = cnode.CFrame
	local dis = (curvec.Position-targetvec.Position).Magnitude
	local angle = CFrame.lookAt(curvec.Position,targetvec.Position)
	local line = workspace:FindFirstChild('line') or Instance.new('Part')
	local cpos = lplr.Character:WaitForChild('RightHand').CFrame
	line.Name = 'line' 
	line.CanCollide = false line.Anchored = true line.Size = Vector3.new(.1,.1,(cpos.Position-curvec.Position).Magnitude)
	line.Transparency = .6
	line.CFrame = 
		CFrame.lookAt(
			curvec.Position,cpos.Position) + 
		(
		CFrame.lookAt(curvec.Position,cpos.Position).LookVector *
		(cpos.Position-curvec.Position).Magnitude)/2
	line.Parent = workspace
	local sval = angle.LookVector*dis*m
	if dis < .1 then sval = Vector3.new(0,0,0) end
	cnode.Attachment.LinearVelocity.VectorVelocity = sval

end

mouse.Button1Down:Connect(function()
	local tnode = mouse.Target
	if tnode then
		if tnode.Parent == workspace.nodes then
			if auth:InvokeServer(tnode) then
				tnode.Transparency = .5
				sM = tnode.Material
				tnode.Material = Enum.Material.Neon
				tnode.Attachment.LinearVelocity.Enabled = true
				cnt = game["Run Service"].Heartbeat:Connect(correct)
				cnode = tnode
			end
		end
	end
end)


mouse.Button1Up:Connect(function()
	if cnode then
		-- Sets the client's view of the model back to regular.
		cnode.Transparency = 0
		cnode.Material = sM
		cnode.AssemblyAngularVelocity = Vector3.new(0,0,0)
		cnt:Disconnect()
		cnode.Attachment.LinearVelocity.VectorVelocity = Vector3.new(0,0,0)
		cnode.Attachment.LinearVelocity.Enabled = false
		if workspace:WaitForChild('line') then workspace:WaitForChild('line'):Destroy() end
		cnode = nil
	end
end)
AuthNodeControl (not relevant but invoked in local)
local Auth = game.ReplicatedStorage.AuthorizeNodeControl

Auth.OnServerInvoke = function(plr, node)
	if node.OwnerId.Value == plr.UserId then
		node:SetNetworkOwner(plr)
		return true
	else
		return false
	end
end
2 Likes

Use AlignOrientation.

And what would I align the position to?

1 Like

You could set the Mode to OneAttachment, then create a new Attachment inside of the part and set the AlignOrientation’s Attachment0 to the attachment.

You should do this before connecting to the RenderStepped event.

Most importantly you have to delete the alignOrientation and the Attachment when the player stops grabbing it otherwise it’ll stay at the same orientation forever…

Decided to fix it with a different method by saving the orientation before the object is picked up and each renderstepped setting the objects orientation to the saved orientation (whilst held ofc)

Edit: turns out that’s only client’s view. I’ll make sure to adapt your alignOrientation method

1 Like

I recommend using my method since its really good in terms of performance since we’re using physics.

Yep, implemented it and it’s all good!

Anyone got an idea of how to solve this?

Updated Local Code
local lplr = game.Players.LocalPlayer
local mouse = lplr:GetMouse()

local cc = workspace.CurrentCamera
local sM : Enum.Material 

local m = 3


local disval = Instance.new('NumberValue')
local cnt : RBXScriptConnection
local auth = game.ReplicatedStorage.AuthorizeNodeControl

local cnode
local bMouse = lplr.PlayerGui:WaitForChild('extra').bMouse
local savedo = Vector3.new(0,0,0)
local tatt = Instance.new('Attachment') tatt.Parent = workspace.Baseplate tatt.Name = 'tatt'

mouse.WheelForward:Connect(function()
	if cnode then
		if m < 5.8 then
			m+= .2
		end
	end
end)
mouse.WheelBackward:Connect(function()
	if cnode then 
		if m > .8 then
			m-= .2
		end
	end
end)

mouse.Move:Connect(function()
	if not cnode and mouse.Target then
		if mouse.Target.Parent == workspace.nodes then
			if mouse.Target.OwnerId.Value == lplr.UserId then
				bMouse.Visible = true
				bMouse.Position = UDim2.fromOffset(mouse.X,mouse.Y)
				return
			end
		end
	end
	bMouse.Visible = false
end)
local correct = function()
	local targetvec = mouse.Origin + mouse.Origin.LookVector*m
	local curvec = cnode.CFrame
	local dis = (curvec.Position-targetvec.Position).Magnitude
	local angle = CFrame.lookAt(curvec.Position,targetvec.Position)
	local line = workspace:FindFirstChild('line') or Instance.new('Part')
	local cpos = lplr.Character:WaitForChild('RightHand').CFrame
	line.Name = 'line' 
	line.CanCollide = false line.Anchored = true line.Size = Vector3.new(.1,.1,(cpos.Position-curvec.Position).Magnitude)
	line.Transparency = .6
	line.CFrame = 
		CFrame.lookAt(
			curvec.Position,cpos.Position) + 
		(
		CFrame.lookAt(curvec.Position,cpos.Position).LookVector *
		(cpos.Position-curvec.Position).Magnitude)/2
	line.Parent = workspace
	local sval = angle.LookVector*dis*m
	if dis < .1 then sval = Vector3.new(0,0,0) end
	cnode.Attachment.LinearVelocity.VectorVelocity = sval

end

mouse.Button1Down:Connect(function()
	local tnode = mouse.Target
	if tnode then
		if tnode.Parent == workspace.nodes then
			if auth:InvokeServer(tnode) then
				tatt.Orientation = tnode.Orientation
				local newt = Instance.new('AlignOrientation')
				newt.Parent = tnode newt.Name = 'newt'
				newt.Enabled = true newt.Attachment0 = tnode.Attachment newt.Attachment1 = tatt
				tnode.Transparency = .5
				sM = tnode.Material
				tnode.Material = Enum.Material.Neon
				tnode.Attachment.LinearVelocity.Enabled = true
				cnt = game["Run Service"].RenderStepped:Connect(correct)
				cnode = tnode
			end
		end
	end
end)


mouse.Button1Up:Connect(function()
	if cnode then
		-- Sets the client's view of the model back to regular.
		cnode.Transparency = 0
		cnode.Material = sM
		cnode.AssemblyAngularVelocity = Vector3.new(0,0,0)
		cnt:Disconnect()
		cnode.Attachment.LinearVelocity.VectorVelocity = Vector3.new(0,0,0)
		cnode.Attachment.LinearVelocity.Enabled = false
		cnode.newt:Destroy()
		if workspace:WaitForChild('line') then workspace:WaitForChild('line'):Destroy() end
		cnode = nil
	end
end)

Thats another topic, please mark my post as a solution and create a new topic for this and mention me so i can help you from there

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.