Remove and read Dictionary

local SelectedSpawnPoint = {}

SelectedSpawnPoint["Spawn1"] = "Player1, Player3"

SelectedSpawnPoint["Spawn2"] = "Player2, Player4"

As a example how could i remove Player4 and how could i check how many Players are inside Spawn1 and Spawn2?

Something like:

for index, value in pairs(SelectedSpawnPoint) do

if value == player then

value = nil

end

end

or something like:

print(#SelectedSpawnPoint["Spawn1"])

local SelectedSpawnPoint = {}

SelectedSpawnPoint["Spawn1"] = "Player1, Player3"
SelectedSpawnPoint["Spawn2"] = "Player2, Player4"

local function RemovePlayerFromSpawns(name: string) 
	-- note we use the *name* of the player, case sensitive
	for stringed, value in pairs(SelectedSpawnPoint) do
		local index = string.find(value, name)
		if index ~= nil then
			local newString = ""
			local endOfName = string.find(value, ", ", index)
			-- we must keep the space!!
			if index ~= 1 then -- we're deleting not the first name
				if endOfName ~= nil then -- we're deleting a name in the middle
					local beforeString = string.sub(value, 1, index - 1)
					local endString = string.sub(value, endOfName + 2, -1)
					newString = beforeString .. endString
					if string.sub(newString, -2) == ", " then
						newString = string.sub(newString, 1, -2)
					end
				else -- we must be trying to delete the last name
					newString = string.sub(value, 1, index - 2)
				end
			else -- we're deleting the first name
				if endIndex ~= nil then
					newString = string.sub(value, endIndex + 2)
				else -- we're deleting the first (and only) name
					newString = ""
				end
			end
			SelectedSpawnPoint[stringed] = newString
			break -- we found the user and removed it, no point in still looping
		end
	end
end

local function CountPlayers(spawnName: string?)
	-- the ": string?" part in the function means the argument "spawnName" can either be a string or nil (aka an optional argument)
	if spawnName ~= nil and SelectedSpawnPoint[spawnName] ~= nil then
		return #string.split(SelectedSpawnPoint[spawnName], ", ") -- we must include the space!
	elseif spawnName == nil then
		local count = 0
		for _, value in pairs(SelectedSpawnPoint) do
			count = count + #string.split(value, ", ")
		end
		return count
	else
		warn "Spawn point is not valid!"
	end
end

or just use arrays like @MakerDoe pointed out below. I was just giving you an option to use strings since you had them originally.

1 Like

This is unnecessary; just use an array instead of strings:

SelectedSpawnPoint["Spawn1"] = { "Player1", "Player3" }

print(#SelectedSpawnPoint["Spawn1"])

table.remove(SelectedSpawnPoint["Spawn1"], table.find(SelectedSpawnPoint["Spawn1"], "Player1"))
2 Likes

It removes all Players in the Table, how could i remove just one?

It prints 1 with print(#SelectedSpawnPoint["Spawn1"]) if there is 2 Players inside, is 0 == 1 for Lua?

What do you mean? If you pass the correct name (for example, call RemovePlayerFromSpawns("Player4")) then it’ll remove that and only that. If you call RemovePlayerFromSpawns("Player") then it’ll break.

endIndex has a yellow line under, is that right?

Oops! Change all instances where it says “endIndex” to “endOfName”. I totally forgot I called it “endOfName” haha

Thanks, it worked, just because im curious does this work with a shorter script like with table.remove() or table.find()?

Not if you’re planning to use strings.

Okay and would that also work if i would insert the player into the Table?

To insert a player you could use this function:

local function InsertPlayerIntoSpawn(spawnName: string, playerName: string)
	SelectedSpawnPoint[spawnName] = SelectedSpawnPoint[spawnName] .. ", " .. playerName
end
1 Like

Okay thanks


1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.