Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Help with combat in my game.
- Thec001guy
-
Scratcher
50 posts
Help with combat in my game.
Does anyone know how to code cool downs so players don't spam attacks? Crosswalk kicker/my game
Last edited by Thec001guy (Oct. 14, 2025 23:26:06)
- PersonPersonson
-
Scratcher
83 posts
Help with combat in my game.
Something like this might work:
Waiting until the space key isn't pressed prevents spam from simply holding down the space bar.
The cooldown is decreased by 1 for every second, though you can adjust that as needed.
Cooldown must be zero to punch.
Lemme know how if this helped! Your game looks really cool so far!
You can experiment by setting the values to whatever works best for your game!
when [space v] key pressed
if <(cooldown) = [0]> then
punch
set [cooldown v] to [3]
end
wait until <not <key [space v] pressed?>>
when green flag clicked
forever
wait (1) secs
if <(cooldown) > [0]> then
change [cooldown v] by (-1)
end
end
Waiting until the space key isn't pressed prevents spam from simply holding down the space bar.
The cooldown is decreased by 1 for every second, though you can adjust that as needed.
Cooldown must be zero to punch.
Lemme know how if this helped! Your game looks really cool so far!
You can experiment by setting the values to whatever works best for your game!
Last edited by PersonPersonson (Oct. 15, 2025 02:28:28)
- Thec001guy
-
Scratcher
50 posts
Help with combat in my game.
What should I do instead because I changed the punch control to when the mouse is down.
- PersonPersonson
-
Scratcher
83 posts
Help with combat in my game.
What should I do instead because I changed the punch control to when the mouse is down.In that case, you can change the “wait until” block to “wait until not mouse down” so it doesn't register a new punch for every frame you hold down the mouse button.
As for the reddish block, it was meant to represent any code you have for punching. I didn't want to copy down your code so it would be less confusing about what's being added!
- Thec001guy
-
Scratcher
50 posts
Help with combat in my game.
Would I still add the
when [ space] key pressed
- OkEmber
-
Scratcher
25 posts
Help with combat in my game.
Would I still add thewhen [ space] key pressed
you can if its like this script,
when [ space ] key pressedI'll post an example in a bit of a refined script, you can use it if you like it without credit :3
if <(punch cd) < [(your cooldown for it v]> then
punch code
end
define punch code
your code
Last edited by OkEmber (Oct. 15, 2025 17:12:46)
- Thec001guy
-
Scratcher
50 posts
Help with combat in my game.
Well what do I code If the punch is by clicking/ mouse down?
if <mouse down?> then
set [punch] to [false]
end
Last edited by Thec001guy (Oct. 15, 2025 17:16:59)
- OkEmber
-
Scratcher
25 posts
Help with combat in my game.
Well what do I code If the punch is by clicking/ mouse down?if <mouse down?> then
set [punch] to [false]
end
hmm good question.
when green flag clicked
if <mouse down?> then
if <(Punch CD) < [whatever number]> then
punch
end
end
- Thec001guy
-
Scratcher
50 posts
Help with combat in my game.
It doesn't work:
when green flag clicked
if <mouse down?> then
if <[cooldown] < [4]> then
set [Punch] to [true]
when green flag clicked
forever
wait (4) secs
if <[cooldown] > [0]> then
change [cooldown] by (-1)
Last edited by Thec001guy (Oct. 15, 2025 18:16:55)
- PersonPersonson
-
Scratcher
83 posts
Help with combat in my game.
Could you share a project with the code so we can see?
- avalyra
-
New Scratcher
1 post
Help with combat in my game.
Hi fellow Scratcher! I have solved your cooldown issue. I removed your “When green flag clicked, if mouse down, then if cooldown < 4 then set punch to true” blocks. I also added a new variable. Here's the change I made. You can change the cooldown duration by changing the “set cooldown to” variable block. Hope it helps! 
(wait oh my days idk how to add an image here's the scratch project i uploaded the image onto: https://scratch.mit.edu/projects/1229787872/ )

(wait oh my days idk how to add an image here's the scratch project i uploaded the image onto: https://scratch.mit.edu/projects/1229787872/ )
Last edited by avalyra (Oct. 16, 2025 14:03:26)
- Discussion Forums
- » Help with Scripts
-
» Help with combat in my game.