How can i stop while true do loop?

So im trying to make it when the player loses his shield a value will regen , but it keeps on going when the object is destroyed

while shield.IsInUse.Value == true do
		wait()
		player.Character.Humanoid.Health = 100
		if not shield:FindFirstChild("IsInUse") then
			break
		end
	end
2 Likes

What type of script is this and what type of script are you using to destroy the value?

it’s a serverscript and im trying to destroy the value in the same script, that’s why i use if not but it didn’t do anything

Try wrapping the while loop in a:
spawn(function() end)

like this?

	spawn(function()
	while shield.IsInUse.Value == true do
		wait()
		player.Character.Humanoid.Health = 100
		if not shield:FindFirstChild("IsInUse") then
			break
		end
	end
end)
1 Like

Yes, that is how you wrap it.

Yes

it still errors

local player = game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local shield = char:WaitForChild("shield")

shield.Touched:Connect(function(hit)
	if hit.Parent == hit.Parent:FindFirstChild("Bullet") then
		player.BulletHit.Value = player.BulletHit.Value - 1
		repeat wait() until player.BulletHit.Value == 0
	end
	if player.BulletHit.Value <= 0 then
		shield:Destroy()
		shield.IsInUse.Value = false
	spawn(function()
	while shield.IsInUse.Value == true do
		wait()
		player.Character.Humanoid.Health = 100
		if not shield:FindFirstChild("IsInUse") then
			break
		end
	end
end)
	
	if not shield:FindFirstChild("IsInUse") then
		player.Character.Humanoid.Health = 100
	end
	
	end
end)

Maybe this?

local ShieldCor = coroutine.wrap(function()
		wait()
		player.Character.Humanoid.Health = 100
		if not shield:FindFirstChild("IsInUse") then
		break
	end
end)

while shield.IsInUse.Value == true do
ShieldCor()
end

Are you getting any logged errors in your output? You could try moving the loop outside of the touched event.

@IEnforce_Lawz when i try both this error shows

Could you show the shield in explorer, please?

no it’s because i destroyed it when a value was 0 but i want it to not error

Sorry, didn’t understand you?
(Text)

im destroying the shield and it’s erroring that it can’t find the value in the shield. So i want it to stop erroring


local player = game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local shield = char:WaitForChild("shield")

shield.Touched:Connect(function(hit)
	if hit.Parent == hit.Parent:FindFirstChild("Bullet") then
		player.BulletHit.Value = player.BulletHit.Value - 1
		repeat wait() until player.BulletHit.Value == 0
	end
	if player.BulletHit.Value <= 0 then
		shield:Destroy()
		shield.IsInUse.Value = false
		local ShieldCor = coroutine.wrap(function()
			wait()
			player.Character.Humanoid.Health = 100
		end)

		while shield.IsInUse.Value == true do
			ShieldCor()
		end
	
	if not shield:FindFirstChild("IsInUse") then
		player.Character.Humanoid.Health = 100
	end
	
	end
end)
1 Like
while shield:FindFirstChild("IsInUse")

I’d recommend trying a repeat loop,

repeat
	-- Your code here
until not shield:FindFirstChild("IsInUse")

One thing I noticed is how you put if not shield:FindFirstChild("IsInUse") then, I believe you meant to put shield:FindFirstChild("IsInUse").Value. Let me know if either of those help!

I would also like to point out that spawn is a very bad practice because of how it can delay depending on how much stuff you have. Here is a post that I searched that should go better into detail about it: Post here

Well for one, that error is occuring because the object IsInUse does not exist within the object.
Instead of

while shield.IsInUse.Value == true do

use

while shield:FindFirstChild("IsInUse") do

This line is useless when doing this:

if not shield:FindFirstChild("IsInUse") then
			break
		end

When the condition at the start of the while loop is no longer met, it already breaks, making that line redundant.

Also, instead of using a while loop, use Instance:GetPropertyChangedSignal.

You can just wrap the while true loop in a pcall function right?

while shield:FindFirstChild('IsInUse') and shield.IsInUse.Value do
	wait()
	player.Character.Humanoid.Health = 100
end

Edit: This will stop the rest of your lines until IsInUse does not exist or value is set to false, if you dont want this to happen wrap it into a coroutine.