• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua I am trying to create a worm digging script

bladeatblackd

New Member
Joined
Oct 19, 2010
Messages
7
Reaction score
0
Im not that good at lua and I have used bits and peices from other scripts to get what I have so far.

Problems:
1. Its not stacking worms
2. I would like to make it a chance thing so you dont get a worm EVERY time. Kinda like fishing script.

Also is there any tutorials on LUA? Im scripting out and mapping out a 100% Custom map server and I would like to make some scripts without asking for help on everyone.

Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
  if getPlayerItemCount(cid, 3976) > 15 then ---This section searches the character for the items required for the outfit or addon 9971 needs to be replaced with the item needed and 15 with the amount needed--
  return doPlayerSendCancel(cid, "You don't need that many worms! Its time to go fishing!"), doSendMagicEffect(getThingPos(cid), CONST_ME_POFF), false
  end
   
   local random, effect = math.random(1, 100000), CONST_ME_GROUNDSHAKER
     if(random <= 3976) then
       doPlayerAddItem(cid,3976, 1)
     elseif(3973 and random >= 3976) then
       doPlayerAddItem(cid, 3976, 1)
     else
       effect = CONST_ME_POFF
     end

       doSendMagicEffect(toPosition, effect)
   return true
end
 
Last edited:
skimming your code I skipped over the first if statement because your comments are confusing... if you're just trying to say "if the player has more than 15 worms, stop the player because the player is greedy".. that should do the trick. BUT! consider formatting the if statement so it is more readable (indent the return statement for example) and removing that long comment that brings my attention to addons unnecessarily.

Use this rather than the line you have:
Code:
local random = math.random(1, 100000)
local effect = CONST_ME_GROUNDSHAKER

Also, why "1, 100000"? Isn't 100000 kind of huge?

ALSO, in my server, the correct version of this would be:
Code:
local random = math.random(100000)
...no need for "1, "

What distribution are you using?

Final statement:
your if and elseif statements include "random <= 3976" and then "random >= 3976" one of those should be "<" or ">" without the "=". Also, the elseif statement declares "elseif(3973 and...)" is there supposed to be some operator on 3973? or just elseif 3973 is true? because 3973 is always a number, numbers don't normally consider whether they would rather be true or false... ever!
 
Unfortunately I know of no comprehensive .lua guide. I can .lua program because I am fluent in many programming languages and draw from a general programming understanding to speak this mutt language. I think the best thing you can do is learn C or C++ fully... (or other languages that are newer, perhaps, I just know that C applies pretty well here).

If you aren't a college student, or won't be a college student in the future, you can't really take a college course in introductory programming.. so try this quirky guy and his elaborate free lectures on many languages:

http://thenewboston.org/tutorials.php

Generally speaking .lua programming is basic programming while using a library of functions that is different per distribution. The Forgotten Server at least LISTS the available functions, but they don't describe what the input and output of each function is.. unfortunately.
 
You messed up because your script says if its LESS THEN 3976 then give them a worm....Then you go on to say if its more then 3976 then give them a worm....

You need to have a if its more then 3976 and less then idk 5000 then give them a worm (which would mean anything over 5000 wouldnt give them anything....

You can use this script.. This will also stack the worms...

Code:
local ground_to_dig = {1111, 1111} --tiles that can be dug on
local ground_to_dig_uid = 30000  --Only tiles with this unique id will be able to be dug in (set in RME)

function onUse(cid, item, fromPosition, itemEx, toPosition)
if (isInArray(ground_to_dig, itemEx.itemid) then
    if item.uid == ground_to_dig_uid then
  if getPlayerItemCount(cid, 3976) > 15 then
  doPlayerSendCancel(cid, "You don't need that many worms! Its time to go fishing!")
  doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
  return false
  end

  local rand_worm = math.random(1, 50)
  local effect = CONST_ME_GROUNDSHAKER
  if(rand_worm <= 12) then
     local amount = getPlayerItemCount(cid, 3976)
     doPlayerRemoveItem(cid, 3976, amount)
  doPlayerAddItem(cid,3976, amount + 1)
  elseif(rand_worm > 12) and (rand_worm < 20) then
  local amount = getPlayerItemCount(cid, 3976)
     doPlayerRemoveItem(cid, 3976, amount)
  doPlayerAddItem(cid,3976, amount + 1)
  else
  effect = CONST_ME_POFF
  end
  doSendMagicEffect(toPosition, effect)
end
end
  return true
end
 
Last edited:
@Drakkhan
Sorry about the comment code. This script was something I was trying to peice together from other scripts. That was something from one of the other scripts I was using. :p

The reason for the 1, 10000 was because that was on the mino leather script and I did not change it. I was just trying to get the proper function down before messing with the variables.

I took this entire part from a mino script and modified it. I wasn't quite sure what the functioning was thats why I have both >= and <=
local random, effect = math.random(1, 100000), CONST_ME_GROUNDSHAKER
if(random <= 3976) then
doPlayerAddItem(cid,3976, 1)
elseif(3973 and random >= 3976) then
doPlayerAddItem(cid, 3976, 1)
else

Im using the TFs 0.3.6 Crying Damson V8

I might actually see if I can download some form of recorded classes on programing. Else I might sign up for one.

@SnafuO1
Thanks man I will have to try it out. Can you explain which part would actually be doing the stacking?

local amount = getPlayerItemCount(cid, 3976) --This is it counting the worms? and registering the number--
doPlayerRemoveItem(cid, 3976, amount + 1) -- Removing the worms and adding one to the registered number?---
doPlayerAddItem(cid,3976, amount) --Re-adding the new amount that was registered?---
 
Hopefully this will fufill your needs.

Code:
local grounds = {103, 194} --id of the grounds
local chance = 70 -- that means 30% possible that you get worm

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local rand = math.random(1, 100)
    if(isInArray(grounds, itemEx.itemid)) then
        if (rand >= chance) then
            doPlayerAddItem(cid,3976,1)
        else
            doSendMagicEffect(toPosition, CONST_ME_POFF)
        end
    end
    return true
end
 
@bladeatblackd the doPlayerAddItem(cid,3976,1) line should stack items on top of each other. It adds a worm, and places it where ever makes most sense. If it is not stacking the worms, it could be that your server does not have worms as stackable (if you can manually stack worms, this is not the case, though).

If worms ARE stackable, but doPlayerAddItem() is not stacking them anyways, then look at Snafu's method of removing the items first and then adding one more item than the amount removed.
 
Updated my code. I just messed up and added the +1 on remove rather then the adding part. It takes the worms then re-adds them with 1 more creating a stack effect....

I looked at cykos script and added one more thing alone with the ground ids as this is a essential part.. He also forgot to keep your script for checking for too many worms.

Code:
local ground_to_dig = {1111, 1111} --tiles that can be dug on
local ground_to_dig_uid = 30000  --Only tiles with this unique id will be able to be dug in (set in RME)
local effect = CONST_ME_GROUNDSHAKER

local if_worms_not_stacking_make_true = false --If the worms arnt stacking make this true

function onUse(cid, item, itemEx, fromPosition, toPosition)
   if getPlayerItemCount(cid, 3976) > 15 then
   doPlayerSendCancel(cid, "You don't need that many worms! Its time to go fishing!")
   doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
return false
end

local rand = math.random(1, 100)
if(isInArray(grounds, itemEx.itemid)) then
   if itemEx.uid == ground_to_dig_uid then
     if (rand >= chance) then
       if worms_not_stacking_make_true = false then
         doPlayerAddItem(cid,3976,1)
       else
         local amount = getPlayerItemCount(cid, 3976)
         doPlayerRemoveItem(cid, 3976, amount)
         doPlayerAddItem(cid, 3976, amount + 1)
       end
     else
       doSendMagicEffect(toPosition, CONST_ME_POFF)
     end
   end
end
return true
end
 
Last edited:

Similar threads

Back
Top Bottom