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)
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: