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

Locating specific information on a table?

Okay so I have a table in a script that contains the players. And when they leave, if they are part of the table then remove. Is this possible? I attempted with....



gamePlayers = {}
playersInGame = 0

function addPlayers()
	gamePlayers = {}
	playersInGame = 0
	for _, player in pairs(game.Players:GetChildren()) do
		if player:FindFirstChild("PlayerValues").ReadyToPlay.Value == true then
			table.insert(gamePlayers, player)
			playersInGame = playersInGame + 1
		end
	end
end

function search(tab, val)
    for i, v in pairs(tab) do
        if val == v then
            return i
        end
    end
end


game.Players.PlayerRemoving:connect(function(leavingPlayer)
	local i = search(gamePlayers, leavingPlayer)
	if i then
		table.remove(gamePlayers, i)
		print(leavingPlayer.Name.." Has Left")
		playersInGame = playersInGame - 1
	end
end)

repeat wait(1)
	addPlayers()
until playersInGame > 1

error :"bad argument #2 to 'find' (string expected, got no value)"

3 comments

0

So you want the script to run when a Player leaves? Does the table keep track if people are alive or anything, or are you just using it to know what people are in the game?

0

know what people are in the game

0

Like, I have a table of the players that are in the game, and when they leave, it removes their name from the table, and checks to see if their name is even in the table first

1 answer

✓ Accepted answer

Don't use string.find to search a table.


table.remove needs where the thing is, not the thing. You need to locate where a value is in the table.

A simple way to search for a thing is to go over each entry in the table using for i, v in pairs(t) and check if v is what you're looking for. If so, then you know that t[i] is v -- so that's where you can remove it from.

function search(tab, val)
	for i, v in pairs(tab) do
		if val == v then
			return i
		end
	end
end

This function returns nil if it doesn't find it. Thus you can do something like this:

local i = search(gamePlayers, leavingPlayer.Name)
if i then
	table.remove(gamePlayers, i)
end

A better solution?

First, I would recommend storing Player objects instead of their names. You are rarely more interested in what you call an object than the object itself (you want its .TeamColor and its .Character and .UserId, etc., don't you?)

Second, instead of constantly fiddling with some list, just write a function that tells you everyone at a particular moment who belongs. For example,

function getPlaying()
	local p = {}
	for _, player in pairs(game.Players:GetPlayers()) do
		if player:FindFirstChild("PlayingValues") then
			table.insert(p, player)
		end
	end
	return p
end

Regardless, playersInGame is not necessary. Just use the length of the list you're building: #gamePlayers

10 comments

0

i actually figured out a simpler way to do it "if table.concat(gamePlayers,(leavingPlayer.Name[#gamePlayers])) then"

0

don't use table.concat... or string.find.... please.... that's wrong....

0

okay... i tested mine and it didn't work with more than 1 table value, ill check yours out and give you some feedback

0

im confused on how your setting up your script

0

keeps saying table expected got nil, even though I put my table name where tab is

0

I don't understand what you mean. You should not need to change the search function at all.

0

well i just changed it back and it said the same thing take a look at the script i just updated

0

You're inserting names, but searching for players.

0

Oh, I am using names because when i use table.concat to print is has trouble printing players

0

Oops, I added a search() on line 25 and forgot to remove it, i noticed that i was already called that function. Appears to work now :) sorry for the hassle

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