Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can anyone tell me what wrong with the Teleportation script?

I want the to script the select to Random players and teleport them to 2 different locations.

function start(plyr)
if script.Parent.Text= "Start Match" then, 
if plyr.Character ~= nil then
plyr.Character:MoveTo(Vector3.new(45.6, 213.604, 56.2)) 
else
ranPlyr()
end
end


function ranPlyr()
if #players > 2 then
local a = math.random(1, game.Players.NumPlayers)
plyrs = game.Players:GetChildren()
for i = 1, #plyrs do
rand = plyrs[a]
end
start(rand)
else
message.Parent = game.Workspace 
wait(4) 
message.Parent = nil 
end
end

while true do
wait(10)
ranPlyr()
end

2 comments

0

I don't see anywhere where you defined "players"

0

You should accept answers if they are on the whole helpful.

1 answer

Syntax

Your script has syntax error.

Use your output. It would tell you exactly what is wrong:

input:2: 'then' expected near '='

We have a problem on the second line near the =.


The operator which compares values is ==.

Use == for comparison instead of = which is used for assignment:

if script.Parent.Text == "Start Match" then

We have another syntax error.

input:2: unexpected symbol near ','

That comma is superfluous and wrong. Remove it.


Yet another syntax error. Maybe you should review your basic Lua. Definitely learn to use your output

input:30: 'end' expected (to close 'function' at line 1) near

Tab and space your code properly. If we do, we can see problems like this easily:

function start(plyr)
	if script.Parent.Text== "Start Match" then
		if plyr.Character ~= nil then
			plyr.Character:MoveTo(Vector3.new(45.6, 213.604, 56.2)) 
		else
			ranPlyr()
		end
	end


	function ranPlyr()
		if #players > 2 then
			local a = math.random(1, game.Players.NumPlayers)
			plyrs = game.Players:GetChildren()
			for i = 1, #plyrs do
				rand = plyrs[a]
			end
			start(rand)
		else
			message.Parent = game.Workspace 
			wait(4) 
			message.Parent = nil 
		end
	end

	while true do
		wait(10)
		ranPlyr()
	end

Clearly (with tabs) we can see something is wrong. We didn't return to no indentation before defining ranPlyr so we're probably missing something above.

Add an end to close start .



Logic

You use the variable playersbut never define it. You define a similarly named plyrs which isn't used a little below. We should move that up:

	players = game.Players:GetChildren()
	if #players > 2 then
		local a = math.random(1, game.Players.NumPlayers)

Note that it is more proper to use :GetPlayers() to get a list of players. Also note that game.Players.NumPlayers is a really long way to use what you've already used: #players.

We should also use players instead of plyrs then from now. (Really -- use readable, normal variable names)

Your for loop on line 15 is pointless.

  • No side effects (doesn't change anything)
  • Is not dependant on the loop (ignores i and doesn't change anything it uses)



You use message but never define it.

Recursion

Warning: If there are more than two players, but none of them have Characters, then your script will have a stack-overflow-error. In that case, start(rand) will call ranPlyr() repeatedly.

You should have some sort of bailout to be sure. For example, you should instead make a list of alive-players and considers its length rather than the number of players.

This discussion is preserved as it was. New answers, comments, and votes are closed.