Beginner Script Not Working Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ErmeNs27

Beginner Script Not Working

23 Apr 2018, 11:17

Hey, I am a beginner with AHK the language. I made a script but is doesnt work. I didnt know a fitting subject.

The error is:

unexpected "}" at 011

This is the script:

Toggle = "off"

GetKeyState, state, f
if state = D {
if (Toggle = "off")
Toggle = "on"
else
Toggle = "off"
}

Loop {
if (toggle = "on") {
click
sleep 40
}
}

p::
ExitApp
gregster
Posts: 9021
Joined: 30 Sep 2013, 06:48

Re: Beginner Script Not Working  Topic is solved

23 Apr 2018, 11:32

The line if state = D { doesn't work like you expect.

Unlike your other ifs (which use expression-style if syntax), this is "traditional" if syntax and the trailing, opening { is not allowed here on the same line. (AHK thinks here that you want to use the string D { for the comparison). Hence, the closing } a bit later is "unexpected" - it is missing its opening brace. Either use also expression syntax here: if (state = "D") { (recommended) - or put the opening brace on the next line:

Code: Select all

if state = D 
{
compare ( https://autohotkey.com/docs/commands/If ... on.htm#otb )
Last edited by gregster on 23 Apr 2018, 11:44, edited 2 times in total.
gregster
Posts: 9021
Joined: 30 Sep 2013, 06:48

Re: Beginner Script Not Working

23 Apr 2018, 11:36

Also, change the Toggle = "off" assignment to Toggle := "off". The same goes for Toggle = "on" to Toggle := "on" . But only the ones which are not in the if statements - there you want to compare, not to assign like with variables.

Same problem here like above - confusion between traditional and expression syntax for assigning variables: https://autohotkey.com/docs/Variables.htm
Try to always use expression syntax for ifs and assigning variables.
ErmeNs28

Re: Beginner Script Not Working

23 Apr 2018, 11:53

Ok thanks man! Now I have this, but it is still not clicking when I press the "f" button.

Toggle := "off"

GetKeyState, state, f
if (state = "D") {
if (Toggle = "off") {
Toggle = "on"
}
else {
Toggle = "off"
}
}

Loop {
if (toggle = "on") {
click
sleep 40
}
}

p::
ExitApp
gregster
Posts: 9021
Joined: 30 Sep 2013, 06:48

Re: Beginner Script Not Working

23 Apr 2018, 18:18

You still didn't change all variable assignments to the right syntax (you changed one, but missed two). Try to use = only for if-comparisons (or == for case-sensitive comparisons).

I don't know your exact use case. I assume it has to do with gaming - about which I know nothing (well, a little some time ago... but not in combination with AHK, anyway).
But I guess that your script is not ideal for what you have in mind. I would rather try something like this:

Code: Select all

f::
Sleeptime := 2000  		; milliseconds between clicks
toggle := !toggle		; well... a toggle
SetTimer clickit, % toggle ? Sleeptime : "off"	; Starts a timer that calls the function clickit() in the above defined time steps - or stops it, if it is already running.
return

clickit() {				; a function definition
    click
}

p::ExitApp
This works for me, but depending on your game it perhaps might not...
(Note that I added more time between the clicks for my testing.)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada and 319 guests