Unanchored all parts in game when a certain player a certain amount of players join the server

So I want to basically Unanchor all parts in the game when a certain amount of players join the game. It’s pretty basic but my mind cant make the code…

Server Script in ServerScriptService

local UNANCHOR_AT = 5

game:GetService("Players").PlayerAdded:Connect(function()
    task.wait(1)
    
    local Players = #game:GetService("Players"):GetChildren()

    if (Players >= UNANCHOR_AT) then 
        for i,v in pairs(game:GetChildren()) do 
            if (v:IsA("Part")) then 
                v.Anchored = false
            end
        end
    end
end)

It gives me this error I put it in a script in the server script service

I would only check for parts under Workspace rather than the entire game.

Untested code, but this should be what you’re looking for with some explanation.

Code
local req_players = 5 -- Set to your desired amount of players that must be in game.
local Players = game:GetService("Players") -- Obtain players service. 

Players.PlayerAdded:Connect(function() -- Set up player added event.
	local players_in_game = #Players:GetPlayers() -- Variables for how many players are in game.
	if players_in_game >= req_players then -- Set up our condition for unanchoring. 
		for _, instance in pairs(workspace:GetDescendants()) do -- Gather everything within workspace.
			if instance:IsA("BasePart") then -- Check if each instance is a part.
				instance.Anchored = false -- Unanchor them.
			end
		end
	end
end)
1 Like
game:GetService("Players").PlayerAdded:Connect(function(Player)
	if #game:GetService("Players"):GetPlayers() >= 10 then -- change
		for _,v in workspace:GetDescendants() do
			if v:IsA("BasePart") and v.Anchored == true then
				v.Anchored = false
			end
		end
	end
end)

Is it possible to make it so the baseplate remains anchored

Yes.

game:GetService("Players").PlayerAdded:Connect(function(Player)
	if #game:GetService("Players"):GetPlayers() >= 10 then -- change
		for _,v in workspace:GetDescendants() do
			if v:IsA("BasePart") and v.Anchored == true and tostring(v) ~= "Baseplate" then
				v.Anchored = false
			end
		end
	end
end)
1 Like

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