コンテンツにスキップ

Why isn't my "when I start as clone" block working?

このコンテンツはまだ日本語訳がありません。

💡 Having trouble with Scratch block assembly? Don’t know how to implement code logic? 🚀 Get Help Now

SA

ScratchLearner_Alex

Posted on July 24, 2025 • Intermediate

🔧 Clone block not working for health bar

Hey everyone! I’m trying to create a health bar system using clones, but my “when I start as clone” block isn’t working properly. Here’s what I’m trying to do:

  • Create a clone for the health bar display
  • Position the clone correctly relative to the player
  • Update the health bar width based on current health

The main sprite works fine, but the clone doesn’t seem to execute its code. Any ideas what might be wrong? 🤔

CD

CloneDebugger_Pro

Replied 3 hours later • ⭐ Best Answer

Great question @ScratchLearner_Alex! Clone issues are super common, especially with health bars. Let me help you debug this step by step:

🔍 Common Clone Block Issues

Here’s a flowchart showing how clone creation and execution should work:

flowchart TD A[🚀 Main Sprite] --> B[Create Clone Block] B --> C[Clone Created] C --> D["When I Start as Clone" Triggered] D --> E{Clone Script Runs?} E -->|Yes| F[✅ Clone Executes Code] E -->|No| G[❌ Debug Needed] G --> H[Check Script Placement] G --> I[Check Variable Scope] G --> J[Check Timing Issues] H --> K[Move to Correct Sprite] I --> L[Fix Variable Access] J --> M[Add Wait Blocks] K --> F L --> F M --> F F --> N[Clone Behavior Works] style A fill:#e1f5fe style C fill:#f3e5f5 style F fill:#e8f5e8 style G fill:#ffebee style N fill:#e8f5e8

🔧 Issue #1: Variable Scope Problems

The most common issue is variables not being accessible to clones. Make sure your variables are set to “For all sprites”:

    // Make sure these variables are 'For all sprites'
when flag clicked
set [Health v] to [100]
set [max health v] to [100]
set [percent v] to [1]
set [width v] to [46]
  

💡 Issue #2: Corrected Clone Code

Here’s the fixed version of your health bar system:

Main Sprite Code:

    when flag clicked
switch costume to [base v]
set size to (100) %
go to [front v] layer
create clone of [myself v]
forever
go to [the guy v]
change y by (30)
end
  

Clone Code (Fixed):

    when I start as a clone
go to [front v] layer
switch costume to [Full v]
forever
go to [the guy v]
change y by (30)
set [percent v] to ((Health) / (max health))
// Fixed the width calculation
set [width v] to ((46) * (percent))
// Fixed positioning - move to base position first
go to [the guy v]
change y by (30)
// Then adjust x position based on health percentage
change x by (((percent) - (1)) * (23))
end
  

🎯 Issue #3: Better Health Bar Logic

For a more robust health bar system, try this approach:

    when I start as a clone
go to [front v] layer
switch costume to [Full v]
forever
// Follow the player
go to [the guy v]
change y by (30)

// Calculate health percentage
set [percent v] to ((Health) / (max health))

// Scale the health bar width
set size to ((percent) * (100)) %

// Position adjustment for scaling
set x to (([x position v] of [the guy v]) + (((1) - (percent)) * (-23)))
end
  

🚀 Pro Tips for Clone Debugging

  • Add visual feedback: Change clone color to see if it’s actually created
  • Use say blocks: Add temporary “say” blocks to see if clone code runs
  • Check sprite selection: Make sure the clone script is on the right sprite
  • Timing matters: Sometimes adding a small wait helps with initialization

Try this debugging version first:

    when I start as a clone
say [Clone started!] for (1) seconds
set [ghost v] effect to (50)
go to [front v] layer
switch costume to [Full v]
// Rest of your code here...
  

Hope this helps! Let me know if you’re still having issues! 😊

SA

ScratchLearner_Alex

Replied 45 minutes later

@CloneDebugger_Pro This is exactly what I needed! 🎉

The variable scope was indeed the issue - I had some variables set to “For this sprite only”. After changing them to “For all sprites”, the clone started working perfectly!

The debugging tips with the “say” blocks were super helpful too. Now I can see exactly when my clones are created and what they’re doing.

GM

GameMechanics_Expert

Replied 2 hours later

Great solution! For anyone else working with health bars, here are some additional enhancements you might want to consider:

🎨 Visual Improvements

  • Color coding: Change health bar color based on health level (green → yellow → red)
  • Smooth transitions: Use glide blocks for smooth health changes
  • Background bar: Add a gray background bar to show max health
    // Color-coded health bar
when I start as a clone
forever
if <(percent) > [0.6]> then
set [color v] effect to (0) // Green
else
if <(percent) > [0.3]> then
set [color v] effect to (30) // Yellow
else
set [color v] effect to (0) // Red
set [brightness v] effect to (-50)
end
end
end
  

Keep up the great work with game mechanics! 🎮

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Master Advanced Clone Techniques

Excellent debugging discussion! For those ready to dive deeper into clone programming, our community can help you master:

  • 🔄 Complex clone management systems
  • 🎯 Performance optimization for many clones
  • 🎮 Advanced game mechanics with clones
  • 🔧 Professional debugging techniques

📚 Related Topics

Ready to become a clone programming expert? Get personalized guidance from our Scratch specialists in the Vibelf app!