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

How to give enough time for a loop, and then play another?

Sorry if I baldy explained the tittle, what I want is when tool's activated it does a slash but when I test it the first one doesn't have enough time to finish and the second one starts automatically.

Script:

script.Parent.Activated:connect(function()
	RightShoulder = script.Parent.Parent.Torso["Right Shoulder"]
	moving = false
	for i = 1,10 do
		if moving == false then
			moving = true
			RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0,0,.1)
			wait()
		end
	end
	for i = 1,10 do
		if moving == true then
			RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0,0,-.1)
			wait()
			moving = false
		end
	end
end)

1 answer

The code inside the if statements in both of your loops will only run a single time.

In the first loop, this is because moving must equal false in order for the code to be executed.

This is fine for the first iteration, but then you change the value of moving -- you make it equal true.

This means that once the code reaches the second iteration, moving now equals true and will not pass the if statement.

It's the same for the second loop, only reverse.


Because of the way your code is executed -- left to right, top to bottom -- the second loop won't start until the first one is finished. Therefore, the problem talked about in your post is no problem at all.


You likely added your debounce variable so that people won't be able to glitch out the animation. Just use it like a normal debounce, checking it before you start anything else.

local moving = false
script.Parent.Activated:connect(function()
	if not moving then
		moving = true
		RightShoulder = script.Parent.Parent.Torso["Right Shoulder"]
	
		for i = 1,10 do
			RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0,0,.1)
			wait()
		end
	
		for i = 1,10 do
			RightShoulder.C0 = RightShoulder.C0 *CFrame.Angles(0,0,-.1)
			wait()
		end
		moving = false
	end
end)

Although you really should look into animations, as they will allow smoother and more complex movements.

2 comments

0

I tried animations, but when I tested it, the script wouldn't work with the tool equipped. So I tried this. Thanks!

0

That's probably because something in the tool is welded in some way that prevented the motion.

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