My setup is when a Button is clicked, it fires a RemoteEvent to the server. The server calls the function after a for loop to check if the map exists.
-- Server
-- ...
ReplicatedStorage.Events.PlayEvent.OnServerEvent:Connect(function(player)
for _, mapName in ipairs(MapHandler:GetAllMaps()) do
local clone = MapHandler:FindCloneByName(mapName)
PlayerHandler:TeleportPlayerToMap(player, clone:FindFirstChild("Spawns"):GetChildren())
end
end)
-- ...
The function MapHandler:FindCloneByName() attempts to find a clone in workspace, and if it exists, then it’ll return the clone.
-- ModuleScript
-- ...
function HandlerObj:FindCloneByName(workspaceMapName)
local Clone = workspace:FindFirstChild(workspaceMapName)
if Clone then return Clone end
return "Cannot find clone"
end
-- ...
MapHandler:GetAllMaps() gets all map names from an array.
-- ModuleScript
local possibleMaps = {"test_area1", "test_area2"}
-- ...
function HandlerObj:GetAllMaps()
return possibleMaps
end
-- ...
I’m currently having an issue with PlayerHandler:TeleportPlayerToMap(), where it randomly chooses a spawn part to teleport the player to. The parameter as seen in the server script will error.
-- ...
function PlayerHandler:TeleportPlayerToMap(player, spawns)
local randSpawn = Ran:NextInteger(1, #spawns)
local character = player.Character
if spawns then
character.HumanoidRootPart.CFrame = spawns[randSpawn].CFrame + Vector3.new(0, 5, 0)
end
end
-- ...
The error message, for reference
I’m suspecting that the parameter spawns may be the issue here, since player won’t be a nil value.