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

Is there any way to fire a line of code once while in a while loop?

local player = game.Players.LocalPlayer
local char = player.Character
if not char or not char.Parent then
    char = player.CharacterAdded:wait()
end
local zeffTorso = game.Workspace:WaitForChild('Weirdo'):WaitForChild'LowerTorso'
local ready = script:WaitForChild('Ready')
local ready2 = script:WaitForChild('Ready2')
local dist = 5

while wait() do
	local distance = player:DistanceFromCharacter(zeffTorso.Position)
	
	if distance <= dist and ready2.Value ~= true then
		print("yes")
		
		
	elseif distance >= dist then
		print("no")
		
		
	end

	


	
end

Nothing's wrong with th code, its just that the yes and no's fire endlessly when i only want it to fire once

2 comments

0

In your logic, if distance is == to ready2.Value, yes and no will both be printed.

0

@JasonTheOwner trust me, the script runs fine

1 answer

Is this what you're looking for?

local player = game.Players.LocalPlayer
local char = player.Character
if not char or not char.Parent then
    char = player.CharacterAdded:wait()
end
local zeffTorso = game.Workspace:WaitForChild('Weirdo'):WaitForChild'LowerTorso'
local ready = script:WaitForChild('Ready')
local ready2 = script:WaitForChild('Ready2')
local dist = 5
local runonce=false
while wait() do
	if runonce==false then
		runonce=true
		 local distance = player:DistanceFromCharacter(zeffTorso.Position)

	    if distance <= dist and ready2.Value ~= true then
	        print("yes")
	
	
	    elseif distance >= dist then
	        print("no")
	
	
	    end
	end
   





end


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