• 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!

string.explode, for your talkactions

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
219
Location
Sweden
Hello, I made this function for a friend when he was making a talkaction.
This will ease your job as talkaction scripter.

Place this in your global.lua
PHP:
function string.trim(str)
	-- Function by Colandus
	return (str:gsub("^%s*(.-)%s*$", "%1"))
end

function string.explode(str, sep, limit)
    -- Function by Colandus
    if limit and type(limit) ~= 'number' then
        error("string.explode: limit must be a number", 2)
    end

    if #sep == 0 or #str == 0 then return end
    local pos, i, t = 1, 1, {}
    for s, e in function() return str:find(sep, pos) end do
        table.insert(t, str:sub(pos, s-1):trim())
        pos = e + 1
        i = i + 1
        if limit and i == limit then break end
    end
    table.insert(t, str:sub(pos):trim())
    return t
end

And then, to use it you do:
PHP:
x=string.explode("da, lol, ha", ",")

x[1] will be "da", x[2] will be "lol" and x[3] will be "ha".

A way to check if they have typed in all the required data(in the following format, for example):
"name, posX posY posZ"
PHP:
function tableToPos(t)
	-- Function by Colandus
	if type(t) == "table" and #t == 3 and tonumber(table.concat(t)) then
		return {x=tonumber(t[1]), y=tonumber(t[2]), z=tonumber(t[3])}
	end
	return FALSE
end

function positionExists(pos)
	-- Function by Pedro B. and Colandus
	return getTileThingByPos(pos) > -1
end

function warnPlayer(cid, msg)
	-- Function by Colandus
	doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
	return doPlayerSendCancel(cid, msg)
end

function onSay(cid, words, param)
	-- Example script by Colandus
	local data = string.explode(param, ",")
	if not data then -- if data was nil, then there was no text, lets send a cancel message
		return warnPlayer(cid, "Please enter a name and a position.")
	end
	local name = data[1] -- data[1] should contain the name
	if data[2] then -- data[2] should contain the position (not splitted yet)
		local t = string.explode(data[2], " ") -- now we split the positions
		local pos = tableToPos(t) -- now we make it to a real position (with x, y, z index), inside of the function it check if position is valid...
		local player = getPlayerByName(name) 
		if pos == FALSE then -- not valid, send cancel message.
			return warnPlayer(cid, "Please enter a valid position.")
		elseif not positionExists(pos) then -- lets see if this position is exists on the map, if there is something on that pos...
			return warnPlayer(cid, "Position does not exist!")
		elseif player == FALSE then -- position was valid, but player was not online, send cancel message
			return warnPlayer(cid, "Player is not online.")
		end
		doSendMagicEffect(player, CONST_ME_POFF) -- send a poff effect on the previous pos
		doTeleportThing(player, pos) -- teleport the player to the new location
		doSendMagicEffect(pos, CONST_ME_TELEPORT) -- send a magic effect to the new position.
	else -- data[2] was not found, means that he did only write /teleport "name and nothing more...
		warnPlayer(cid, "Please enter a position.")
	end
	return TRUE
end

That script has several restrictions and does not allow you to do wrong. I can't say it's 100% bug free, but I do think it is!

Enjoy this function, and feel free to use the script (you could strip off the comments if you wish)


Regards,
Colandus
 
Last edited:
Yeah, but as usual, people don't look at useful codes lol... They only want these simple newbie codes that requires no work to add to ur serv >.<
 
It would be cool to have an optional parameter called limit, like in PHP, so for example:
PHP:
s = "Colandus is from Sweden"
exp = string.explode(s," ")

exp[1] = "Colandus"
exp[2] = "is"
exp[3] = "from"
exp[4] = "Sweden"

And if we had limit:
PHP:
s = "Colandus makes my scripts way shorter"
exp = string.explode(s," ",2)

exp[1] = "Colandus"
exp[2] = "makes my scripts way shorter"
 
Last edited:
Oh smart :D I fix later now busy ;)

and oh didn't know php had limit :p will be handy for me too when making websites :)
 
It would be cool to have an optional parameter called limit, like in PHP, so for example:
PHP:
s = "Colandus is from Sweden"
exp = string.explode(s," ")

exp[1] = "Colandus"
exp[2] = "is"
exp[3] = "from"
exp[4] = "Sweden"

And if we had limit:
PHP:
s = "Colandus makes my scripts way shorter"
exp = string.explode(s," ",2)

exp[1] = "Colandus"
exp[2] = "is from Sweden"
haha, setback should be

PHP:
exp[2] = "makes my scripts way shorter"
not
PHP:
exp[2] = "is from Sweden"
ehehe
nice offtop ya? :D
 
Thanks Colandus, good work as usual.
Too bad I can't give you rep :p
 
Back
Top