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

Why does my loop fail to run?

I have this script in a tool.

ball = script.Parent
running = false
hoop1 = game.Workspace["Basketball Court"].Hoop1
hoop2 = game.Workspace["Basketball Court"].Hoop2

ball.Equipped:connect(function()
	character = ball.Parent
	local weld1 = Instance.new("Weld",character.Torso)
	weld1.Name = "Right Arm"
	weld1.Part0 = character.Torso
	weld1.Part1 = character["Right Arm"]
	weld1.C1 = CFrame.new(-1.5,0.5,0.25)  * CFrame.Angles(4.75,-0.15,0)
	local weld2 = Instance.new("Weld",character.Torso)
	weld2.Name = "Left Arm"
	weld2.Part0 = character.Torso
	weld2.Part1 = character["Left Arm"]
	weld2.C1 = CFrame.new(1.5,0.5,0.25)  * CFrame.Angles(4.75,0.15,0)
	ball.GripPos = Vector3.new(0,1.3,0.1)
	character.Humanoid.Running:connect(function()
		running = true
		ball.GripPos = Vector3.new(1,0,0)
		weld1.C1 = CFrame.new(-1.5,0.5,0.5) * CFrame.Angles(4.7,0,0)
		weld2:Destroy()
	end)
end)

ball.Unequipped:connect(function()
	running = false
	player = ball.Parent.Parent
	character = player.Character
	for i,v in pairs(character:FindFirstChild("Torso"):GetChildren()) do
		if v:IsA("Weld") then
			v:Destroy()
		end
	end
end)

local c1 = coroutine.resume(coroutine.create(function()
	while running == true do
		print("Does this crud even work?")
		repeat 
			ball.GripPos = Vector3.new(ball.GripPos.X + 0.1,0,0)
			wait()
		until ball.GripPos.X == 2.5
		repeat 
			ball.GripPos = Vector3.new(ball.GripPos.X - 0.1,0,0)
			wait()
		until ball.GripPos.X == 1
		wait(0.5)
	end
end))

Whenever I print the value 'running', it's true when I want it to be and the coroutine runs, but the loop inside doesn't. I have no errors in the output. Any suggestions?

2 comments

0

Try getting rid of the coroutine (there's no reason to use one here) -- that may fix the problem on its own, otherwise, fewer things for us to check

0

I removed the coroutine. Still doesn't work.

1 answer

Remember -- once a while loop stops, control flow begins past it.

running starts as false, so it will look at the loop and decide it's done.


Instead, what you probably mean is "whenever" running is true; when it's false you're just waiting for it to be true again.

The clearest way to do that looks like this:

while true do
	wait(0.5)
	if running then
		-- Do whatever when running
	end
end

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