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

Error during for and repeating loops?

Upon my scripting on a gun (which i have never done before), i met various of situations,errors and bugs which took me hours to resolve and not like the others, i'm still not very confident on what i have done: Not really used and trained on using loops and such, but i attemped to make an animation that works whenever you press the SHIFT KEY

OBJECTIVES

  • Have the arm sway back and fourth.
function userrun(localkey)
	if localkey:byte() == 48 and bool_ready == true and bool_run == false then
	if bool_move == true then
	bool_run = true
	for frame = 1,10 do
	regleft.C0 = regleft.C0 * CFrame.Angles(0,0,-0.0150)
	wait()
	end	
	repeat -- LOOP STARTS
	
	for frame = 1,10 do
	regleft.C0 = regleft.C0 * CFrame.Angles(0.0550,0,0)
	wait(.1)
	end
	
	for frame = 1,10 do
	regleft.C0 = regleft.C0 * CFrame.Angles(-0.0550,0,0)
	wait()
	end
	
	until bool_run == false
end
	end
end
mouse.KeyDown:connect(userrun)

Here's a part of the script, which states the 'repeat' loop that is composed of 2 'for' loops. I want the arm to sway, having an exact timing for me to prevent any errors to the arm itself. 'regweld' are some welds in the arm i used so i can perform this type of method. and of course, useless(in this situation) booleans.

The result: https://gyazo.com/ad706a563d6706ce41dcd16e72effcf5

As you see, it does NOT sway properly, due to the fact that it breaks the arm and continue moving forward and forward, i believe it is due to the speed of both loop that has no 'wait' in it. But that just makes it too slow, not synchronizing with the steps of the character.

Any scripters can help me out here and give me an idea or something that i can do for me to surpass and resolve this problem?

  • LW14

1 answer

First of all, can we please tab out code correctly if we are going to tab it? Also, keydown is deprecated, or does not work correctly. So we should use UserInputService or something like this in a LOCAL SCRIPT.

game:GetService("UserInputService"):connect(function(key)

if key == Enum.KeyCode.LeftShift or key == Enum.KeyCode.RightShift then --This checks if the key the user pressed is left shift or right shift.
--stuff
end
end)

Also, wait() has a optianal argument. If it is not supplied with any, it will wait for 0.3 seconds, but sometimes you see that it waits longer than the amount supplied! Why does this happen? Well, it turns out that ROBLOX is very busy. There is currently no way to be 100% sure that it will wait for the amount you supplied in the wait. Also, you should never do this if you dont want a laggy game.

while wait(0.000000000000000000000000000000000000000000000000000001) do --Just never make a wait like that. Unless you have a super good computer, it is going to lag alot.
warn("I am laggy!")
end

Since we cant make a wait like that, we should make a animation. Click here to learn how to make a animation. If this helps, accept the answer!

1 comment

0

You didn't even answer the question..

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