Randomized teleport script

Hello! I am trying to make a teleport script for my game but it seems to not be working. I also am not getting any errors which is weird. Help needed.

local num = math.random(1, 5)

local TPad1 = game.Workspace.Pad2
local TPad2 = game.Workspace.Pad3
local TPad3 = game.Workspace.Pad4
local TPad4 = game.Workspace.Pad5
local TPad5 = game.Workspace.Pad6

if num = 1 then
	script.Parent.Touched:Connect(function(touchPart)
		if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
			local Character = touchPart.Parent
			local teleportLocation = CFrame.new(TPad1.CFrame.X, TPad1.CFrame.Y + 5, TPad1.CFrame.Z)
			Character:SetPrimaryPartCFrame(teleportLocation)

			local teleportingValue = Character.currentlyTeleporting
			teleportingValue.Value = true
			wait(3)
			teleportingValue.Value = false
		end
	end)
	if num = 2 then
		script.Parent.Touched:Connect(function(touchPart)
			if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
				local Character = touchPart.Parent
				local teleportLocation = CFrame.new(TPad2.CFrame.X, TPad2.CFrame.Y + 5, TPad2.CFrame.Z)
				Character:SetPrimaryPartCFrame(teleportLocation)

				local teleportingValue = Character.currentlyTeleporting
				teleportingValue.Value = true
				wait(3)
				teleportingValue.Value = false
			end
			(end
		if num = 3 then
			script.Parent.Touched:Connect(function(touchPart)
				if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
					local Character = touchPart.Parent
					local teleportLocation = CFrame.new(TPad3.CFrame.X, TPad3.CFrame.Y + 5, TPad3.CFrame.Z)
					Character:SetPrimaryPartCFrame(teleportLocation)

					local teleportingValue = Character.currentlyTeleporting
					teleportingValue.Value = true
					wait(3)
					teleportingValue.Value = false
				end
				(end
			if num = 4 then
				script.Parent.Touched:Connect(function(touchPart)
					if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
						local Character = touchPart.Parent
						local teleportLocation = CFrame.new(TPad4.CFrame.X, TPad4.CFrame.Y + 5, TPad4.CFrame.Z)
						Character:SetPrimaryPartCFrame(teleportLocation)

						local teleportingValue = Character.currentlyTeleporting
						teleportingValue.Value = true
						wait(3)
						teleportingValue.Value = false
					end
					(end
				if num = 5 then
					script.Parent.Touched:Connect(function(touchPart)
						if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
							local Character = touchPart.Parent
							local teleportLocation = CFrame.new(TPad5.CFrame.X, TPad5.CFrame.Y + 5, TPad5.CFrame.Z)
							Character:SetPrimaryPartCFrame(teleportLocation)

							local teleportingValue = Character.currentlyTeleporting
							teleportingValue.Value = true
							wait(3)
							teleportingValue.Value = false
						end
					(end

1- Use elseif statements instead of creating new if statements.
example:

local randomnum = math.random(1,3)
if randomnum == 1 then
print(randomnum)
elseif randomnum == 3 then
print(randomnum)
end

2- You do == instead of = on if statements.
3- If you want to end a function you do end), not (end

Still not working

local num = math.random(1, 5)

local TPad1 = game.Workspace.Pad2
local TPad2 = game.Workspace.Pad3
local TPad3 = game.Workspace.Pad4
local TPad4 = game.Workspace.Pad5
local TPad5 = game.Workspace.Pad6

if num == 1 then
	script.Parent.Touched:Connect(function(touchPart)
		if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
			local Character = touchPart.Parent
			local teleportLocation = CFrame.new(TPad1.CFrame.X, TPad1.CFrame.Y + 5, TPad1.CFrame.Z)
			Character:SetPrimaryPartCFrame(teleportLocation)

			local teleportingValue = Character.currentlyTeleporting
			teleportingValue.Value = true
			wait(3)
			teleportingValue.Value = false
		end
	end)
	if num = 2 then
		script.Parent.Touched:Connect(function(touchPart)
			if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
				local Character = touchPart.Parent
				local teleportLocation = CFrame.new(TPad2.CFrame.X, TPad2.CFrame.Y + 5, TPad2.CFrame.Z)
				Character:SetPrimaryPartCFrame(teleportLocation)

				local teleportingValue = Character.currentlyTeleporting
				teleportingValue.Value = true
				wait(3)
				teleportingValue.Value = false
			end
		end)
		elseif num == 3 then
			script.Parent.Touched:Connect(function(touchPart)
				if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
					local Character = touchPart.Parent
					local teleportLocation = CFrame.new(TPad3.CFrame.X, TPad3.CFrame.Y + 5, TPad3.CFrame.Z)
					Character:SetPrimaryPartCFrame(teleportLocation)

					local teleportingValue = Character.currentlyTeleporting
					teleportingValue.Value = true
					wait(3)
					teleportingValue.Value = false
				end
			end)
			elseif num == 4 then
				script.Parent.Touched:Connect(function(touchPart)
					if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
						local Character = touchPart.Parent
						local teleportLocation = CFrame.new(TPad4.CFrame.X, TPad4.CFrame.Y + 5, TPad4.CFrame.Z)
						Character:SetPrimaryPartCFrame(teleportLocation)

						local teleportingValue = Character.currentlyTeleporting
						teleportingValue.Value = true
						wait(3)
						teleportingValue.Value = false
					end
				end)
				elseif num == 5 then
					script.Parent.Touched:Connect(function(touchPart)
						if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
							local Character = touchPart.Parent
							local teleportLocation = CFrame.new(TPad5.CFrame.X, TPad5.CFrame.Y + 5, TPad5.CFrame.Z)
							Character:SetPrimaryPartCFrame(teleportLocation)

							local teleportingValue = Character.currentlyTeleporting
							teleportingValue.Value = true
							wait(3)
							teleportingValue.Value = false
						end
					end)

Atom meant this:

Touched:Connect(function()
    if something1 then
        something1
    elseif something2 then
         something2
    end
end)

Not:

if stuff1 then
	Touched:Connect(function()
		if things then
			doStuff
		 end
end)

elseif stuff2 then
	Touched:Connect(function()
		if things then
			doStuff
		 end
end)

This is formatted horribly because I’m on phone (I realised it became worse out of edit mode)

I would recommend adding tons of prints to find the exact place where the code breaks. You have a lot of if statements so one of them probably isn’t firing when you want them too.

Lemme type something out for you rq…

Here you do, I’m not sure if it will work, but let’s hope. Your code is too big, and can be simplified easily. Let me know if you have any more issues!

local num

-- Line of code that actually teleports the player.
local function Teleport(touchPart)
local teleportpart = "Pad"..num
print(teleportpart)
local teleportpath = game.Workspace[teleportpart]
local teleportLocation = CFrame.new(teleportpath.CFrame.X, teleportpath.CFrame.Y + 5, teleportpath.CFrame.Z)

local char = touchPart.Parent
local teleportingValue = char.currentlyTeleporting
char:SetPrimaryPartCFrame(teleportLocation)
teleportingValue.Value = true
wait(3)
teleportingValue.Value = false
end

-- Checks and randomizes the teleport.
local function randomizenumber(touchPart)
local num = math.random(1, 5) -- Define what the maximum and minimum teleports are!

if touchPart and touchPart.Parent and touchPart.Parent.Humanoid and touchPart.Parent.currentlyTeleporting.Value == false then
	Teleport(touchPart) -- Run the function.
end
end

script.Parent.Touched:Connect(randomizenumber)

“Workspace.Pad1.Teleport Script:5: attempt to concatenate string with nil - Server - Teleport Script:5”

If you actually try to figure out what the script does, you will know to replace the first line with

local num = math.random(1,5)

Didn’t really read that since I was more interested in the randomizing part of it and stuff, thanks though.

1 Like