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.
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.
local function InsertPlayerIntoSpawn(spawnName: string, playerName: string)
SelectedSpawnPoint[spawnName] = SelectedSpawnPoint[spawnName] .. ", " .. playerName
end