Network ownership bug with drag system

It’s been a day since i’ve made this drag system and i’ve encountered yet another issue.
Everytime i destroy the draggable part and try to drag another it doesn’t work properly for some reason, i figured it had to do something with network ownership cause i had some errors relating to that so.. please help

Code snippets
NETWORK

local remote = game.ReplicatedStorage:WaitForChild("DragRequest")

remote.OnServerInvoke = function(player, object, requestingPickup)
	
	if object and object:IsDescendantOf(game.Workspace) then

		if requestingPickup and not object:GetAttribute("dragOwner") then
			object:SetAttribute("dragOwner", player.UserId)
			object:SetNetworkOwner(player)
			return true

		elseif not requestingPickup and object:GetAttribute("dragOwner") == player.UserId then
			object:SetAttribute("dragOwner", nil)
			object:SetNetworkOwnershipAuto()
			return true
		end

		return false
	
	else
		
		return false
		
	end
end

CLIENT

local STRING_TAG = "Draggable"
local MAX_DISTANCE = 25

local currentTarget = nil
local currentObject = nil

local terrain = workspace:WaitForChild("Terrain")
local dragAtt = terrain:WaitForChild("DragAttachment")
local dragRequest = game.ReplicatedStorage:WaitForChild("DragRequest")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local player = game.Players.LocalPlayer

local scrollValue = 0

local partHighlight = script:FindFirstChild("Highlight")
if not partHighlight then
	partHighlight = Instance.new("Highlight")
	partHighlight.Parent = player:WaitForChild("PlayerGui")
end

local alignPosition = script.AlignPosition
local alignOrientation = script.AlignOrientation

alignPosition.Attachment1 = dragAtt
alignOrientation.Attachment1 = dragAtt

local function dropObject()
	if currentObject then
		pcall(function()
			dragRequest:InvokeServer(currentObject, false)
		end)
	end
	currentObject = nil
	alignPosition.Attachment0 = nil
	alignOrientation.Attachment0 = nil
end

UIS.InputChanged:Connect(function(input, gp)
	if gp then return end
	if input.UserInputType ~= Enum.UserInputType.MouseWheel then return end
	
	scrollValue += input.Position.Z
end)

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	if currentTarget and not currentObject then
		
		scrollValue = 0

		currentObject = currentTarget
		local partAtt = currentObject:FindFirstChild("PartAttachment")
		if not partAtt then
			partAtt = Instance.new("Attachment")
			partAtt.Name = "PartAttachment"
			partAtt.Parent = currentObject
		end

		local result = dragRequest:InvokeServer(currentObject, true)
		if result then
			alignPosition.Attachment0 = partAtt
			alignOrientation.Attachment0 = partAtt
		else
			currentObject = nil
		end
	else
		dropObject()
	end
end)

UIS.InputEnded:Connect(function(input, gp)
	if gp then return end
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	dropObject()
end)

RunService.RenderStepped:Connect(function()
	if currentObject then
		local partAtt = currentObject:FindFirstChild("PartAttachment")
		if not partAtt then
			partAtt = Instance.new("Attachment")
			partAtt.Name = "PartAttachment"
			partAtt.Parent = currentObject
		end

		alignPosition.Attachment0 = partAtt
		alignOrientation.Attachment0 = partAtt

		alignPosition.Parent = currentObject
		alignOrientation.Parent = currentObject

		local attPosition = workspace.CurrentCamera.CFrame * CFrame.new(0,0,-math.clamp((8 + scrollValue),5,15))
		dragAtt.CFrame = attPosition
	else
		dropObject()
		
		local mousePosition = UIS:GetMouseLocation()
		local ray = workspace.CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y)

		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Include
		rayParams.FilterDescendantsInstances = CollectionService:GetTagged(STRING_TAG)

		local raycast = workspace:Raycast(ray.Origin, ray.Direction * MAX_DISTANCE, rayParams)

		alignPosition.Parent = script
		alignOrientation.Parent = script

		if raycast and raycast.Instance then
			partHighlight.Adornee = raycast.Instance
			currentTarget = raycast.Instance
		else
			partHighlight.Adornee = nil
			currentTarget = nil
		end
	end
end)

I’d appreciate any type of help, thanks in advance

1 Like

Hi there, could you please send the error output?

1 Like

One thing to check: when you destroy the old part, make sure you fully clear out the attachments and reset network ownership before it goes. Sometimes if you try to set ownership on something that’s just been removed, Roblox bugs out and doesn’t update properly.

Also, maybe try adding a tiny delay (like task.wait()) before picking up a new part. Roblox seems to need a frame or two to catch up when you’re switching things fast like that.

And double check that the "dragOwner" attribute is actually getting removed when you drop it — I’ve had issues where it stays behind and blocks other interactions.

3 Likes

it seems fine? ignore the printed new trash

how can i reset the network ownership?

Resetting is just setting ownership to server (nil)

1 Like

also, your game is extremely exploitable, a cheater can request ownership of any BasePart within workspace and server will grant it, add a tag on Parts that are only pickable and check on server

1 Like

i’m a bit confused on how to clear the actual network ownership after the part is removed..

1 Like
on drop
part:SetNetworkOwnership(nil)
part:Destroy()
1 Like

i don’t really have time to care about that :head_shaking_vertically:
this is just for a gamejam, thanks for the info tho

then I aswell will not take you seriously, goodbye and goodluck

1 Like

i’ve modified the main system a bit but the bug still occurs, i don’t understand .. why?

LOCAL SCRIPT

local STRING_TAG = "Draggable"
local MAX_DISTANCE = 25

local currentTarget = nil
local currentObject = nil

local terrain = workspace:WaitForChild("Terrain")
local dragAtt = terrain:WaitForChild("DragAttachment")
local dragRequest = game.ReplicatedStorage:WaitForChild("DragRequest")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local player = game.Players.LocalPlayer

local scrollValue = 0

local partHighlight = script:FindFirstChild("Highlight")
if not partHighlight then
	partHighlight = Instance.new("Highlight")
	partHighlight.Parent = player:WaitForChild("PlayerGui")
end

local alignPosition = script.AlignPosition
local alignOrientation = script.AlignOrientation

alignPosition.Attachment1 = dragAtt
alignOrientation.Attachment1 = dragAtt


local function clearAttachments()
	alignPosition.Attachment0 = nil
	alignOrientation.Attachment0 = nil
end

local function validateObject()
	if not currentObject then return false end
	if not currentObject.Parent then return false end
	if not currentObject:IsDescendantOf(workspace) then return false end
	return true
end

local function dropObject()
	if currentObject then
		if validateObject() then
			pcall(function()
				dragRequest:InvokeServer(currentObject, false)
			end)
		end
	end
	currentObject = nil
	clearAttachments()
end

UIS.InputChanged:Connect(function(input, gp)
	if gp then return end
	if input.UserInputType ~= Enum.UserInputType.MouseWheel then return end
	scrollValue += input.Position.Z
end)

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	if currentTarget and not currentObject then
		scrollValue = 0
		currentObject = currentTarget
		local partAtt = currentObject:FindFirstChild("PartAttachment")
		if not partAtt then
			partAtt = Instance.new("Attachment")
			partAtt.Name = "PartAttachment"
			partAtt.Parent = currentObject
		end
		
		task.wait()

		local result = dragRequest:InvokeServer(currentObject, true)
		if result then
			alignPosition.Attachment0 = partAtt
			alignOrientation.Attachment0 = partAtt
		else
			currentObject = nil
		end
	else
		dropObject()
	end
end)

UIS.InputEnded:Connect(function(input, gp)
	if gp then return end
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	dropObject()
end)

RunService.RenderStepped:Connect(function()
	if currentObject then

		if not validateObject() then
			dropObject()
			return
		end

		local partAtt = currentObject:FindFirstChild("PartAttachment")
		if not partAtt then
			partAtt = Instance.new("Attachment")
			partAtt.Name = "PartAttachment"
			partAtt.Parent = currentObject
		end

		alignPosition.Attachment0 = partAtt
		alignOrientation.Attachment0 = partAtt

		alignPosition.Parent = currentObject
		alignOrientation.Parent = currentObject

		local attPosition = workspace.CurrentCamera.CFrame * CFrame.new(0,0,-math.clamp((8 + scrollValue),5,15))
		dragAtt.CFrame = attPosition
	else
		dropObject()

		local mousePosition = UIS:GetMouseLocation()
		local ray = workspace.CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y)

		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Include
		rayParams.FilterDescendantsInstances = CollectionService:GetTagged(STRING_TAG)

		local raycast = workspace:Raycast(ray.Origin, ray.Direction * MAX_DISTANCE, rayParams)

		alignPosition.Parent = script
		alignOrientation.Parent = script

		if raycast and raycast.Instance then
			partHighlight.Adornee = raycast.Instance
			currentTarget = raycast.Instance
		else
			partHighlight.Adornee = nil
			currentTarget = nil
		end
	end
end)

NETWORK

local remote = game.ReplicatedStorage:WaitForChild("DragRequest")

remote.OnServerInvoke = function(player, object, requestingPickup)
	
	if object and object:IsDescendantOf(game.Workspace) then

		if requestingPickup and not object:GetAttribute("dragOwner") then
			object:SetAttribute("dragOwner", player.UserId)
			object:SetNetworkOwner(player)
			return true

		elseif not requestingPickup and object:GetAttribute("dragOwner") == player.UserId then
			object:SetAttribute("dragOwner", nil)
			object:SetNetworkOwnershipAuto()
			return true
		end
		
		object:SetNetworkOwner(nil)
		return false
	
	else
		
		return false
		
	end
end

SCRIPT THAT DESTROYS

script.Parent.Touched:Connect(function(hit)
	print("touched", hit)
	if hit:HasTag("Trash") then
		hit:SetNetworkOwner(nil)
		task.wait()
		hit:Destroy()
	end
end)

I double checked a few things and made sure to reset the network owner but it doesn’t appear to effect anything..

1 Like

You likely destroy it while its still holding, on client listen to Part.Destroying and then set references to nil

and for gods sake stop pcalling remote functions

i just used pcall once for the remote function, is it really that bad :skull:

it serves no purpose for you, so yes its bad

2 Likes

i added the .destroying in the runservice but it.. still doesn’t affect anything
I’m concerned if the problem isn’t even in the network ownership itself, but i’ll try to find it out

when did i say to connect to it in RunService? now knowing that you have put it in RunService i can confidently say you are not ready for whatever gamejam you are doing this

i just figured to put it there because it’ll give an error if the currentObject is nil

although i know i’m really BAD at scripting, entering a gamejam is just for experience so theres no downsides to that lol

It’ll print out an error if it’s outside

1 Like

you connect to Destroying when u grab it :person_facepalming: so at that moment currentObject is not nil

so.. i guess in here?

local result = dragRequest:InvokeServer(currentObject, true)
		if result then
			alignPosition.Attachment0 = partAtt
			alignOrientation.Attachment0 = partAtt
			
			currentObject.Destroying:Connect(dropObject)
		else
			currentObject = nil
		end

it still doesn’t really affect the bug tho