Script that checks occupants in seats is not working

  1. What do you want to achieve? Keep it simple and clear!

I want the game to count players as dead if they are not sitting in a helicopter seat.

  1. What is the issue? Include screenshots / videos if possible!

The game is saying that the player is and isn’t sitting in the seat.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have already asked for help on the developer forum but only one person tried to help and they couldn’t fix it, so I am posting this again so hopefully, more people can see this and help me if possible (The last post I made for this seeking help was made last Friday by the way)

for i = 1, #people do 
			if people[i].Dead.Value == false then
				if workspace:FindFirstChild("Rescue"..game.ReplicatedStorage.GM.RescueType.Value) then
					for _, seat in next, workspace:FindFirstChild("Rescue"..game.ReplicatedStorage.GM.RescueType.Value):FindFirstChildOfClass("Model"):GetChildren() do
						if seat:IsA("Seat") and seat.Occupant then
							if seat.Occupant.Parent == people[i].Character then
								print(people[i].Name.." is in the heli, they made it.")
								if game.ReplicatedStorage.GM.Winners.Value == "The Survivors are:" then
									game.ReplicatedStorage.GM.Winners.Value = game.ReplicatedStorage.GM.Winners.Value.." "..people[i].Name
								else
									game.ReplicatedStorage.GM.Winners.Value = game.ReplicatedStorage.GM.Winners.Value..", "..people[i].Name
								end
							elseif people[i].Character ~= seat.Occupant.Parent then
								people[i].Dead.Value = true
								print(people[i].Name.." did not make it to the heli!")
							end
						end
					end
				end
			end
		end

Here is the output:
image

Are you looping through a list of people and through the seats every single time? This probably happens because you are going through two different loops. For instance, if we had Player A, B, and C, and seats A, B, and C.

In the first loop, our player will be Player A. Now, it starts looping through each seat as well. We are now in Player A and Seat A. Since Player A is on Seat A, it will say that the player is in the helicopter. But, now that we moved to Seat B, we have not moved on from Player A, so since Seat B is not occupied by Player A, it will say that the player did not make it and it marks them as dead. Same for Seat C.

Now we are in the second loop and we have Player B. Now, it starts looping through each seat as well. We are now in Player B and Seat A. The problem here is though is that Seat A would be occupied by Player A (who is now dead anyways), so it would mark Player B as dead, and from then on, it would mark him as a winner, and then dead again.

I don’t even need to discuss Player C, since it is basically the same as Player B. In short, you need to either: 1) Mark a player as dead ONLY after every seat has been looped through and the player has not been found sitting in any of them (as seen by the reply below from @ZombieCrunchUK), or 2) Loop through each seat, mark every occupant, if there are any, and mark the rest as dead (as seen by my example below).

I highly advice you use the second option since it saves resources and is overall less of a pain to work with. Here is how that would look like:

local playerService = game:GetService("Players")

if workspace:FindFirstChild("Rescue"..game.ReplicatedStorage.GM.RescueType.Value) then
	local playersSitting = {}
	for _, seat in next, workspace:FindFirstChild("Rescue"..game.ReplicatedStorage.GM.RescueType.Value):FindFirstChildOfClass("Model"):GetChildren() do
		if seat:IsA("Seat") and seat.Occupant then
			local player = playerService:GetPlayerFromCharacter(seat.Occupant)
			if player and player.Dead.Value == false then
				table.insert(playersSitting, player)
			end
		end
	end
	
	for _, player in pairs(playersSitting) do
		if game.ReplicatedStorage.GM.Winners.Value == "The Survivors are:" then
			game.ReplicatedStorage.GM.Winners.Value = game.ReplicatedStorage.GM.Winners.Value.." "..player.Name
		else
			game.ReplicatedStorage.GM.Winners.Value = game.ReplicatedStorage.GM.Winners.Value..", "..player.Name
		end
	end
	
	for i = 1, #people do
		local index = table.find(playersSitting, people[i])
		if index == nil then
			people[i].Dead.Value = true
		end
	end
end

Now, this code is by no means efficient or clean. I simply reordered your code in order to work. This has not been tested, so please let me know if you get any errors. Hopefully this helped.

2 Likes

Give this a try:

local GM=game.ReplicatedStorage.GM

local function IsPlayerInSeat(Plr, Seats)
	
	for _,seat in Seats do 
		if seat:IsA("Seat") and seat.Occupant and seat.Occupant.Parent == Plr.Character then return true end
	end
	return false
	
end

for i = 1, #people do 
	if people[i].Dead.Value == false then
		if workspace:FindFirstChild("Rescue"..GM.RescueType.Value) then
			local MadeIt=IsPlayerInSeat(people[i], workspace:FindFirstChild("Rescue"..GM.RescueType.Value):FindFirstChildOfClass("Model"):GetChildren())
			if MadeIt then
				print(people[i].Name.." is in the heli, they made it.")
				if GM.Winners.Value == "The Survivors are:" then
					GM.Winners.Value = GM.Winners.Value.." "..people[i].Name
				else
					GM.Winners.Value = GM.Winners.Value..", "..people[i].Name
				end
			else 
				people[i].Dead.Value = true
				print(people[i].Name.." did not make it to the heli!")
			end
	    end
    end
end
2 Likes
local players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local gm = rs:WaitForChild("GM")
local rt = gm:WaitForChild("RescueType")
local winners = gm:WaitForChild("Winners")

local function whoMadeIt()
	local isInHeli = {}
	for _, player in ipairs(players:GetPlayers()) do
		local name = player.Name
		local character = player.Character
		local dead = player:FindFirstChild("Dead")
		if dead then
			if dead.Value == false then
				local rescueFolder = workspace:FindFirstChild("Rescue"..rt.Value)
				if rescueFolder then
					local rescueModel = rescueFolder:FindFirstChildOfClass("Model")
					if rescueModel then
						for _, seat in ipairs(rescueModel:GetChildren()) do
							if seat:IsA("Seat") then
								if seat.Occupant then
									if seat.Occupant.Parent == character then
										table.insert(isInHeli, name)
										print(name.." is in the helicopter, they made it!")
									end
								end
							end
						end
					end
				end
			end
		end
		if not table.find(isInHeli, name) then
			print(name.." is not in the helicopter, they did not make it!")
			local dead = player:FindFirstChild("Dead")
			if dead then
				dead.Value = true
			end
		end
	end

	if #isInHeli == 0 then
		winners.Value = "No one survived!"
	else
		winners.Value = "The survivors are: "..table.concat(isInHeli, ", ")
	end
end
1 Like

Your issue was occurring because if the player was not in one of the many available seats they were presumed to be dead even if they were in another seat, I’ve used a table and made a lot of other changes to remedy this.

1 Like
if seat:IsA("Seat") and seat.Occupant and seat.Occupant.Parent == Plr.Character then return true end

This is going to error if the instance represented by the variable “seat” does not have a valid member named “Occupant”. There are many other issues which needed changing from the original source code as well.

1 Like

I will try these options later when I get home. Thanks for the help

2 Likes

I just tested this, it doesn’t work sadly. I also didn’t get any error messages. It didn’t count anyone as alive and counted everyone as dead.

This also does not work, Players that are not in the helicopter are still getting points and are getting counted as alive.

ahh, my bad - change this:

for _,seat in Seats do

to

for _, seat in ipairs(Seats) do
1 Like

That fixed it and now it seems to work! Thank you so much for your help on this. Same with everyone else who attempted to help.

1 Like

It’s a function so it won’t work unless called.

1 Like

The last bit which you decided to cross out will error if the player has left the game/died, there’s a lot of potential issues with this code.

I was also referring to a member of any instance named “Occupant” not it being nil itself. If you attempt to access an invalid member of an instance the script will error.

1 Like