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

How do i make a toggle button in my GUI loop, until it gets de-activated by player? (im beginner)

Hey, im a beginner and Im trying to make button that has a toggle feature in my gui and when it toggles it goes in loop which i already made but it only goes 1 time. But i dont want it to go one time, i want it to go on loop. Also i dont want the loop to end unless the player presses it again and makes it in-active or de-activate (Sorry for bad English and maybe lack of explaining). I know you can just repeat the same code over and over like this (Down Below) . But its too messy and takes up a TON of space. (Original Code Down Below)

Messy Code:

local toggle = false

TButton.MouseButton1Click:Connect(function()
if not toggle then
		toggle = true
		TButton.Text = "Target People/Person (ACTIVE)"	-- Changes Text
wait()
---------------------------
	local plrText = AnchorP1 -- TextBox
    local player = game:GetService("Players"):FindFirstChild(plrText.Text)

    if player then
        local char = player.Character or player.CharacterAdded:Wait()
        if char then
            for _,part in pairs(char:GetChildren()) do
                if part.Name == "Head" then
part.Anchored = true -- [trying to loop]
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
wait(0.25)
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
wait(0.25)
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
wait(0.25)
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
wait(0.25)
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
wait(0.25)
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
wait(0.25)
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
wait(0.25)
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
wait(0.25)
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
wait(0.25)
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
                end
            end
        end
    end
---------------------------
	else 
		toggle = false
		TButton.Text = "Target People/Person (IN-ACTIVE)"	-- Changes Text
wait()
---------------------------
	local plrText = AnchorP1 -- TextBox
    local player = game:GetService("Players"):FindFirstChild(plrText.Text)

    if player then
        local char = player.Character or player.CharacterAdded:Wait()
        if char then
            for _,part in pairs(char:GetChildren()) do
                if part.Name == "Head" then
part.Anchored = false -- [DONT need to loop]
                end
            end
        end
    end
---------------------------
	end
end)

Original Code:

local toggle = false

TButton.MouseButton1Click:Connect(function()
if not toggle then
		toggle = true
		TButton.Text = "Target People/Person (ACTIVE)"	-- Changes Text
wait()
---------------------------
	local plrText = AnchorP1 -- TextBox
    local player = game:GetService("Players"):FindFirstChild(plrText.Text)

    if player then
        local char = player.Character or player.CharacterAdded:Wait()
        if char then
            for _,part in pairs(char:GetChildren()) do
                if part.Name == "Head" then
part.Anchored = true -- [trying to loop]
part.Position = game.Players.LocalPlayer.Character.LowerTorso.Position + Vector3.new(0, 0, 0) -- [trying to loop]
                end
            end
        end
    end
---------------------------
	else 
		toggle = false
		TButton.Text = "Target People/Person (IN-ACTIVE)"	-- Changes Text
wait()
---------------------------
	local plrText = AnchorP1 -- TextBox
    local player = game:GetService("Players"):FindFirstChild(plrText.Text)

    if player then
        local char = player.Character or player.CharacterAdded:Wait()
        if char then
            for _,part in pairs(char:GetChildren()) do
                if part.Name == "Head" then
part.Anchored = false -- [DONT need to loop]
                end
            end
        end
    end
---------------------------
	end
end)

Thanks!

1 answer

✓ Accepted answer

You are looking for loops, they are used in every language and allow code to be executed over and over, forever or until a condition is met.

The most simple of which is the while true do loop:

Ex.

while true do
	print("Doing")
end

This simply runs this code forever but you will want the code to stop once a condition is met. You could use many different types of loops for this.

Take until loops:

until condition == true do
	print("Doing")
end

This runs code until the variable condition is equal to true.

A nice thing when it comes to if true queries is that you can remove == true entirely as when it is not present it will see if the value given is true by default. This can be useful to cut down on space use and clean up your code.

Another very important loop to know (Though not applicable to this situation) is for do loop:

for i=1,var do
	print("Doing")
end

You seemed to use this in your code but not to its full potential.

This will run code a fixed amount of times before moving on. It will run for however many integers are between 1 and var. Say if var was 10 it would run 10 times. If 1 was 5 and var was 10 then it would run 5 times. i can be read by code as well making it useful for sorting through tables or making lists.

Another thing to take into consideration when using loops is that you never want a loop to run without a pause. Something like this puts immense strain on the game, many of these piling up can cause serious performance effects, even crashes.

This can be prevented with a simple wait() statement or with any code that will cause a slight pause.

while true do
	print("Doing")
	wait(0.01)
end

More information on loops can be found here

1 comment

0

thank you so much this helped alot!

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