Repeat look not stopping

You can write your topic however you want, but you need to answer these questions:

  1. I want to make repeat loop when input state is end.

  2. It does not stop.

  3. I tried a lot, no time to mention.

Here is a part of the script, it is not stopping.

local function HandleActions(actionName, inputState, _inputObject)
	if actionName == ACTION_CHARGE and inputState == Enum.UserInputState.Begin then
		repeat
			wait(0.1)
			Force += 1
			print(Force)
		until inputState == Enum.UserInputState.End
			
	elseif actionName == ACTION_CHARGE and inputState == Enum.UserInputState.End then
		Throw()
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

The issue is that the value of inputState never changes within the scope of the function, and is only ever defined at the very start of the function.

A possible workaround is to have another variable outside of the function, like this:

local LoopActive = false
local function HandleActions(actionName, inputState, _inputObject)
	if actionName == ACTION_CHARGE and inputState == Enum.UserInputState.Begin then
		LoopActive = true
		repeat
			wait(0.1)
			Force += 1
			print(Force)
		until LoopActive == false -- or 'until not LoopActive'
			
	elseif actionName == ACTION_CHARGE and inputState == Enum.UserInputState.End then
		LoopActive = false
		Throw()
	end
end

(the values of the function’s arguments (actionName, inputState, _inputObject) will not change for a particular function call if the function is called again with a different set of parameters)

2 Likes

I tried that, that LoopActive stays true.

local function Throw()
	local ThrowProjectile = ThrowPhysics.NewProjectile(Character.PrimaryPart.Position, -100, Character, 4, Mouse.Hit.Position, Projectile, Force)
	ThrowProjectile:Fire()
	Force = 0
	Charging = false
end

local function HandleActions(actionName, inputState, _inputObject)
	if actionName == ACTION_CHARGE and inputState == Enum.UserInputState.Begin then
		Charging = true
		repeat
			wait(0.1)
			Force += 1
			print(Force)
			print("Charging: ", Charging)
		until Charging == false
			
	elseif actionName == ACTION_CHARGE and inputState == Enum.UserInputState.End then
		Throw()
	end
end```

i think its because this never happens (or it might, idk), can you add a print inside Throw function? and try running the repeat loop inside a task.spawn(function() end) cuz i suspect that the repeat loop blocks the elseif condition which makes it never run it, do it like this:

task.spawn(function()
       repeat
		task.wait(0.1)
		Force += 1
		print(Force)
		print("Charging: ", Charging)
	until Charging == false
end)
1 Like

That’s not how it works. Wrapping the code in task.spawn does not prevent the else-if from executing, as it won’t yield in the first place. The introduction to include task.spawn only serves to introduce bad practice and is snake oil.

local CurrentlyRunning = false
local function FunnyCode()
	if CurrentlyRunning == false then
		CurrentlyRunning = true
		print("Pineapple sauce!")
		wait(5)
		print("Ready to output a kind of sauce again!")
		CurrentlyRunning = false
	elseif CurrentlyRunning == true then -- If what you said was accurate, then...
		print("lol this is funny") -- This would never output.
	end
end

game:service("RunService").Stepped:Connect(FunnyCode)

The reason why it would never be blocked is because…

  1. The code runs a separate coroutine, and
  2. The initial conditional statement wouldn’t run the code first and then follow-up by checking the else-if conditional thereafter. That wouldn’t make sense.

Conditional statements will only run the instruction if the condition was truthy, and would move on and check the next one (if applicable) if the prior condition was falsey (if it was an else-if chain).

@OP Had you tried this same code using the UserInputService?

...
local Charging = false
local MaxCharge = 25
local ActionKey = Enum.KeyCode.E

...

UserInputService.InputBegan:Connect(function(InputInfo, WasProcessed)
	if WasProcessed then
		return
	end
	if InputInfo.KeyCode == ActionKey and not Charging then
		Charging = true
		while Charging do
			task.wait(0.1)
			Force = math.min(Force + 1, MaxCharge)
		end
		Throw()
	end
end)

UserInputService.InputEnded:Connect(function(InputInfo)
	if InputInfo.KeyCode == ActionKey and Charging then
		Charging = false
	end
end)
1 Like
local charging
local function HandleActions(actionName, inputState)
	if actionName == ACTION_CHARGE then
		if inputState == Enum.UserInputState.Begin then
			charging = true
			while charging do
				task.wait(0.1)
				Force += 1
				--print(Force)
			end
		elseif inputState == Enum.UserInputState.End then
			charging = false
			Throw()
		end
	end
end