Item is being released when it nears a player

Basically i have a script in which when somebody grabs the item, it fires a remotevent and the script on the server adds an item to the grabbed object. in the localscript if the item is detected in the object, it can’t be grabbed. However i seem to have a problem with when a player nears the object, It gets released and i’ve got no clue why.

https://gyazo.com/10042e564c08280e6bc2587263ee9866
NOTE: When the item gets highlighted after it rolls under the second player, im trying to pick it back up but unable to, However, I’m able to pick it back up once the ball gets out of the set range which is 8 studs

local plr = game.Players.LocalPlayer
local char = plr.Character
local grabbing = false
local grabbeditem = nil
local facing = false
local dist = 8
local userinputservice = game:GetService('UserInputService')
local mouse = plr:GetMouse()
local camera = workspace.CurrentCamera
local highlightobject = nil

repeat wait() until plr.Character

mouse.Button1Down:Connect(function()
	if mouse.Target and mouse.Target.Parent:IsA("Model") and mouse.Target.Parent.PrimaryPart then
		if not game.Players:FindFirstChild(mouse.Target.Parent.Name) and not mouse.Target.Parent.PrimaryPart:FindFirstChild("IsGrabbed") then
			grabbing = true
			grabbeditem = mouse.Target.Parent.PrimaryPart
			game.ReplicatedStorage.ItemGrabbed:FireServer(grabbeditem)
			local bp = Instance.new("BodyPosition", grabbeditem)
			bp.D = 150
			bp.P = 1000
			grabbeditem.Parent.Outline.Transparency = 0
			if grabbeditem.Name == "Disc" then
			local bf = Instance.new("BodyForce", grabbeditem)
			bf.Force = Vector3.new(0, grabbeditem:GetMass() * game.Workspace.Gravity, 0)
			end
		end
	end
end)

mouse.Button2Down:Connect(function()
	if grabbing == true then
		facing = true
		local bg = Instance.new("BodyGyro", grabbeditem)
		bg.MaxTorque = Vector3.new(100000000, 100000000, 100000000)
	end
end)

mouse.Button2Up:Connect(function()
	facing = false
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
		grabbeditem.BodyGyro:Destroy()
	end
end)

mouse.Button1Up:Connect(function()
	grabbing = false
	if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
		game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem)
		grabbeditem.Parent.Outline.Transparency = 1
		grabbeditem.BodyPosition:Destroy()
	end
	
	if grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
		grabbeditem.BodyGyro:Destroy()
	end
	
	
	wait(math.random(1, 2.5))
	if grabbeditem and grabbeditem:FindFirstChild("BodyForce") then
		grabbeditem.BodyForce:Destroy()
	end
end)

while wait() do 
	if grabbing == true and grabbeditem ~= nil then
		if (plr.Character.PrimaryPart.Position - grabbeditem.Position).Magnitude < dist * 1.2 then
			grabbeditem.BodyPosition.Position = ((camera.CFrame * CFrame.new(0,0,-dist)) ).Position
			if facing == true and grabbeditem:FindFirstChild("BodyGyro") then
			grabbeditem.BodyGyro.CFrame = CFrame.new(grabbeditem.Position, plr.Character.PrimaryPart.Position)
			end
		else
			grabbing = false
			if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
				grabbeditem.BodyPosition:Destroy()
			elseif grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
				grabbeditem.BodyGyro:Destroy()
			end
		end
			else
				if mouse.Target ~= nil then
					if mouse.Target.Parent:IsA("Model") then
					if not game.Players:FindFirstChild(mouse.Target.Parent.Name) and mouse.Target.Parent.PrimaryPart  then
					if (plr.Character.PrimaryPart.Position - mouse.Target.Parent.PrimaryPart.Position).Magnitude < dist * 1.2 and not mouse.Target.Parent.PrimaryPart:FindFirstChild("IsGrabbed") then
					if highlightobject and mouse.Target ~= highlightobject then
						highlightobject.Transparency = 1
					end
					highlightobject = mouse.Target.Parent.Outline
					highlightobject.Transparency = 0
					end
				else
					if highlightobject then
						highlightobject.Transparency = 1
					end
			end
		
				else
				if highlightobject ~= nil then
				highlightobject.Transparency = 1
			end
		end
	end	
	end
end

Here’s the script if anybody can find anything wrong that i can’t.

This might be because the other player is claiming network ownership of the ball as it nears them. Try setting the ball’s network owner to the dragging player on the server before letting them drag it with BasePart:SetNetworkOwner.

2 Likes

Nevermind, It Worked that’s awesome! Thank you so much!