LocalScript randomly working in workspace

For some reason, my localscript is randomly working in workspace without setting the model ownership to the player.
image

I tested a print in the local script without setting the network ownership and it printed.

How can I fix this? I thought local scripts only work if the ownership is set first.

The script is set to client:
image

NetworkOwner:

local model = script.Parent

local function setNetworkOwner(owner)
	for i,v in ipairs(model:GetDescendants()) do
		if v:IsA("BasePart") then
			local result = v:CanSetNetworkOwnership()
			print(result) <-- true
			v:SetNetworkOwner(owner)
		end
	end
end

script.SetNetworkOwner.OnServerEvent:Connect(function(player)
	setNetworkOwner(player)
end)

Client Handler:

script.Parent.SetNetworkOwner:FireServer()
1 Like

once it is not a script you turned into a local script you will get the results you want
so try adding a local script and not by adding a script and changing it to client

3 Likes

I have tried using a local script but that doesn’t work in the workspace.

2 Likes

This is intended behaviour, scripts with their RunContext set to client will run for each connected client regardless of their parent. If you don’t want the script to run, disable it or add a LocalScript, not a script with RunContext client.

1 Like

But local scripts don’t run in workspace.

I have done this with other models and it works but this one doesn’t work.

You don’t have a LocalScript, you have a script with RunContext set to client, they’re two different things. I’m not too sure what you’re asking, do you want the script to run when the parts have their network owner changed?

I have tried using a regular local script. The network script works.

Like I said I’m not too sure what you’re asking here, what are you expecting to happen?

I want the client script to run only if the model ownership is set to the player.

I see, in that case you could probably just create a remote, then fire the client when the network ownership is set to them. Then on the client in the connected function, do what it is you want to happen. There isn’t any part.NetworkOwnershipGained event so you would need to make a custom implementation.

Here is the output, as you can see, the client prints first than the network owner server:
image

Yes because on the client you are telling the server to set network ownership of the part to the client, it happens in the correct order. The local script will run for each connected client, regardless of network ownership.

image

Fixed by doing this:

local model = script.Parent.Parent:WaitForChild("Street Light")

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