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

Why doesn't the script run again after it ran the first time?

I'm making a teleport script (function totolobby()) in a localscript, but it only teleports once, but not twice. Same for removing the map (function unloadmap()) .

Script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local lobbylocation = CFrame.new(math.random(4066,4114),7.5,math.random(84,182))

local tpBackToLobbyEvent = ReplicatedStorage:WaitForChild("TpBackToLobby")

function tptolobby()
	for i, player in pairs(game.Players:GetChildren()) do
		if player.Character:FindFirstChild("HumanoidRootPart") then
			player.Character.HumanoidRootPart.CFrame = lobbylocation
		end
	end
end

function unloadMap()
	if game.Workspace:FindFirstChild("Map") then
		game.Workspace.Map:Destroy()
	end
end


function onTpBackToLobby()
	print("BackToLobby")
	tptolobby()
	unloadMap()
end
 
tpBackToLobbyEvent.OnClientEvent:Connect(onTpBackToLobby)

2 comments

0

You never told it to run twice. You will need to loop it, or find a reason to make it run again

0

Actually, this issue was solved yesterday by putting the activator script to another location, but thanks for the explanation!

1 answer

✓ Accepted answer

Main Idea Scripts don't read your mind. You want it do run again and again judging by your question, lets get started.

For Loops

The main idea of for loops, it your telling the computer to do something For a certain amount of times. We will be using a comma in between two numbers for i=1, 10 do. So if you wanted to do this:

for i=1, 10 do   	
   print("Hello World")
end

the result would be this in the output:

"Hello World"
"Hello World"
"Hello World"
"Hello World"
"Hello World"
"Hello World"
"Hello World"
"Hello World"
"Hello World"
"Hello World"

it is counting by one's, fist number

to 10 second number

if the script was this:

for i=2, 10 do
	print("Hello World")
end

the output would be:

"Hello World"
"Hello World"
"Hello World"
"Hello World"
"Hello World"

because it is counting by two's, it would only print 5 times.

In your script, if you wanted to repeat your script twice, you would do this

for i=1, 2 do 
	--script--
end

Hope this helps :)

While Loops

These loops are very similar to for loops, and I think they are much easier to understand.

so we will start with the most simple while loop. The idea of while loops is while the statement listed is true, do so many times. so if you did:

while true do
--script--
end

the statement between while and do is true, so it repeats forever.

if you did:

i = 1

while i < 10 do
i = i+1
--script--
end

the script would run 10 times.

If this was not adequate, the wiki think is below:

http://wiki.roblox.com/index.php?title=Loops

2 comments

0

Everything you said was correct except for this. "for i = 2,10" does the following: i starts on 2 then increases by ONE until it hits 10, therefore "hello world" would be printed NINE times. The correct way of doing what you said would be "for i = 1,10,2" where 2 describes how much i increases by. I hope I helped you :)

0

oops, i messed up with that, thx

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