Forum > Coding >

Looping code. Help!

Posts: 196

Threads: 58

Joined: Apr, 2020

Reputation: 3

Posted

So let's say I have some code for Ex.

while true do wait(0.1)
   if game:GetService("Players").LocalPlayer.TeamColor ~= game:GetService("Teams").Spectating.TeamColor then 
      player.Character.Head:Destroy()
   else
while true do wait(0.1)
   if game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame ~= CFrame.new(1149.9324951171875, 61.9999885559082, -814.690673828125) then
      game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(1149.9324951171875, 61.9999885559082, -814.690673828125)
while true do wait(0.1)
   if player.Character.Humanoid.Health == 0 then
while true do wait(8)
      game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(1149.9324951171875, 61.9999885559082, -814.690673828125)
                        end
                    end
                end
            end
        end
   end
end

 

Heres what the code is suppose to do.

 

Infinitely loop to see if the players team color has changed.

If the players team color is playing then the player respawns/resets/dies.

If the players team color is not playing and is spectating then it infinitely loops while still looping to check if the player has changed teams. It then infinitely loops to check if the player is not at the .CFrame. If the player is not at the .CFrame then the player gets teleported to the .CFrame where it's suppose to be infinitely.

It then infinitely loops to check if the players health is at 0 (due to reseting when finding out it's on the playing team) If it finds that the players health is at 0 (dead) then its suppose to wait for the player to respawn and when the player has spawned in it infinitely loops back to the designated .CFrame.

 

Yet this is what happens.

Repeats code every 0.1second

Checks if the player is on playing team.

If it's on the playing team it resets the character.

Teleporting doesn't continue

If it's not on the playing team it continues as normal

It repeats every 0.1second

Checks if the player is at the designated .CFrame

If not it teleports the player to the designated .CFrame

Yet it only teleports once instead of infinitely

If you manually reset when farming which equals your health to be 0

It waits 8 seconds then teleports your player once.

 

You can die 2 times before it all breaks.

 

Does anyone know how to loop all of these without using too much or while true do wait(0.1 and to check if the player has respawned and if it has it teleports to the .CFrame automatically.

If so please respond and give me a detailed tutorial on how you managed to do it!

It would help me later on aswell.

 

(No errors that I know of)

  • 0

Kura

kuraise

Posts: 135

Threads: 3

Joined: Jun, 2021

Reputation: 5

Replied

Hello, if you're implying (I didn't want to read it all) that when the character resets it doesn't teleports then do:

game.Players.LocalPlayer.CharacterAdded:Connect(function() -- Every time your character resets and it exists.
  -- Source code.
end)

-- Did it on mobile. :sungl:
  • 1

Added

@_realnickk Oh alright, thanks for letting me know.

  • 0

 

https://cdn.discordapp.com/attachments/877610317037858846/975469974405673041/Signature.png

Posts: 196

Threads: 58

Joined: Apr, 2020

Reputation: 3

Replied

@Kura

Thankyou for the help!!

You are a life saver!

  • 0

Kura

kuraise

Posts: 135

Threads: 3

Joined: Jun, 2021

Reputation: 5

Replied

@DadComeBack It's alright, there's always the roblox docs that can provide you some of the help but if you need any just DM me, kura#0069 (I believe this is the latest discord tag I currently have because I change it too often.)

  • 0

 

https://cdn.discordapp.com/attachments/877610317037858846/975469974405673041/Signature.png

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )