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

Why does it expect Identifier here?

local Animations = {
	
	Reload = function(S) --This is the main reload animation. The parameter S contains all the variables and functions that are necessary for this animation

		
		local W1 = nil
		local W2 = nil
		local animSpeed = S.isMagEmpty() and S.reloadTimeEmpty / 1.3 or S.reloadTimeLoaded / 0.9
		local Ammunition = script.Parent.Ammo.Value
		local ClipSize = script.Parent.ClipSize.Value
		local AmmoNeeded = ClipSize - Ammunition - 2
		return {
			function()
				if (not S.isNewMag()) then
					if S.isMagVisible() then
						local Mag1, magTable1 = S.createMag("Mag1")
							
						Mag1.Parent = S.gunIgnore
						
						W1 = Instance.new("Weld")
						W1.Part0 = magTable1[1].magClone
						W1.Part1 = S.Handle
						W1.C0 = magTable1[1].Original.CFrame:toObjectSpace(S.Handle.CFrame)
						W1.Parent = magTable1[1].magClone
						
						S.tweenJoint(S.LWeld, nil, S.CF(0.58, 1.63, -1.4) * S.CFANG(S.RAD(90), 0, S.RAD(-60)), S.Sine, 0.2 * animSpeed)
						S.tweenJoint(S.RWeld, nil, S.CF(0.3, 0.2, -0.51) * S.CFANG(S.RAD(-12), 0, S.RAD(25)), S.Sine, 0.2 * animSpeed)
						S.tweenJoint(S.Grip, nil, S.CFANG(0, S.RAD(20), S.RAD(10)), S.Sine, 0.1 * animSpeed)
						wait(0.2 * animSpeed)
					end
				end
			end;
			
			function()
				if (not S.isNewMag()) then
					if S.isMagVisible() then
						S.makeMagInvisible()
						W1:Destroy()
						local Mag1, magTable1 = S.getMag("Mag1")
						magTable1[1].magClone.Velocity = S.Handle.Velocity + S.Handle.CFrame:vectorToWorldSpace(S.V3(0, -1, 0)) * 20
						
						S.tweenJoint(S.Grip, nil, S.CFANG(0, S.RAD(20), S.RAD(10)), S.Sine, 0.2 * animSpeed)
					else

						S.tweenJoint(S.Grip, nil, S.CFANG(S.RAD(-10), S.RAD(20), S.RAD(10)), S.Sine, 0.2 * animSpeed)
					end
					
					S.tweenJoint(S.LWeld, nil, S.CF(0, 0.5, 0) * S.CFANG(S.RAD(95), 0, S.RAD(-25)), S.Sine, 0.2 * animSpeed)
					
					wait(0.25 * animSpeed)
				end
			end;
			
--	--  --	--	ERROR HERE -- on 'if' it says Expected identifier, got 'if'

			if AmmoNeeded < 0 then
				repeat
					function()
						if (not S.isNewMag()) then
							local Mag1, magTable1 = S.getMag("Mag1")
							if Mag1 then Mag1:Destroy() end
							
							local Mag2, magTable2 = S.createMag("Mag2")
							
							Mag2.Parent = S.gunIgnore
							
							local LArmCF = S.LWeld.Part0.CFrame * S.LWeld.C0 * (S.CF(0.58, 1.63, -1.4) * S.CFANG(S.RAD(-22), S.RAD(20), S.RAD(-60))):inverse()
							local RArmCF = S.RWeld.Part0.CFrame * S.RWeld.C0 * (S.CF(0.6, 0.2, -0.61) * S.CFANG(S.RAD(-15), S.RAD(20), S.RAD(25))):inverse()
							local handleOffsetCF = S.RArm.CFrame:toObjectSpace(S.RArm.CFrame * S.Grip.C0 * (S.CFANG(S.RAD(-10), S.RAD(20), S.RAD(10))):inverse())
							local originalMagOffsetCF = S.Handle.CFrame:toObjectSpace(magTable2[1].Original.CFrame)
							local newMagC0 = LArmCF:toObjectSpace(RArmCF * handleOffsetCF * originalMagOffsetCF)
							
							W2 = Instance.new("Weld")
							W2.Part0 = S.LArm
							W2.Part1 = magTable2[1].magClone
							W2.C0 = newMagC0
							W2.Parent = magTable2[1].magClone
							
							S.tweenJoint(S.LWeld, nil, S.CF(0.55, 0.6, -2.4) * S.CFANG(S.RAD(-20), S.RAD(20), S.RAD(-60)), S.Sine, 0.2 * animSpeed)--0.25
							S.tweenJoint(S.RWeld, nil, S.CF(0.6, 0.2, -0.61) * S.CFANG(S.RAD(-12), S.RAD(20), S.RAD(25)), S.Sine, 0.2 * animSpeed)
							S.tweenJoint(S.Grip, nil, S.CFANG(S.RAD(-10), S.RAD(20), S.RAD(10)), S.Sine, 0.2 * animSpeed)
							wait(0.2 * animSpeed)
						end
					end;
					
					function()
						if (not S.isNewMag()) then
							S.tweenJoint(S.Grip, nil, S.CFANG(S.RAD(-10), S.RAD(20), S.RAD(10)), S.Sine, 0.15 * animSpeed)
							S.tweenJoint(S.LWeld, nil, S.CF(0.58, 1.63, -1.4) * S.CFANG(S.RAD(-22), S.RAD(20), S.RAD(-60)), S.Sine, 0.15 * animSpeed)--0.25
							S.tweenJoint(S.RWeld, nil, S.CF(0.6, 0.2, -0.61) * S.CFANG(S.RAD(-15), S.RAD(20), S.RAD(25)), S.Sine, 0.2 * animSpeed)
							script.Parent.Handle.MagIn:Play()
							wait(0.2 * animSpeed)
						end
					end;
								function()
						if (not S.isNewMag()) then
							if S.isMagVisible() then
								S.makeMagInvisible()
								W1:Destroy()
								local Mag1, magTable1 = S.getMag("Mag1")
								magTable1[1].magClone.Velocity = S.Handle.Velocity + S.Handle.CFrame:vectorToWorldSpace(S.V3(0, -1, 0)) * 20
								
								S.tweenJoint(S.Grip, nil, S.CFANG(0, S.RAD(20), S.RAD(10)), S.Sine, 0.2 * animSpeed)
							else
		
								S.tweenJoint(S.Grip, nil, S.CFANG(S.RAD(-10), S.RAD(20), S.RAD(10)), S.Sine, 0.2 * animSpeed)
							end
							
							S.tweenJoint(S.LWeld, nil, S.CF(0, 0.5, 0) * S.CFANG(S.RAD(95), 0, S.RAD(-25)), S.Sine, 0.2 * animSpeed)
							
							wait(0.25 * animSpeed)
							AmmoNeeded = AmmoNeeded - 1
						end
					end;
				until AmmoNeeded = 0
			end


--  --  --			

			function()
				if (not S.isNewMag()) then
					local Mag1, magTable1 = S.getMag("Mag1")
					if Mag1 then Mag1:Destroy() end
					
					local Mag2, magTable2 = S.createMag("Mag2")
					
					Mag2.Parent = S.gunIgnore
					
					local LArmCF = S.LWeld.Part0.CFrame * S.LWeld.C0 * (S.CF(0.58, 1.63, -1.4) * S.CFANG(S.RAD(-22), S.RAD(20), S.RAD(-60))):inverse()
					local RArmCF = S.RWeld.Part0.CFrame * S.RWeld.C0 * (S.CF(0.6, 0.2, -0.61) * S.CFANG(S.RAD(-15), S.RAD(20), S.RAD(25))):inverse()
					local handleOffsetCF = S.RArm.CFrame:toObjectSpace(S.RArm.CFrame * S.Grip.C0 * (S.CFANG(S.RAD(-10), S.RAD(20), S.RAD(10))):inverse())
					local originalMagOffsetCF = S.Handle.CFrame:toObjectSpace(magTable2[1].Original.CFrame)
					local newMagC0 = LArmCF:toObjectSpace(RArmCF * handleOffsetCF * originalMagOffsetCF)
					
					W2 = Instance.new("Weld")
					W2.Part0 = S.LArm
					W2.Part1 = magTable2[1].magClone
					W2.C0 = newMagC0
					W2.Parent = magTable2[1].magClone
					
					S.tweenJoint(S.LWeld, nil, S.CF(0.55, 0.6, -2.4) * S.CFANG(S.RAD(-20), S.RAD(20), S.RAD(-60)), S.Sine, 0.2 * animSpeed)--0.25
					S.tweenJoint(S.RWeld, nil, S.CF(0.6, 0.2, -0.61) * S.CFANG(S.RAD(-12), S.RAD(20), S.RAD(25)), S.Sine, 0.2 * animSpeed)
					S.tweenJoint(S.Grip, nil, S.CFANG(S.RAD(-10), S.RAD(20), S.RAD(10)), S.Sine, 0.2 * animSpeed)
					wait(0.2 * animSpeed)
				end
			end;
			
			function()
				if (not S.isNewMag()) then
					S.tweenJoint(S.Grip, nil, S.CFANG(S.RAD(-10), S.RAD(20), S.RAD(10)), S.Sine, 0.15 * animSpeed)
					S.tweenJoint(S.LWeld, nil, S.CF(0.58, 1.63, -1.4) * S.CFANG(S.RAD(-22), S.RAD(20), S.RAD(-60)), S.Sine, 0.15 * animSpeed)--0.25
					S.tweenJoint(S.RWeld, nil, S.CF(0.6, 0.2, -0.61) * S.CFANG(S.RAD(-15), S.RAD(20), S.RAD(25)), S.Sine, 0.2 * animSpeed)
					script.Parent.Handle.MagIn:Play()
					wait(0.2 * animSpeed)
				end
			end;
			function()
				if (not S.isNewMag()) then
					local Mag1, _ = S.getMag("Mag1")
					local Mag2, _ = S.getMag("Mag2")
					S.makeMagVisible()
					S.setNewMag()
					if Mag1 then Mag1:Destroy() end
					Mag2:Destroy()
				end
			end;
			
			function()
				if S.isMagEmpty() then
					if S.isNewMag() then
						S.tweenJoint(S.Grip, nil, S.CFANG(S.RAD(-10), S.RAD(20), S.RAD(10)), S.Sine, 0.15 * animSpeed)
						S.tweenJoint(S.LWeld, nil, S.CF(0.58, 1.63, -1.4) * S.CFANG(S.RAD(-22), S.RAD(20), S.RAD(-60)), S.Sine, 0.15 * animSpeed)--0.25
						S.tweenJoint(S.RWeld, nil, S.CF(0.6, 0.2, -0.61) * S.CFANG(S.RAD(-15), S.RAD(20), S.RAD(25)), S.Sine, 0.2 * animSpeed)
					end
					S.tweenJoint(S.LWeld, nil, S.CF(0.2, 1.9, -0.45) * S.CFANG(S.RAD(-23), 0.1, S.RAD(-40)), S.Sine, 0.1 * animSpeed)
					
					wait(0.4 * animSpeed)
				end
			end;
			
			function()
				if S.isMagEmpty() then
					S.tweenJoint(S.LWeld, nil, S.CF(-0.2, 1.7, -0.45) * S.CFANG(S.RAD(-23), 0.1, S.RAD(-40)), S.Sine, 0.1 * animSpeed)
					script.Parent.Handle.Prime:Play()
					wait(0.3 * animSpeed)
				end
			end;
			
			function()
				if S.isMagEmpty() then
					S.tweenJoint(S.LWeld, nil, S.CF(0.2, 1.9, -0.45) * S.CFANG(S.RAD(-23), 0.1, S.RAD(-40)), S.Sine, 0.1 * animSpeed)
					
					wait(0.3 * animSpeed)
					

				end
			end;
		}
	end;

Line 56 has the error. Thanks to some people, I've realised that the error lies within the fact that I can't exactly return an if statement to a table - 'cos it's a returning function. But the goal of the if statement was to repeat a function a certain amount of times, which can vary depending on outside values. So help would be appreciated if you can help me do that.

Thank you!

N.B. Most of the code after line 119 is not needed to solve the problem.

2 comments

0

you could do <= 1

0

Unfortunately, that's not the problem, and since it's always integers, there was no need.

1 answer

✓ Accepted answer

What you are trying to do is return an if statement. There is also a problem with your repeat loop, which will cause it to run infinitely and crash the script (and possibly your server/client).


If Statement

return {
	if condition then
		-- code
	end
}

This code would error because an if statement is not a value type - it is simply code. You can only return values.

Instead, run the if statement before the return:

if condition then
	-- code
end
return {}

If something needs to be done in a specific order, run them in order before using return, then return the result instead.


Repeat Loop

Essentially what you were doing with this was:

repeat
	function()
		-- code
	end
until AmmoNeeded == 0

When you are defining a function, the function itself does not run. If you want to run a function, you do this:

functionName()

If you want to define a function (which doesn't run the code inside it), you do this:

function functionName()
	-- code
end

So, make sure to define the functions you want to run beforehand, then run them in the repeat loop, or just execute the code inside the loop:

local function Func1()
	-- code
end

repeat
	Func1()
until AmmoNeeded =0 0

or

repeat
	-- code
until AmmoNeeded == 0

Tip

The code you sent was rather large - for questions like this, try and keep the bare basics. It's very annoying to scroll through it all. We have no need to see the many lines of code inside the functions for a problem like this.


Hope I helped!

~TDP

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