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

[TFS 1.2] Check players in room

Unknown Soldier

Mapping a map
Joined
Oct 30, 2010
Messages
291
Solutions
11
Reaction score
660
Hello,

got a little problem with a movement script. It nicely checks creatures in room, and remove them if player is allowed to enter, however I got a problem making it also check if there is a player in the room. If there is someone then just doesn't allow new player to enter until current player leave (either exit the room or die).

Thanks in advance!
Lua:
local config = {
    centre_of_arena = {x = 1500, y = 1617, z = 10},
    x_range = 10,
    y_range = 10,
    player_teleport_position = {x = 1500, y = 1616, z = 10},
    --player_cancel_position = {x = 1500, y = 1618, z = 11}
}

function onStepIn(cid, item, position, fromPosition)
    if not isPlayer(cid) then
        return true
    end
        local queststatus = getPlayerStorageValue(cid,60003)
        if queststatus == 2 then
                local count, creatures = 0, getSpectators(config.centre_of_arena, config.x_range, config.y_range, false)
                if creatures ~= nil then
                    for _, pid in ipairs(creatures) do
                        if isPlayer(pid) then
                            count = count + 1
                        end
                    end
                end           
                if count == 0 then
                    if creatures ~= nil then
                        for _, pid in ipairs(creatures) do
                            doRemoveCreature(pid)
                        end
                    end
                    Game.createMonster("Rotworm Queen", {x = 1500 + math.random(-2,2), y = 1612 + math.random(-2,2), z = 10}, false, true)
                end
                doTeleportThing(cid, config.player_teleport_position)
                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'Kill Rotworm Queen!')
        else
            doTeleportThing(cid, fromPosition, true)
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR,"You are not allowed to go there.")
        end
    return true
end
 
Solution
first: lua sees nil as false so you don't need to compare creatures to nil. Just type "if creatures then"

okay, now regarding your problem:
move lines 31 and 32 above that "end"

Now replace that end with
Code:
else
            doTeleportThing(cid, fromPosition, true)
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR,"Somebody is inside, please wait for your turn.")
end
first: lua sees nil as false so you don't need to compare creatures to nil. Just type "if creatures then"

okay, now regarding your problem:
move lines 31 and 32 above that "end"

Now replace that end with
Code:
else
            doTeleportThing(cid, fromPosition, true)
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR,"Somebody is inside, please wait for your turn.")
end
 
Solution
also I recommend teleporting people to cancel position(the line you commented) instead of fromPosition because if someone logout near the teleport, some people/monsters take all tiles around and he login again, fromPos will be same as tp pos which will crash the server with call stack overflow error (infinite loop of stepIn and teleporting to same pos)
 
Back
Top