Grab Mechanic difficulties

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I’m working on a simple grab mechanic (similar to what Lumber Tycoon 2) has. For this I’m taking advantage of the align position instance, and I’ve been happy with my results so far.

  2. What is the issue?
    An issue I encountered when testing it with multiplayer was that if I lifted an object, close to a player, it would for some reason just fall to the ground, and I wouldn’t be able to move it at all until the other player walks away from the object.
    Clip, Edit, and Share Your Game Clips & Gameplay - Medal

  3. What solutions have you tried so far?
    I’ve spent a few hours going through chatGPT, documentation, friends and allat to hopefully find a solution. I’ve tried changing the collision, alignPosition mode, maxForce, Responsiveness however to no help.

The ONE thing that actually had a slight effect, was making the object I want to move massless. However, I don’t want it to be massless, and it created a further issue where other players would not be able to see you move the object until you let go of it. I have no idea what caused this but I ended up trying to focus on fixing the issue without making it massless, however to no avail.

The code is a bit messy and as I’ve mainly been trying to get a hang of the mechanic before “optimizing” it to look well however I do hope I can get this solved without having to resolve to something completely else.

-- Services
local userInputService = game:GetService("UserInputService")

-- Player Variables
local camera = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local char = plr.Character
local humanoid = char:WaitForChild("Humanoid")
local head = char:WaitForChild("Head")
local mouse = plr:GetMouse()

-- Configurables
local grabDistance = 20
raycastFilter = {plr.Character, game.Workspace}

-- Bools
local grabbed = false

-- Instances
local grabbedObject = nil

holdDistance = 3.5


local function whileGrabbed()
	local mouseRay = mouse.UnitRay
	local targetPos = head.CFrame.Position + camera.CFrame.LookVector * holdDistance
	local mouseOffset = mouseRay.Direction * 8
	local finalPos = targetPos + mouseOffset
	alignPos.Position = finalPos
end

local function createOrb()
	ball = game.Workspace.CloneThis:Clone()
	ball.Parent = game.Workspace.Ball
	ball.Transparency = 0
	ball.Name = "ugh"
	ball.Anchored = false
	ball.Position = mouse.Hit.Position
end

local function grabObject()
	if not mouse.Target then return end
	if not mouse.Target:HasTag("Amogus") then return end
	createOrb()
	grabbedObject = mouse.Target
	
	weldCon = Instance.new("WeldConstraint", grabbedObject)
	weldCon.Part0 = grabbedObject
	weldCon.Part1 = ball
	
	attachment0 = Instance.new("Attachment", grabbedObject)
	alignPos = Instance.new("AlignPosition", grabbedObject)
	alignPos.Mode = Enum.PositionAlignmentMode.OneAttachment
	alignPos.Attachment0 = attachment0
	alignPos.MaxForce = 9999999442119689768320106496
	alignPos.Responsiveness = 14
	alignPos.ApplyAtCenterOfMass = true
	
	grabbed = true
	while grabbed == true do
		wait()
		whileGrabbed()
	end
	
end

userInputService.InputBegan:Connect(function(input, gPE)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not gPE then
		if grabbed == true then return end
		grabObject()
	end
end)

userInputService.InputEnded:Connect(function(input, gPE)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not gPE then
		if grabbed == true and grabbedObject then
			grabbed = false
			grabbedObject = nil
			attachment0:Destroy()
			alignPos:Destroy()
			ball:Destroy()
			weldCon:Destroy()
		end
	end
end)
2 Likes

I’m gonna take a guess and say it’s got to do with network ownership. Do :SetNetworkOwner(Player) on whatever the client is trying to grab.

1 Like

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