How to make a part spawn if players are seated?

So, I have 8 seats inside a folder in workspace, and basically what I am trying to do is that if 8 players are sitting on these seats (not just 1 or 2 seats, all of the seats need to have someone sitting in them) at the same time, then something will happen, and this something will spawn a part (coin) from replicatedstorage as well as change the skybox (send it from replicatedstorage to lighting)

And after a player collects the coin (only 1 player can get it), so once someone touches it, that someone gets a badge and the coin despawns and no one else can collect it unless 8 players sit on all 8 seats at the same time again.

I’m unsure how to work with this but this is what I have so far: (I’m stuck on trying to figure out how to make it so that if 8 players are seated it works, I know using touched event is wrong but that’s were I started from and not sure where to go after from there):

Main ServerScript under ServerScriptService:

local rs = game:GetService("ReplicatedStorage")
local coin = rs:WaitForChild("BlackCoin") 
local remote = rs.Remotes.AwardBlackCoinBadge
local pos = game.Workspace:WaitForChild("BlackCoinTP")
local sound = game.Workspace.BlackCoinSound
local Folder = game.Workspace.Seats
local lighting = game.Lighting

local sky1 = rs.Sky
local atmosphere1 = rs.Atmosphere

local sky2 = rs.SkyDARK
local atmosphere2 = rs.AtmosphereDARK

local SeatsTable = {}

for i,v in pairs(Folder:GetChildren()) do
	table.insert(SeatsTable, v)
end 

for i,v in pairs(SeatsTable) do
	v.Touched:Connect(function()
		
		sound:Play()
		sky2.Parent = lighting
		atmosphere2.Parent = lighting
		sky1.Parent = rs
		atmosphere1.Parent = rs
		coin.Parent = workspace
		coin.Touched:Connect(function(part)
			if part.Parent:FindFirstChild("Humanoid") then
				remote:FireAllClients(game.Players:GetPlayerFromCharacter(part.Parent))
				coin.Parent = rs
				sky2.Parent = rs
				atmosphere2.Parent = rs
				sky1.Parent = lighting
				atmosphere1.Parent = lighting
				sound:Stop()
			end
		end)
		
	end)
end

Another ServerScript under ServerScriptService (Handles the remoteevent for giving badge to the 1 player who claims coin before others):

game.ReplicatedStorage.BadgeAwarder2.OnServerEvent:Connect(function(Player, userID, badgeID)
	game.BadgeService:AwardBadge(userID, badgeID)
end)

LocalScript under StarterPlayerScripts (Also handles remoteevent for giving badge):

local blackCoinRemote = game.ReplicatedStorage.Remotes.AwardBlackCoinBadge

blackCoinRemote.OnClientEvent:Connect(function(player)
	if player == game.Players.LocalPlayer then
		game.ReplicatedStorage.BadgeAwarder2:FireServer(player.UserId, 267475149527160)
	end
end)

Check if
seat.Occupant ~= nil for every seat
instead of using
.Touched

for i,v in pairs(SeatsTable) do
	if v.Occupant ~= nil then
1 Like

Just tried it in-game with 8 players and it doesn’t seem to work/do anything… How can I fix it?

No errors or anything in dev console :frowning_face:

I tried running the mainscript by just copy pasting it in dev console and it only worked once then when I tried running it again this error came up:
RobloxPlayerBeta_HXnD364aMi

(Also the script didn’t work as intended it only worked when I ran it in dev console, and there weren’t even 8 people sitting on all seats when I ran it)

--[[ Roblox Services ]]--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LightingService = game:GetService("Lighting")
local PlayerService = game:GetService("Players")

--[[ Main Instances ]]--
local RemoteFolder = ReplicatedStorage:WaitForChild("Remotes") 
local SeatFolder = workspace:WaitForChild("Seats")

--[[ Black Coin Instances ]]--
local CoinObject = ReplicatedStorage:WaitForChild("BlackCoin") 
local CoinRemoteBadge = RemoteFolder and RemoteFolder:WaitForChild("AwardBlackCoinBadge")
local CoinPosition = workspace:WaitForChild("BlackCoinTP")
local CoinSound = workspace:WaitForChild("BlackCoinSound")

--[[ Lighting Instances ]]--
local Skybox1 = LightingService:WaitForChild("Sky")
local Skybox2 = ReplicatedStorage:WaitForChild("SkyDARK")

local Atmosphere1 = LightingService:WaitForChild("Atmosphere")
local Atmosphere2 = ReplicatedStorage:WaitForChild("AtmosphereDARK")

-----------------------------------------------------------------------------

--[[ Local Functions ]]--
local function CoinCollected(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		CoinRemoteBadge:FireAllClients()
		CoinObject.Parent = ReplicatedStorage
		Skybox1.Parent = LightingService
		Skybox2.Parent = ReplicatedStorage
		Atmosphere1.Parent = LightingService
		Atmosphere2.Parent = ReplicatedStorage
		CoinSound:Stop()
	end
end

local function CheckSeats()
	local AllSeatOccuped = true
	
	for _, CurrentSeat in pairs(SeatFolder:GetChildren()) do
		if CurrentSeat.Occupant == nil then
			AllSeatOccuped = false
		end
	end
	
	if AllSeatOccuped == true then
		CoinSound:Play()
		Skybox1.Parent = ReplicatedStorage
		Skybox2.Parent = LightingService
		Atmosphere1.Parent = ReplicatedStorage
		Atmosphere2.Parent = LightingService
		CoinObject.Parent = workspace
		CoinObject.CFrame = CoinPosition.CFrame
	end
end

-----------------------------------------------------------------------------

--[[ Main Connections ]]--
CoinObject.Touched:Connect(CoinCollected)

--[[ Seats Setup ]]--
for _, CurrentSeat in pairs(SeatFolder:GetChildren()) do
	CurrentSeat:GetPropertyChangedSignal("Occupant"):Connect(CheckSeats)
end