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

It wont remove player from table? [SOLVED]

So I'm making an elevator game and I made a lobby so players choose to go into the elevator and not get teleported so I made a table to access all the players detect if they are in the elevator but I wanted to make when they die they get removed from the table so they won't get teleported but my code won't work, can someone help?

Here's my code its at line 55

-- Variables
local elevatorFolder = workspace:WaitForChild("Elevator")
local elevator = workspace:WaitForChild("Elevator"):WaitForChild("Elevator")
local replicatedStorage = game:GetService("ReplicatedStorage")
local floorsFolder = replicatedStorage:WaitForChild("Floors")
local floors = floorsFolder:GetChildren()
local AvailableMusic = workspace:WaitForChild("ElevatorMusic"):GetChildren()
local randomMusic = AvailableMusic[math.random(1,#AvailableMusic)]
open = false
door1 = elevatorFolder:WaitForChild("Door1")
door2 = elevatorFolder:WaitForChild("Door2")

local TweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)

local Door1Open = {CFrame = door1.CFrame + door1.CFrame.lookVector * 4}
local Door2Open = {CFrame = door2.CFrame + door2.CFrame.lookVector * 4}
local Door1Close = {CFrame = door1.CFrame}
local Door2Close = {CFrame = door2.CFrame}

local Open1 = TweenService:Create(door1,tweenInfo,Door1Open)
local Open2 = TweenService:Create(door2,tweenInfo,Door2Open)
local Close1 = TweenService:Create(door1,tweenInfo,Door1Close)
local Close2 = TweenService:Create(door2,tweenInfo,Door2Close)
local ElevatorPart = workspace:WaitForChild("Lobby"):WaitForChild("TeleportInElevator")

local function openDoors()
	if open == false then
		Open1:Play()
		Open2:Play()
		wait(2)
		open = true
	end
end

local function closeDoors()
	if open == true then
		Close1:Play()
		Close2:Play()
		wait(2)
		open = false
	end
end

local plrs = {}

ElevatorPart.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum ~= nil then
		hit.Parent.HumanoidRootPart.CFrame = CFrame.new(workspace:WaitForChild("Lobby"):WaitForChild("TPPart").Position)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		wait(3)
		hit.Parent.HumanoidRootPart.CFrame = CFrame.new(workspace:WaitForChild("Elevator"):WaitForChild("TPPart").Position)
		if player then
			table.insert(plrs,player)
		elseif player.Character.Humanoid.Died then
			table.remove(plrs,player)
		end
	end
end)


while true do
	randomMusic:Play()
	wait(14)
	randomMusic:Stop()
	local randomFloor = floors[math.random(1,#floors)]
	local clonedFloor = randomFloor:Clone()
	local timeLimit = clonedFloor:WaitForChild("TimeLimit").Value
	clonedFloor.Parent = workspace
	workspace:WaitForChild("DoorClosing"):Play()
	openDoors()
	wait(timeLimit)
	workspace:WaitForChild("DoorClosing"):Play()
	closeDoors()
	randomMusic:Play()
	clonedFloor:Destroy()
	for i,v in pairs(plrs) do
		v.Character:MoveTo(Vector3.new(-32.67, 7.331, 85.424))
		v.Stats.Tokens.Value = v.Stats.Tokens.Value + 1
	end
end

1 comment

0

If people are wondering what the solution was, I had to do table.remove(plrs, table.find(plrs, player))

2 answers

Try this:

local plrs = {}
local Debug = true -- Flip this to false if you don't want any debug info in your output!
ElevatorPart.Touched:Connect(function(hit)
	if(hit.Parent == nil) then return end
    local hum = hit.Parent:FindFirstChild("Humanoid")
	local HRP = hit.Parent:FindFirstChild("HumanoidRootPart")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	--// if above checks come back as nil then stop executing script further!
	if(hum == nil) then 	if(Debug == true) then warn("Warning: "..tostring(hit.Parent).." Has no Humanoid Object! \n ignoring!") 	end return end
	if(HRP == nil) then 	if(Debug == true) then warn("Warning: "..tostring(hit.Parent).." Has No HumanoidRootPart! \n ignoring!") end return end
	if(player == nil) then 	if(Debug == true) then warn("Warning: "..tostring(hit.Parent).." Is not a player! \n ignoring!") 		end return end
	
	hit.Parent.HumanoidRootPart.CFrame = CFrame.new(workspace:WaitForChild("Lobby"):WaitForChild("TPPart").Position)
	wait(3)
	hit.Parent.HumanoidRootPart.CFrame = CFrame.new(workspace:WaitForChild("Elevator"):WaitForChild("TPPart").Position)
	if hum.Health > 0 then
		table.insert(plrs,player)
	else
		table.remove(plrs,player)
    end
end)

I just rewrote your Touched function to include a simple debugger. Hope this helps!

5 comments

0

Sorry it didn't work

0

Updated my answer with better code. Should work now! :)

0

Still doesn't work

0

what doesn't work?, where does it fail?

0

It's ok bro, I figured it out

remove that part and add somewhere:

function playerAdded(player)
	local function characterAdded(character)
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.Died:Wait()
			if table.find(plrs,player) then
				table.remove(plrs,player)
			end
		end
	end
	player.CharacterAdded:Connect(characterAdded)
end
game.Players.PlayerAdded:Connect(playerAdded)

i promise you there is probably some whay you could do this alot quicker

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