Repeat until loop isn't stopping?

Hello, basically, I want to be able to create a part which follows the user continuously through a repeat loop, and then I want to destroy it.

I was doing fine until I ran into an issue, basically, my repeat loop is saying that ‘Activated’ is true, even though in reality, it’s false. I tested with printing. Why is this the case, and how can I make my repeat until stop loop when I fire the second remote event into the server?

I’ve looked for solutions, but nothing on the Devforum seems to be working for me

LOCAL SCRIPT

local UserInputService = game:GetService("UserInputService")
local HpEn = game.ReplicatedStorage.HpEn
local Player = game.Players.LocalPlayer
local Activated = false 

UserInputService.InputBegan:connect(function(input)

	if input.KeyCode == Enum.KeyCode.F and Activated == false then

	Activated = true
	HpEn:FireServer(Activated)
	print ("En has been activated . .")
	

elseif input.KeyCode == Enum.KeyCode.F and Activated == true then

		
		Activated = false
		HpEn:FireServer(Activated)
		print ("En has been deactivated . .")
	end
end)



HpEn.OnClientEvent:Connect(function()

print ("RETURNED TO THE CLIENT!")

end)

SERVER SCRIPT

local HpEn = game.ReplicatedStorage.HpEn

HpEn.OnServerEvent:Connect(function(Player, Activated)

local Char = Player.Character
print ("Server has been reached!")
print (Activated)
if Activated == true then

	local Newpart = Instance.new("Part")
	Newpart.Name = "What's Goodie?"
	Newpart.Shape = "Ball"
	Newpart.Transparency = 0.9
	Newpart.CanCollide = false
	Newpart.Anchored = true
	Newpart.Parent = game.Workspace
	Newpart.Position = Vector3.new(0,10,0)
	Newpart.Size = Vector3.new(20,20,20)
	Newpart.Material = "Neon"

		repeat 
			wait (0.001)
			Newpart.CFrame = Char.LowerTorso.CFrame
			print (Activated)
		until Activated == false

else if Activated == false then 
print ("PART HAS BEEN DESTROYED BRUH!")

		end
	end
end)


its not stopping because every time the remote event receives a call, it creates a new function and not continue with the same function, if that makes any sense. But this can be solved if you put a bool outside the remote function and use that for repeat loop to check.

start the bool as false and when the remote event is called make the bool the same as the ‘Activated’ variable.

something like this (server script)

local HpEn = game.ReplicatedStorage.HpEn
local activatedBool = false

HpEn.OnServerEvent:Connect(function(Player, Activated)

local Char = Player.Character
print ("Server has been reached!")
print (Activated)
activatedBool = Activated
if Activated == true then

	local Newpart = Instance.new("Part")
	Newpart.Name = "What's Goodie?"
	Newpart.Shape = "Ball"
	Newpart.Transparency = 0.9
	Newpart.CanCollide = false
	Newpart.Anchored = true
	Newpart.Parent = game.Workspace
	Newpart.Position = Vector3.new(0,10,0)
	Newpart.Size = Vector3.new(20,20,20)
	Newpart.Material = "Neon"

		repeat 
			wait (0.001)
			Newpart.CFrame = Char.LowerTorso.CFrame
			print (Activated)
		until activatedBool== false

else if Activated == false then 
print ("PART HAS BEEN DESTROYED BRUH!")

		end
	end
end)
1 Like

If the bool is being given from the client though, how would that work? Since it won’t register as a variable outside of the Fireserver function

thats what the ‘activatedBool’ is for, since it was created outside the function it will change throughout the entire script even if its changed inside the remote function

1 Like