Applescript defining "result" as a variable

I am trying to make an Applescript code that uses variables to stop a repeat loop, and I keep getting an error that the variable "result" is not defined. Here is my code:
Code Block applescript
set x to 0
display dialog "test" buttons {"Ok"}
repeat 4 times
if result = {button returned:"Ok"} then
set x to x + 1
display dialog "test" buttons {"Ok"}
if x = 4 then
display dialog "test successful"
exit repeat
end if
end if
end repeat


I keep getting this error:

The variable result is not defined.

Can someone explain to me why this keeps happening?

Thanks, I really appreciate it!


In AppleScript, control statements such as if are not expressions and do not return results - until a statement that yields a result is executed, the value of the built-in result property is undefined.

You shouldn't rely on the value of the result property remaining stable across multiple statements - in this case, when executing the if x = 4 statement in the repeat loop, the property is getting cleared.  This kind of issue can be avoided by explicitly setting your own variable to a value to test, for example set myResult to button returned of (display dialog "whatever").
Thanks your your reply, this worked for me!

Applescript defining "result" as a variable
 
 
Q