• 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 Need Help with Lever Monster Script

xx Kami xx

Retired.
Joined
Dec 29, 2012
Messages
509
Reaction score
20
hello and i need help fixing a small script

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
			doSummonCreature("Ferumbras", {x= 1007, y= 1025, z= 7})
 
	local amount = 1
	for x = fromPos.x, toPos.x do
		for y = fromPos.y, toPos.y do
			for z = fromPos.z, toPos.z do
				local monster = getTopCreature({x=x,y=y,z=z}).uid
				if(isCreature(monster) == TRUE) then
					if getCreatureName(monster) == "Ferumbras" then
						amount = amount+1
					end
				end	
			end
		end
	end
 
	if amount >= 1 then
		doSendMagicEffect(getThingPos(cid), CONST_ME_POFF) 
		doPlayerSendCancel(cid, "You need to kill Ferumbras first before you can summen one again.")
		return true
	else
	end
	      	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have summened Ferumbras.")
		doSendMagicEffect(getThingPos(cid), CONST_ME_MAGIC_BLUE)
	end
end

What the script is suppose to do: Using the lever you spawn 1 ferumbras but cannot use lever again untill the ferumbras is dead.

Repp++ if anyone can fix it
 
Last edited by a moderator:
It already works. just use the one I gave you, don't edit anything.
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
 
local fromPos = {x = toPosition.x - 10, y = toPosition.y - 10, z = toPosition.z} 
local toPos = {x = toPosition.x + 10, y = toPosition.y + 10, z = toPosition.z}
 
	local amount = 0
	for x = fromPos.x, toPos.x do
		for y = fromPos.y, toPos.y do
			for z = fromPos.z, toPos.z do
				local monster = getTopCreature({x=x,y=y,z=z}).uid
				if(isCreature(monster) == TRUE) then
					if getCreatureName(monster) == "Ferumbras" then
						amount = amount+1
					end
				end	
			end
		end
	end
 
	if amount >= 1 then
		doSendMagicEffect(getThingPos(cid), CONST_ME_POFF) 
		doPlayerSendCancel(cid, "You need to kill Ferumbras first before you can summen one again.")
		return true
	else
		doSummonCreature("Ferumbras", {x = toPosition.x - 1, y = toPosition.y + 1, z = toPosition.z})
	      	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have summened Ferumbras.")
		doSendMagicEffect(getThingPos(cid), CONST_ME_MAGIC_BLUE)
	end
end
 
It already works. just use the one I gave you, don't edit anything.
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
 
local fromPos = {x = toPosition.x - 10, y = toPosition.y - 10, z = toPosition.z} 
local toPos = {x = toPosition.x + 10, y = toPosition.y + 10, z = toPosition.z}
 
	local amount = 0
	for x = fromPos.x, toPos.x do
		for y = fromPos.y, toPos.y do
			for z = fromPos.z, toPos.z do
				local monster = getTopCreature({x=x,y=y,z=z}).uid
				if(isCreature(monster) == TRUE) then
					if getCreatureName(monster) == "Ferumbras" then
						amount = amount+1
					end
				end	
			end
		end
	end
 
	if amount >= 1 then
		doSendMagicEffect(getThingPos(cid), CONST_ME_POFF) 
		doPlayerSendCancel(cid, "You need to kill Ferumbras first before you can summen one again.")
		return true
	else
		doSummonCreature("Ferumbras", {x = toPosition.x - 1, y = toPosition.y + 1, z = toPosition.z})
	      	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have summened Ferumbras.")
		doSendMagicEffect(getThingPos(cid), CONST_ME_MAGIC_BLUE)
	end
end

its not what i want but i Rep++ anyways
 
Then explain what you want.

You said you want that with using a lever a Ferumbras will spawn, then you can't spawn it again untill the Ferumbras is dead, that is what this script is doing.

@Your PM,
Here is an example with a location that doesn't have anything to do with the lever position, just edit the positions to where it is.
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
 
local fromPos = {x = 1000, y = 1000, z = 7} -- north west corner of the room
local toPos = {x = 1000, y = 1000, z = 7} -- south east corner of the room
 
	local amount = 0
	for x = fromPos.x, toPos.x do
		for y = fromPos.y, toPos.y do
			for z = fromPos.z, toPos.z do
				local monster = getTopCreature({x=x,y=y,z=z}).uid
				if(isCreature(monster) == TRUE) then
					if getCreatureName(monster) == "Ferumbras" then
						amount = amount+1
					end
				end	
			end
		end
	end
 
	if amount >= 1 then
		doSendMagicEffect(getThingPos(cid), CONST_ME_POFF) 
		doPlayerSendCancel(cid, "You need to kill Ferumbras first before you can summen one again.")
		return true
	else
		doSummonCreature("Ferumbras", {x = 1000, y = 1000, z = 7})
	      	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have summened Ferumbras.")
		doSendMagicEffect(getThingPos(cid), CONST_ME_MAGIC_BLUE)
	end
end
 
Last edited:
I tried to used the last script on OTHire, but you can spawn as many monsters as you want. How to fix that?

Most likely one of the functions return a diffrent value of OTX.
Use ex print(amount) to see where it bugs on a couple of diffrent places and diffrent variables.

The problem is most likely here;
Lua:
local monster = getTopCreature({x=x,y=y,z=z}).uid
if(isCreature(monster) == TRUE) then
    if getCreatureName(monster) == "Ferumbras" then
        amount = amount+1
    end
end
 
print() is a way to print text in the application @WibbenZ is telling you to add print(amount) because amount equals something in your script so if you put print(amount) in your script, you will see the value in the application. Most of scripting is just trying things out and see what happens, you can always undo so don't be afraid to experiment, also there are tons of lua tutorials on youtube, also google is an amazing tool.
 
Thank you. Well I'm not sure if I've done it correctly, but now when I use the item to spawn the monster it says 'nil'.

Nil in Lua is something that dosn't exist, did you print amount then the value amount does not exist (should be a number in your script).
But it also depends on where you place the print.

Ex.
Lua:
print(amount)
local amount = 1
print(amount)

If you execute that code, it will print nil (since amount dosn't exist) and then 1, since amount is now 1 (line 2)
 
Like this?

print(amount)
if amount >= 1 then
print(amount)


I get 0.

You could do that, tbh there is no wrong here, just do it after the variable should be set (after local amount = ..) and that goes for all Lua scripts.

Try this;
Lua:
    local fromPos = {x = 1000, y = 1000, z = 7} -- north west corner of the room
    local toPos = {x = 1000, y = 1000, z = 7} -- south east corner of the room

    local amount = 0
    for x = fromPos.x, toPos.x do
        for y = fromPos.y, toPos.y do
            for z = fromPos.z, toPos.z do
                local monster = getTopCreature({x=x,y=y,z=z}).uid
                if(isCreature(monster)) then
                    print("there is a monster there ...")
                    if getCreatureName(monster) == "Ferumbras" then
                        print("ferumbras is here, amount = ", amount)
                        amount = amount + 1
                        print("amount + 1", amount)
                    end
                end
            end
        end
    end
    print(amount)

If amount is below 0 and there is a monster there, check the positions
 
When I spawn the monster for first time it says

there is a monster there ...
0

Then when I click for a second time it says:

there is a monster there ...
there is a monster there ...
0

then

there is a monster there ...
there is a monster there ...
there is a monster there ...
0

and so on..
 
Go into your lib file and add this line anywhere...

Lua:
ferumbras_spawned = 0

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)

    if isCreature(ferumbras_spawned) then
        doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
        return doPlayerSendCancel(cid, "You need to kill Ferumbras first before you can summen one again.")
    else
        local monster = doSummonCreature("Ferumbras", {x = toPosition.x - 1, y = toPosition.y + 1, z = toPosition.z})
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have summened Ferumbras.")
        doSendMagicEffect(getThingPos(cid), CONST_ME_MAGIC_BLUE)
        ferumbras_spawned = monster
    end
return true
end

Way easier way to do it.
 
Last edited:
Thank you both WibbenZ and Itutorial for the help.

Itutorial's script worked :)

Do you know how to put some kind of time limit so people can't abuse this?

Yes by fixing the script :p
Okay so we now know that we didn't find a monster, then we check the if statment;
Lua:
if getCreatureName(monster) == "Ferumbras" then

This is a simple one, we wanna compare a string to another string.
We know the monster exists, so it has a name but the name we are comparing it to is wrong.

There are a couple of things we can do (and the person who did the script should have done it)

1. We can use the String:lower() function, ex getCreatureName(monster):lower() or string.lower(getCreatureName(monster))
That will convert w/e we write into lower cases, then we change Ferumbras to ferumbras (note F and f)
This is most likely the problem you are having.

2. We can check the monster file to see what the name is (might be FERumbras if someone wrote wrong, or Ferumbras (should be ferumbras IMO)
If thats the problem we can either modify the script or the monster name in it's XML file.
 
Back
Top