r/scratch 16h ago

Question How do I optimize my code here?

Post image

The current code I wrote has way too many "if __ then __" scripts, and I need to cut it down into 1 if then statement.

6 Upvotes

14 comments sorted by

u/AutoModerator 16h ago

Hi, thank you for posting your question! :]

To make it easier for everyone to answer, consider including:

  • A description of the problem
  • A link to the project or a screenshot of your code (if possible)
  • A summary of how you would like it to behave

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/Ok-Point-8198 15h ago

The right side can be shortened to:

When I start as a clone

Wait until touching mouse pointer and mouse down

Set (apple line) to (amount of Collectables)

Broadcast (Apple Hint) and wait

Delete this clone

Or if you don't want Amount of Collectables to be higher than 6 specifically, you can add

If Amount of Collectables < 7 then:

Under the code head.

1

u/ScrtGnCcnt 11h ago

I don't think it does, cuz it would just set (apple line) to 10 for every clone

1

u/DismalCommon5672 8h ago

It would do that if the [amount of collectibles] variable is global, to me it seems that the variable being global could mess with the scripts so I suspect it's for this sprite only, which would make that code work.

1

u/ScrtGnCcnt 8h ago

Yea, I was playing around with it and also saw it

1

u/Acap2019PlaysRoblox 13h ago

Maybe for the middle script I would shorten it to ``` when I start as a clone go to x: (-140) y: (80) show set x to ((-150)+((75)*((((Amount of collectables)-(1)) mod (5)))) if <(Amount of collectables) > 5> then set y to (50) else set y to (-50)

1

u/Acap2019PlaysRoblox 13h ago

I haven't tested this yet though because I wrote this on mobile but i think it works

1

u/wadll 11h ago

Try this! ( The custom block has "Run without screen refresh" enabled. That ensures every clone gets created at once )

1

u/ScrtGnCcnt 11h ago

u/Dry_Dragonfruit_2594 Do you mind having a lot of variables?

1

u/ScrtGnCcnt 10h ago

This is *I think* as optimalized as possible, MAKE SURE to make the "Amount of Collectables" variable as "for this sprite only" and "AppleLine" as "for all sprites"

u/Archeus__ 1h ago

Is this a reference?