NPC Buy levels

Eazy M

Developer
Joined
Oct 17, 2010
Messages
901
Best answers
0
Reaction score
57
Location
mah chair
Here is my action script: http://otland.net/f81/buy-levels-176840/

data/npc/Eazy.xml
XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Eazy" nameDescription="Eazy, the npc that sells levels" script="buylevel.lua" walkinterval="2000" floorchange="0" skull="green">
	<health now="100" max="100"/>
	<look type="130" head="39" body="122" legs="125" feet="57" addons="0"/>
	<parameters>
		<parameter key="message_greet" value="Hello |PLAYERNAME|. I am selling levels."/>
		<parameter key="message_decline" value="Is one million gold coins too much for you? Get out of here!"/>
	</parameters>
</npc>
data/npc/scripts/buylevel.lua:
Lua:
-- Created by Eazy M @ otland.net
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

function onCreatureAppear(cid)				npcHandler:onCreatureAppear(cid)			end
function onCreatureDisappear(cid)			npcHandler:onCreatureDisappear(cid)			end
function onCreatureSay(cid, type, msg)			npcHandler:onCreatureSay(cid, type, msg)		end
function onThink()					npcHandler:onThink()					end

function creatureSayCallback(cid, type, msg)
	if(not npcHandler:isFocused(cid)) then
		return false
	end

	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

	local config = {
		addLevels = 2,
		costAmount = 1000000,
		maxLevel = 300,
	}
	
	if(msgcontains(msg, 'buy') or msgcontains(msg, 'level')) then
		selfSay('Do you want to buy '..config.addLevels..' level(s) for '..config.costAmount..' gold?', cid)
		talkState[talkUser] = 1
	elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
		if getPlayerLevel(cid) < config.maxLevel then
			if(doPlayerRemoveMoney(cid, config.costAmount)) then
				doPlayerAddLevel(cid, config.addLevels)
				selfSay('You just bought '.. config.addLevels ..' levels for '.. config.costAmount ..' gold coins.', cid)
			else
				selfSay('Sorry, you don\'t have enough gold.', cid)
			end
		else
			selfSay('Sorry, you are too high level to buy levels.', cid)
		end
		talkState[talkUser] = 0
	elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser])) then
		talkState[talkUser] = 0
		selfSay('Ok then.', cid)
	end

	return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Rep++? :)
 

Evil Puncker

I'd rather kiss a rattlesnake
Joined
May 30, 2009
Messages
5,932
Best answers
34
Reaction score
1,875
Location
160,54,7
try this:
Lua:
-- Created by Eazy M @ otland.net
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

function onCreatureAppear(cid)                npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)            npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)            npcHandler:onCreatureSay(cid, type, msg)        end
function onThink()                    npcHandler:onThink()                    end

local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end
function Player:addLevel(amount)
    return self:addExperience(getExpForLevel(self:getLevel() + (amount and amount or 1)) - self:getExperience())
end

function creatureSayCallback(cid, type, msg)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
    local player = Player(cid)
    if not player then
        return
    end

    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    local config = {
        addLevels = 2,
        costAmount = 1000000,
        maxLevel = 300,
    }
    
    if(msgcontains(msg, 'buy') or msgcontains(msg, 'level')) then
        selfSay('Do you want to buy '..config.addLevels..' level(s) for '..config.costAmount..' gold?', cid)
        talkState[talkUser] = 1
    elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
        if getPlayerLevel(cid) < config.maxLevel then
            if(doPlayerRemoveMoney(cid, config.costAmount)) then
                player:addLevel(config.addLevels)
                selfSay('You just bought '.. config.addLevels ..' levels for '.. config.costAmount ..' gold coins.', cid)
            else
                selfSay('Sorry, you don\'t have enough gold.', cid)
            end
        else
            selfSay('Sorry, you are too high level to buy levels.', cid)
        end
        talkState[talkUser] = 0
    elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser])) then
        talkState[talkUser] = 0
        selfSay('Ok then.', cid)
    end

    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 

adrianootavares

Premium User
Premium User
Joined
Jul 23, 2012
Messages
53
Best answers
0
Reaction score
3
Location
Salvador / Bahia / Brasil
try this:
Lua:
-- Created by Eazy M @ otland.net
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

function onCreatureAppear(cid)                npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)            npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)            npcHandler:onCreatureSay(cid, type, msg)        end
function onThink()                    npcHandler:onThink()                    end

local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end
function Player:addLevel(amount)
    return self:addExperience(getExpForLevel(self:getLevel() + (amount and amount or 1)) - self:getExperience())
end

function creatureSayCallback(cid, type, msg)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
    local player = Player(cid)
    if not player then
        return
    end

    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    local config = {
        addLevels = 2,
        costAmount = 1000000,
        maxLevel = 300,
    }
   
    if(msgcontains(msg, 'buy') or msgcontains(msg, 'level')) then
        selfSay('Do you want to buy '..config.addLevels..' level(s) for '..config.costAmount..' gold?', cid)
        talkState[talkUser] = 1
    elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
        if getPlayerLevel(cid) < config.maxLevel then
            if(doPlayerRemoveMoney(cid, config.costAmount)) then
                player:addLevel(config.addLevels)
                selfSay('You just bought '.. config.addLevels ..' levels for '.. config.costAmount ..' gold coins.', cid)
            else
                selfSay('Sorry, you don\'t have enough gold.', cid)
            end
        else
            selfSay('Sorry, you are too high level to buy levels.', cid)
        end
        talkState[talkUser] = 0
    elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser])) then
        talkState[talkUser] = 0
        selfSay('Ok then.', cid)
    end

    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

I LOVE YOU!
 
Top