Saltearse al contenido

How to fix shop system pricing bugs in Scratch games

Esta página aún no está disponible en tu idioma.

💡 Having trouble with shop system bugs? Need help with purchase validation logic? 🚀 Get Help Now

GA

GameDev_Alex

Posted on January 22, 2024 • Intermediate

🛒 Shop system pricing bug - prices doubling randomly

Hey everyone! I’m working on a game called “YouTube Simulator X” and I’ve run into a frustrating bug with my shop system. The prices for items are sometimes showing up as twice the normal amount! 😤

It started happening with cameras, then spread to PCs. Here’s what my purchase code looks like:

when I receive [yCameraPurchase v]
change [CameraPurchases v] by (1)
set [CameraPower v] to (item ((CameraPurchases) v) of [list v] :: _Camera Benefits)
change [Cash v] by (item ((CameraPurchases) v) of [list v] :: _Camera Prices)
if <(Cash) < [0]> then
set [CameraPower v] to [1]
stop [all v]
else
Skip
end

The weird thing is it’s inconsistent - sometimes it works fine, other times the prices are doubled. Has anyone seen this before? Any ideas what might be causing this? 🤔

SP

ShopSystem_Pro

Replied 1 hour later • ⭐ Best Answer

@GameDev_Alex I see the issue! Your purchase validation is happening AFTER the purchase, which is causing the pricing bug. Here’s what’s going wrong and how to fix it:

🐛 The Problem

Your current code:

  1. Increases the purchase count first
  2. Deducts money based on the NEW purchase level
  3. Only THEN checks if you have enough money

This creates a race condition where the price calculation uses the wrong index!

flowchart TD A[🛒 Purchase Triggered] --> B[❌ Increase Purchase Count] B --> C[💰 Deduct Money Using NEW Count] C --> D{💸 Check if Cash < 0?} D -->|Yes| E[🔄 Rollback - But Price Already Wrong!] D -->|No| F[✅ Purchase Complete] style A fill:#e1f5fe style B fill:#ffebee style C fill:#ffebee style E fill:#ffcdd2 style F fill:#e8f5e8

✅ The Solution

Always validate BEFORE making changes. Here’s the corrected code:

    when I receive [yCameraPurchase v]
// First, check if we can afford the NEXT item
if <(Cash) ≥ (item ((CameraPurchases) + [1]) of [_Camera Prices v])> then
// Only proceed if we have enough money
change [CameraPurchases v] by [1]
set [CameraPower v] to (item (CameraPurchases) of [_Camera Benefits v])
change [Cash v] by ((-1) * (item (CameraPurchases) of [_Camera Prices v]))
play sound [purchase success v]
else
// Show error message
say [Not enough money!] for [2] seconds
play sound [error v]
end
  

🚀 Enhanced Version with Better UX

For an even better shop experience:

    when I receive [yCameraPurchase v]
// Check if already at max level
if <(CameraPurchases) = (length of [_Camera Prices v])> then
say [Max level reached!] for [2] seconds
play sound [max level v]
else
// Get the price of the next upgrade
set [next price v] to (item ((CameraPurchases) + [1]) of [_Camera Prices v])

if <(Cash) ≥ (next price)> then
// Successful purchase
change [CameraPurchases v] by [1]
set [CameraPower v] to (item (CameraPurchases) of [_Camera Benefits v])
change [Cash v] by ((-1) * (next price))

// Visual feedback
say (join [Upgraded! New power: ] (CameraPower)) for [2] seconds
play sound [purchase success v]

// Optional: Show floating text effect
broadcast [show upgrade effect v]
else
// Not enough money - show helpful message
set [needed v] to ((next price) - (Cash))
say (join [Need $] (needed)) for [2] seconds
play sound [insufficient funds v]
end
end
  

🔧 Additional Tips for Robust Shop Systems

1. Use Custom Blocks for Reusability:

    // Custom block: purchase item
define purchase item (item type) (price list) (benefit list) (purchase counter)
if <(item (purchase counter) of (price list)) ≤ (Cash)> then
change (purchase counter) by [1]
change [Cash v] by ((-1) * (item (purchase counter) of (price list)))
// Apply benefits
broadcast (join [upgrade ] (item type))
else
say [Insufficient funds!] for [2] seconds
end
  

2. Add Purchase Confirmation:

    when I receive [camera purchase request v]
set [confirm purchase v] to [false]
ask (join [Buy camera upgrade for $] (item ((CameraPurchases) + [1]) of [_Camera Prices v]))
if <(answer) = [yes]> then
set [confirm purchase v] to [true]
broadcast [yCameraPurchase v]
end
  

3. Save/Load Shop State:

    // Save shop data
define save shop data
set [shop data v] to (join (CameraPurchases) (join [,] (PCPurchases)))
// Use cloud variables or local storage

// Load shop data
define load shop data
if <(length of (shop data)) > [0]> then
set [CameraPurchases v] to (item [1] of (split (shop data) by [,]))
set [PCPurchases v] to (item [2] of (split (shop data) by [,]))
end
  

This approach eliminates the pricing bug and makes your shop system much more reliable! 🎯

GA

GameDev_Alex

Replied 45 minutes later

@ShopSystem_Pro This is EXACTLY what I needed! 🎉 The validation-first approach makes so much sense now.

I implemented your solution and the pricing bug is completely gone. The enhanced version with the UX improvements is fantastic too - my players are going to love the better feedback!

Thank you so much for the detailed explanation and the bonus tips! 🙏

ED

EconomyDesigner_Sam

Replied 2 hours later

Great solution @ShopSystem_Pro! 👏 I’d like to add some game economy design tips for shop systems:

  • Progressive Pricing: Make each upgrade cost more than the last (exponential growth)
  • Value Feedback: Show players what they’re getting for their money
  • Purchase Limits: Consider max levels to prevent infinite progression
  • Refund Options: Allow players to sell back items at reduced price

These design patterns help create engaging and balanced shop systems! 🎮

VB

Vibelf_Community

Pinned Message • Moderator

🛒 Master Advanced Shop Systems

Excellent debugging discussion! For developers looking to create even more sophisticated shop systems, our community can help you implement:

  • 🏪 Multi-currency systems
  • 🎁 Dynamic pricing and sales
  • 📦 Inventory management
  • 🔄 Item trading between players

📚 Related Discussions

Ready to build professional-grade shop systems? Get expert guidance from our experienced game developers!