Is there is a way to replace repeat wait() until? (In other words is there is a way to pause the script until something become true ?)

local DB = false
script.Parent.Touched:Connect(function(Touch)
	local TouchingParts = script.Parent:GetTouchingParts()
	local player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent) 
	if player then
		if DB == false then
			DB = true
			game.SoundService:PlayLocalSound(game.SoundService.SpotSound)
			script.Parent.TouchEnded:Connect(function(TouchEnd)
				game.SoundService.SpotSound:Stop()
				repeat wait() until script.Parent.Parent.SpotScreen.SurfaceGui.ScreenFrame.ScreenText.Text == "0/2"
				DB = false
			end)
		end
	end
end)

so I have this script and as you see here

repeat wait() until script.Parent.Parent.SpotScreen.SurfaceGui.ScreenFrame.ScreenText.Text == “0/2”

I am using repeat wait() until so is there is a way to replace it with something that pauses the script until the text changes to 0/2 then continue the rest of the code to prevent looping ?

There’s a ton of other alternatives out there that I could name, but I’ll just start out with this:

If you want to detect when the text changes to a specific value, you can use the GetPropertyChangedSignal function that’ll detect for a certain property of the Instance (The SpotScreen.ScreenFrame.ScreenText.Text in your case) and it’ll fire whenever that said property changes

Also please don’t use TouchEnded, it can become incredibly unreliable at times & results in some wacky effects especially since you’re scoping it in the wrong order

local Plrs = game:GetService("Players")
local SS = game:GetService("SoundService")

local DB = false
local Part = script.Parent
local ScreenText = script.Parent.Parent.SpotScreen.SurfaceGui.ScreenFrame.ScreenText

Part.Touched:Connect(function(Touch)
    local Target = Touch.Parent

    if not Target or DB then
        return
    end

    local Plr = Plrs:GetPlayerFromCharacter(Target)

    if Plr then
        DB = true
        SS:PlayLocalSound(SS.SpotSound)
    end
end)

ScreenText:GetPropertyChangedSignal("Text"):Connect(function()
    SS.SpotSound:Stop()

    if ScreenText.Text == "0/2" then
        DB = false
    end
end)

You’ll have to find an alternative to TouchEnded, something like using Region3 or Magnitude can work fine as you’re only checking when you want to get far away from the Part (As opposed to not touching the part anymore)

You could do:

local RS = game:GetService("RunService")
local hb
hb = RS.Heartbeat:Connect(function()
	if Condition then
		-- code here
		hb:Disconnect()
	end
end)

I did not use your script but I used the GetPropertyChangedSignal as you said Thank You

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.