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

Is it possible to load an animation once and play it in the same script?

Is it possible to load an animation once and play it in the same script? I keep getting an error. I'm using a remote event, it fired when the player click on the textbutton, inside the player's Combat GUI

if loadA == true then
loadA = false
local punch = char.Target:LoadAnimation(animation)
print("Punch Loaded")
end
	
punch:Play()	<--- got an error here
	

2 answers

That error occurs when loadA==false because the declaration of variable punch is inside of the if statement and the Play call is outside of the if statement.

I suggest putting the call inside of the if statement.

Sample: Declaring a local variable inside of an if statement will be nil or the value declared on the same level if used outside of the if statement.

local a = 1
local b = 2
if true then
	a = 4
	local b = 3
	print(a,b) --4,3
end
print(a,b) -- prints 4 because you changed the value in the if statement but prints 2 after because you assigned variable b, thus variable b would be 3 inside of the if statement but not outside.

Comment on me if I should correct anything.

Like the post above said your problem is with scoping and also the fact that when that line of code runs punch might not be defined yet

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