While loop problem

Hay! im trying to create a teleport loop.
So whats the problem? Well, it doesent print() after i say “go” after “stop”. But it does stop.

local playG = "go"
local playS = "stop" 

game.Players.LocalPlayer.Chatted:Connect(function(msg)
if msg:sub(1, playG:len()):lower() == playG then
print("Teleporting: ✅")
teleport = true
end
end) 

game.Players.LocalPlayer.Chatted:Connect(function(msg) 
if msg:sub(1, playS:len()):lower() == playS then
print("Teleporting: ❌")
teleport = false
end
end) 

while teleport == true do
print("Teleporting...")
wait(2)
end

test1

The issue is that the while loop will only loop once. Once teleport is set to false the while loop will never run again even if you set teleport back to true.

1 Like

Try this maybe.

local playG = "go"
local playS = "stop" 
local teleport = true -- Disregard this if you have this variable declared somewhere else

game.Players.LocalPlayer.Chatted:Connect(function(msg)
if teleport == true then return end
if msg:sub(1, playG:len()):lower() == playG then
print("Teleporting: ✅")
teleport = true
while teleport == true do
print("Teleporting...")
wait(2)
end
end
end) 

game.Players.LocalPlayer.Chatted:Connect(function(msg) 
if msg:sub(1, playS:len()):lower() == playS then
print("Teleporting: ❌")
teleport = false
end
end) 

2 Likes

it starts looping when i say “go”, its perfect! Thank you so much!

1 Like

No problem, happy I could help!

1 Like