Currently I want to assign a Part’s NetworkOwnership to the player closest to it in-game. How would I go about doing this?
I know this may not be necessarily an efficient way of doing this, but here’s what I have tried:
local Part = game.Workspace:FindFirstChild("Part")
Part.Changed:Connect(function()
print("Part Can Have Network Ownership: " .. tostring(Part:CanSetNetworkOwnership()))
print("Current Network Owner: " .. tostring(Part:GetNetworkOwner()))
end)
while wait() do
for i,v in pairs(game.Workspace:GetDescendants()) do
if v:IsA("BasePart") and v.Name == "HumanoidRootPart" then
if math.floor((Part.Position - v.Position).Magnitude) <= 20 then
local PlayerName = v:FindFirstAncestorWhichIsA("Model").Name
for _, obj in pairs(game.Players:GetChildren()) do
if obj.Name == PlayerName then
Part:SetNetworkOwner(obj)
end
end
end
end
end
end
The problem with this Script is that it doesn’t find the closest player; rather, it just switches the NetworkOwner between players that are within a certain zone. I would like to set it to whoever is CLOSEST to the part, not within a certain radius.
This is a ServerScript, its Parent is ServerScriptService. I’ve looked on the DevForum multiple times but can’t find a specific scenario made for something like this. Any help would be greatly appreciated. Thank you for your time.