Code works but gives an error

Hello!

I have an if statement to check whether or not a map exists in a folder. Code is below:

-- Variables
local cube = script.Parent
local currentMapFolder = workspace.CurrentMap

-- Functions
function TeleportPlayerToMap (hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local humanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
		
		if humanoidRootPart then
			local currentMap = currentMapFolder:FindFirstChildOfClass("Model")
			local currentMapSpawn = currentMap:FindFirstChild("Spawn")
			
			if currentMap and currentMapSpawn then
				humanoidRootPart.CFrame = currentMapSpawn.CFrame
			end
		end
	end
end

-- Connections
cube.Touched:Connect(TeleportPlayerToMap)

The code works, but I’d like it to not spit an error when the map doesn’t exist. The error stems at
local currentMapSpawn = currentMap:FindFirstChild("Spawn") and I am unsure how to get rid of it.

Any help would be appreciated!

And yes, I’ll add a debounce in a second, jeez. :tired_face:

Do “if not ITEM:findifirstchild then else” I think that should fix it

1 Like
-- Variables
local cube = script.Parent
local currentMapFolder = workspace.CurrentMap

-- Functions
function TeleportPlayerToMap (hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local humanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
		
		if humanoidRootPart then
			local currentMap = currentMapFolder:FindFirstChildOfClass("Model")
			local currentMapSpawn = nil
if not currentMap:FindFirstChild("Spawn") then else
currentMapSpawn = currentMap:WaitForChild("Spawn")
			
			if currentMap and currentMapSpawn then
				humanoidRootPart.CFrame = currentMapSpawn.CFrame
			end
		end
	end
end

-- Connections
cube.Touched:Connect(TeleportPlayerToMap)

I’m not sure if that’ll work but should work smthn like that

1 Like

It’d be helpful if you told us exactly what this error is.

1 Like

Showed us where the stem came from so I’d assume that is the error.

1 Like

I’m pretty sure the error is saying currentMap is nil, so it should look like this:


-- Variables
local cube = script.Parent
local currentMapFolder = workspace.CurrentMap

-- Functions
function TeleportPlayerToMap (hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local humanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
		
		if humanoidRootPart then
			local currentMap = currentMapFolder:FindFirstChildOfClass("Model")
            if not currentMap then return end
			local currentMapSpawn = currentMap:FindFirstChild("Spawn")
			
			if currentMap and currentMapSpawn then
				humanoidRootPart.CFrame = currentMapSpawn.CFrame
			end
		end
	end
end

-- Connections
cube.Touched:Connect(TeleportPlayerToMap)
2 Likes
  21:13:02.905  Workspace.Cube.TeleportPlayerToMap:12: attempt to index nil with 'FindFirstChild'  -  Server - TeleportPlayerToMap:12
  21:13:02.905  Stack Begin  -  Studio
  21:13:02.905  Script 'Workspace.Cube.TeleportPlayerToMap', Line 12 - function TeleportPlayerToMap  -  Studio - TeleportPlayerToMap:12
  21:13:02.905  Stack End

No I added that LOOO :sob: [ignore this]

me when mine doesn’t get marked at solution

1 Like

Ah, I see. Thanks! :pray:

ignore this