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

This works but only for a while, Why?

I am making a teleporter that would teleport to a certain brick then the next person that goes to that teleporter will teleport to another brick then etc. But the problem is the loop. After every 5th teleport the teleporter only teleports to one brick and not teleport to another. Could fix the problem or explain to me why?

A = 0
while true do
	wait(1)
if A == 0 then
	script.Parent.Touched:connect(function(p)
		if p.Parent:findFirstChild("Torso") and A == 0 then
			A = A + 1
			p.Parent.Torso.CFrame = script.Parent.Parent["1"].CFrame + Vector3.new(0,5,0)
while true do
wait(1)
if A == 1 then
	script.Parent.Touched:connect(function(p)
		if p.Parent:findFirstChild("Torso") and A == 1 then
			A = A + 1
			p.Parent.Torso.CFrame = script.Parent.Parent["2"].CFrame + Vector3.new(0,5,0)
			while true do
				wait(1)
if A == 2 then
	script.Parent.Touched:connect(function(p)
		if p.Parent:findFirstChild("Torso") and A == 2 then
			A = A + 1
			p.Parent.Torso.CFrame = script.Parent.Parent["3"].CFrame + Vector3.new(0,5,0)
			while true do
				wait(1)
if A == 3 then
	script.Parent.Touched:connect(function(p)
		if p.Parent:findFirstChild("Torso") and A == 3 then
			A = A + 1
			p.Parent.Torso.CFrame = script.Parent.Parent["4"].CFrame + Vector3.new(0,5,0)
			while true do
				wait(1)
if A == 4 then
	script.Parent.Touched:connect(function(p)
		if p.Parent:findFirstChild("Torso") and A == 4 then
			A = A + 1
			p.Parent.Torso.CFrame = script.Parent.Parent["5"].CFrame + Vector3.new(0,5,0)
			while true do
				wait(1)
A = 0
			end
		end
	end)
	end
end
			end
		end)
	end
	end
end
			end)
		end
	end
	end
end)
			end
		end
	end
	end)
end
end

3 answers

A = 1
local Obj = script.Parent
Obj.Touched:connect(function(Hit)
	if Hit.Parent:findFirstChild("Torso") then
		pcall(function()
			Hit.Parent.Torso.CFrame = Obj.Parent[tostring(A)].CFrame * CFrame.new(0, 5, 0)
			A = (A == 5 and 1) or (A + 1)
		end)
	end
end)

You were massively over complicating it. This should do fine. As you can see, I have changed the value of A to 1. This is because, you can just index the object through A. You also don't need any while loops, just one Touched event will do. I then carried on to do the CFraming, and then for setting A, I checked if it was equal to 5 and if it is it sets it to 1, else it adds 1 to the current value. The pcall was used in case there is no Obj.Parent[tostring(A)], and that basically sums up my improvements.

It is now much more efficient.

2 comments

0

I'm not sure on the specifics with Lua, but shouldn't A be a string if he's indexing objects called 1, 2, and 3 etc... - they aren't necessarily stored under the numerical keys.

0

Yeah, I updated that 12 seconds before you posted your comment.

You don't need while loops, you don't need to connect the Touched event to a function multiple times, you don't need any duplicate code for each value of A, you don't need wait()s between each variable check, and if I'm not mistaken your ends are all in the wrong places!

From the start:

A = 0

Every time a Player gets teleported, A goes up by 1, but not past 5. A Player gets teleported every time they touch the scripts Parent.

So lets connect to the Touched event:

script.Parent.Touched:connect(function(p)
	--Handle teleportation
end

Now, allow me to introduce you to a new function, tostring(). tostring() converts input that you give to it to strings (formal and official lists of ASCII characters representing text), using this function, we can use the variable A to index script.Parent.Parent. For example, instead of doing:

if A == 3 then p.Parent.Torso.CFrame = script.Parent.Parent["3"].CFrame end

We could just do:

p.Parent.Torso.CFrame = script.Parent.Parent[tostring(A)].CFrame

The next thing I'll introduce you to is the % operator, see if you can figure out what happens in these examples:

8 % 2 = 0
7 % 2 = 1
5 % 3 = 2
11 % 3 = 2
12 % 12 = 0
12 % 13 = 12

What does % do? A % B returns the remainder when A is divided by B.

Putting this all together, your original script can be re-written as:

A = 0
script.Parent.Touched:connect(function(p)
	if p.Parent:FindFirstChild("Torso") then
		A = (A+1) % 5 --Replace 5 with No. of teleports
		p.Parent.Torso.CFrame = script.Parent.Parent[tostring(A+1)].CFrame + Vector3.new(0,5,0)
	end
end

There me be some slight syntactical ("spelling") mistakes, but you should be able to identify and fix any of them using the Output (ROBLOX Studio > View > Output).

I hope this helps!

Try adding a repeat function to see if that helps. I'm suggesting this because when you state A == 0 on the last line, it breaks. A repeat function may help there c:

Or if you don't want to add a repeat, just remove the last A == 0.

1 comment

0

Why did you thumbs down me!!! D:

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