CastorFlynn
Member
- Joined
- Aug 29, 2021
- Messages
- 100
- Reaction score
- 15
Comparing the two codes, which would be the most logical and safe?
LUA:
local shovel = TalkAction("!shovel")
local cost = 100
function shovel.onSay(player, words, param)
local item = Game.createItem(2554, 1)
if not item then
player:sendCancelMessage("An error occurred while creating the item.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return
end
if player:addItemEx(item) ~= RETURNVALUE_NOERROR then
player:getPosition():sendMagicEffect(CONST_ME_POFF)
player:sendCancelMessage("You do not have enough capacity or space in your inventory.")
return
end
if not player:removeMoneyNpc(cost) then
player:sendCancelMessage("You do not have enough money.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
player:removeItem(item:getId(), 1)
return
end
player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have bought a shovel!")
end
shovel:register()
LUA:
local shovel = TalkAction("!shovel")
function shovel.onSay(player, words, param)
local tempItem = Game.createItem(2554, 1)
if not tempItem then
return false
end
if player:addItemEx(tempItem) ~= RETURNVALUE_NOERROR then
tempItem:remove()
player:sendCancelMessage("You do not have enough capacity to carry this item.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end
tempItem:remove()
if not player:removeMoneyNpc(100) then
player:sendCancelMessage("You do not have enough money.")
player:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end
player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
player:addItem(2554, 1)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have bought a shovel!")
end
shovel:register()