Functions and If loops

Hi,

I am trying to make a teleport block that only works when game.Workspace.BaseMap exists and do nothing when it doesn’t (between rounds).

Here is my current code in a script inside the teleport block:

function teleportPlayer(hit)
	local spawnblock = {game.Workspace.BaseMap.Spawn1,game.Workspace.BaseMap.Spawn2,game.Workspace.BaseMap.Spawn3,game.Workspace.BaseMap.Spawn4,game.Workspace.BaseMap.Spawn5,game.Workspace.BaseMap.Spawn6,game.Workspace.BaseMap.Spawn7}
	local target = CFrame.new(spawnblock[math.random(1,#spawnblock)].Position)
	if hit.Parent:findFirstChild("Humanoid") ~= nil then 
	hit.Parent.Torso.CFrame = target
	game.Workspace.Sounds.Teleport:Play()
	end
end


script.Parent.Touched:Connect(teleportPlayer)

If have tried:

function teleportPlayer(hit)
	local spawnblock = {game.Workspace.BaseMap.Spawn1,game.Workspace.BaseMap.Spawn2,game.Workspace.BaseMap.Spawn3,game.Workspace.BaseMap.Spawn4,game.Workspace.BaseMap.Spawn5,game.Workspace.BaseMap.Spawn6,game.Workspace.BaseMap.Spawn7}
	local target = CFrame.new(spawnblock[math.random(1,#spawnblock)].Position)
	if hit.Parent:findFirstChild("Humanoid") ~= nil and game.Workspace:findFirstChild("BaseMap") ~= nil then 
	hit.Parent.Torso.CFrame = target
	game.Workspace.Sounds.Teleport:Play()
	end
end


script.Parent.Touched:Connect(teleportPlayer)

and

function teleportPlayer(hit)
	local spawnblock = {game.Workspace.BaseMap.Spawn1,game.Workspace.BaseMap.Spawn2,game.Workspace.BaseMap.Spawn3,game.Workspace.BaseMap.Spawn4,game.Workspace.BaseMap.Spawn5,game.Workspace.BaseMap.Spawn6,game.Workspace.BaseMap.Spawn7}
	local target = CFrame.new(spawnblock[math.random(1,#spawnblock)].Position)
	if hit.Parent:findFirstChild("Humanoid") ~= nil then 
	hit.Parent.Torso.CFrame = target
	game.Workspace.Sounds.Teleport:Play()
	end
end

if game.Workspace.BaseMap ~= nil then
script.Parent.Touched:Connect(teleportPlayer)
end

The first two work fine, but throw errors constantly between rounds saying the basemap doesn’t exist.
The third one doesn’t work at all.
I also tried putting the statement in the 2nd line. I tried it with WaitForChild and I tried doing if the basemap == nil then return else teleport, but nothing is preventing the error.

Can someone help me understand what I am doing wrong and why?

Thanks.

1 Like

Add a variable.
local baseMap = workspace:FindFirstChild(“BaseMap”)

Then inside the function add an if statement
if baseMap then
–put the rest of your code in here
end

1 Like

Thanks!

I found that also helped 2 other bugs in my code.

I believe you can also use WaitForChild.

I don’t think WaitForChild would work for this situation. I could be wrong.

WaitForChild won’t run at all if your object doesn’t exist, if it does then it’ll run.

This is not correct. WaitForChild will run in all cases because you’re calling a function. WaitForChild will yield the thread until an object exists that has the same name as the first string you pass to it. Optionally, you can also include a number - after n amount of seconds, if that object isn’t found, WaitForChild will stop yielding and return nil.

-- Code will not continue until an instance named BasePlate is in the Workspace
local basePlate = workspace:WaitForChild("BasePlate")

-- Code will wait 5 seconds or until BasePlate is found, otherwise return nil
local basePlate = workspace:WaitForChild("BasePlate", 5)
3 Likes