Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a script that changes amount of parts in a model based off player count?

So basically I’m looking for a way to make a script which will

If there are 10 players say and I wanted say 5 parts I have a player count value and when it’s 10 remove number amount of parts until there are 5 parts. And if 7 players let’s say 3 parts, vice versa. Let’s say these parts are seats and I want to check if someone is sitting on that seat and not remove it if they are. Here’s some scrap I constructed before which might show you what I’m looking for.


local count = script:FindFirstChild("Players")
local seats = game.Workspace.PlaneSeats
local remainingseats = script:FindFirstChild("RemainingSeats")

game.Players.ChildAdded:Connect(function(player)
    count.Value = count.Value + 1 
    print(count.Value)

    if count.Value == 10 then

        repeat wait()
        if seats.Seat1.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat1.Transparency = 1 
            seats.Seat1.CanCollide = false
            seats.Seat1.Archivable = false
            seats.Seat1.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        elseif seats.Seat2.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat2.Transparency = 1 
            seats.Seat2.CanCollide = false
            seats.Seat2.Archivable = false
            seats.Seat2.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        elseif seats.Seat3.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat3.Transparency = 1 
            seats.Seat3.CanCollide = false
            seats.Seat3.Archivable = false
            seats.Seat3.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        elseif seats.Seat4.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat4.Transparency = 1 
            seats.Seat4.CanCollide = false
            seats.Seat4.Archivable = false
            seats.Seat4.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        elseif seats.Seat5.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat5.Transparency = 1 
            seats.Seat5.CanCollide = false
            seats.Seat5.Archivable = false
            seats.Seat5.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        elseif seats.Seat6.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat6.Transparency = 1 
            seats.Seat6.CanCollide = false
            seats.Seat6.Archivable = false
            seats.Seat6.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        elseif seats.Seat7.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat7.Transparency = 1 
            seats.Seat7.CanCollide = false
            seats.Seat7.Archivable = false
            seats.Seat7.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        elseif seats.Seat8.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat8.Transparency = 1 
            seats.Seat8.CanCollide = false
            seats.Seat8.Archivable = false
            seats.Seat8.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        elseif seats.Seat9.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat9.Transparency = 1 
            seats.Seat9.CanCollide = false
            seats.Seat9.Archivable = false
            seats.Seat9.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        elseif seats.Seat10.SeatPart.Occupant ~= "Humanoid" then
            seats.Seat10.Transparency = 1 
            seats.Seat10.CanCollide = false
            seats.Seat10.Archivable = false
            seats.Seat10.SeatPart.Disabled = true
            remainingseats.Value = remainingseats.Value - 1
        end

    until remainingseats.Value == 1
        print("Done!")
    end
end)

game.Players.ChildRemoved:Connect(function(player)
    count.Value = count.Value - 1
    print(count.Value)
end)

2 answers

First of all use the proper events: PlayerAdded and PlayerRemoving

and instead of counting each player and removing 1 each time someones leave just count total players in game each time someone joins like thiscount.Value = #game.Players:GetPlayers()

and not sure if i misunderstanded you but here we go:

local amount ={ 10, 7 } -- not sure what u what wanted with this

local whichAmount = 1

local count = script:FindFirstChild("Players") -- why do you keep values inside an actual intValue thats parented to the script and not just in a variable ???
local seats = game.Workspace.PlaneSeats
local remainingseats = script:FindFirstChild("RemainingSeats") -- what tf is this

game.Players.PlayerAdded:Connect(function(player)
	count.Value = #game.Players:GetPlayers()
    	print(count.Value)

	if count.Value == amount[whichAmount]  then
		
		local minimum = math.floor(amount[whichAmount]/2) -- 10 becomes 5, 7 becomes 3
		-- but for what do you even need it ?
		for i = 1,amount[whichAmount] do -- does the amount of players affect the amount of seats ???
			local seat = seats["Seat"..i]
			 if seat.SeatPart.Occupant ~= "Humanoid" then -- i think you wanted to make all seats that arent used to be not visible
				seat.Transparency = 1 
				seat.CanCollide = false
				seat.Archivable = false -- do you even know what Archivable does ??
				seat.SeatPart.Disabled = true
				minimum -= 1
		end
		print("Done")
	end

end)

this is what Archivable does

all this was code in chat so if there is mistake just comment it

4 comments

1

Good job, I hope he will take your comment into a count because it's well written. I went with a different approach, I made almost ready code which might help him now but not in the long term as you did.

0

It cant hurt to post your approach maybe he ll learn something from it too, thanks for the comment

0

It's just under your post

0

You was fiew mins faster ;)

I have noticed many bad practices, such as not using a loop in your code. I think I understood you so I made a simple code that can help you.

Note: You should understand all of the code because It's based on your script, in case you don't write it down in the comments.

Code:

local Players = game:GetService("Players")
local Seats = game.Workspace:WaitForChild("PlaneSeats"):GetChildren()


local RemoveSeats = function() -- When called { You call it by doing RemoveSeats() }, it removes all excess saets that people are not sitting on.
	local PlayerCount = #Players:GetPlayers()
	local SeatsToRemove = #Seats-PlayerCount 
	
	for i,Seat in pairs(Seats) do
		if not Seat.SeatPart.Occupant then
			if SeatsToRemove > 0 then
				Seat.Transparency = 1
				Seat.CanCollide = false
				Seat.SeatPart.Transparency = 1
				Seat.SeatPart.CanCollide = false
				Seat.SeatPart.Disabled = true
				SeatsToRemove = SeatsToRemove-1
			end
		end
	end
end

local Refresh = function() -- When called { You call it by doing Refresh() }, it resfreshes all seats (makes them visible and usable again)
	for i,Seat in pairs(Seats) do		
		Seat.Transparency = 0
		Seat.CanCollide = true
		Seat.SeatPart.Transparency = 0
		Seat.SeatPart.CanCollide = true
		Seat.SeatPart.Disabled = false
	end
end

Testing I did: https://gyazo.com/ecc1443e29c5e6ea444f4e31411ef7db

This discussion is preserved as it was. New answers, comments, and votes are closed.