Topic category: Troubleshooting, bugs, and solutions
This is the procedure of a sword that is called upon being crafted/smelted:
If random < 0.5
do set local Rand to random
if get local rand < 0.25
do Add enchantment 1
Play sound x
else if get local rand < 0.5
do Add enchantment 2
Play sound x
else if get local rand < 0.5
do Add enchantment 3
Play sound x
else
do Add enchantment 4
Play sound x
So basically there's a 50% chance that the weapon gets an enchantment. When it gets an enchantment it gets either 1 of 4 by random and always plays a sound.
However when testing I noticed some strange behaviours:
1. When crafting the item, it would play a sound and have an enchantment. Yet when the crafting table closes the enchantment would disappear.
2. When crafting the item, it would play a sound, have 1 enchantment, yet when closing the crafting table, it would change to another enchantment and keep that one instead.
Is this a server client side issue or a bug?
EDIT:
I also tried wrapping the procedure in an if statement checking for client-side only or server side only. When in clientside only: No enchantment ever stays on the item. Only during dragging the item to your inventory will you see it. No sounds will play either.
On server side only: This fixes most of the issue. However, you can no longer see the enchantment while dragging it out of the inventory. For strange reasons, this is only visible on clientside.
SOLUTION:
When adding an enchantment to an item on being crafted, the part where you drag it to your inventory is client-side, where as the enchantment you get is server side. Because this procedure randomises which enchantment you get, you'll get bugged feedback of what enchantment you get as you may drag out Sharpness 5 (Client-Side result) while you get Smite 5 (server side result) in your inventory.
Making the procedure Client-Side only prevents it from having any visible enchantment while dragging it out, but will correctly display your enchantment once in the inventory + playing the sound
This is because the random value can be different between client side and server side.
Best thing to do with random things is to keep it server side if it changes the world (placing/removing blocks), entities or items. Use client side for decorations (e.g. showing some particles, playing a sound)