• 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 0.4] [8.60] Check if player is in area

jeffaklumpen

Member
Joined
Jan 20, 2022
Messages
76
Solutions
2
Reaction score
15
GitHub
jeffaklumpen
Hello!

I'm trying to create a boss room where the monsters and boss spawn only when a player enters the area. If there is no players in the area they should despawn. It works perfectly when there's 1 player online. If there's more players online however the script only works if all players are inside the area. If one player is outside it still despawns all the monsters inside.

I have the following script: (It's a global event that checks every 3 seconds)

Lua:
local from, to = {x = 31889, y = 31958, z = 8}, {x = 31991, y = 32019, z = 10}

function onThink(interval, lastExecution)

local tentaclepos1 = {x = 31934, y = 31982, z = 10, stackpos = 253}
local tentaclepos2 = {x = 31930, y = 31986, z = 10, stackpos = 253}
local tentaclepos3 = {x = 31939, y = 31987, z = 10, stackpos = 253}
local tentaclepos4 = {x = 31946, y = 31983, z = 10, stackpos = 253}
local tentaclepos5 = {x = 31935, y = 31990, z = 10, stackpos = 253}
local tentaclepos6 = {x = 31943, y = 31991, z = 10, stackpos = 253}
local tentaclepos7 = {x = 31938, y = 31995, z = 10, stackpos = 253}
local tentaclepos8 = {x = 31933, y = 31998, z = 10, stackpos = 253}
local tentaclepos9 = {x = 31946, y = 31996, z = 10, stackpos = 253}
local yogpos = {x = 31939, y = 31991, z = 10, stackpos = 253}

local tentacle1 = getThingfromPos(tentaclepos1).uid
local tentacle2 = getThingfromPos(tentaclepos2).uid
local tentacle3 = getThingfromPos(tentaclepos3).uid
local tentacle4 = getThingfromPos(tentaclepos4).uid
local tentacle5 = getThingfromPos(tentaclepos5).uid
local tentacle6 = getThingfromPos(tentaclepos6).uid
local tentacle7 = getThingfromPos(tentaclepos7).uid
local tentacle8 = getThingfromPos(tentaclepos8).uid
local tentacle9 = getThingfromPos(tentaclepos9).uid
local yog = getThingfromPos(yogpos).uid
  
    for _, cid in ipairs(getPlayersOnline()) do
        if not isInRange(getThingPosition(cid), from, to) and getGlobalStorageValue(43200) > -1 then
            setGlobalStorageValue(43200, -1)
        end
      
        if not isInRange(getThingPosition(cid), from, to) and isMonster(yog) then
            if isMonster(yog) then
                doRemoveCreature(yog)
            end
            if isMonster(tentacle1) then
                doRemoveCreature(tentacle1)
            end
            if isMonster(tentacle2) then
                doRemoveCreature(tentacle2)
            end
            if isMonster(tentacle3) then
                doRemoveCreature(tentacle3)
            end
            if isMonster(tentacle4) then
                doRemoveCreature(tentacle4)
            end
            if isMonster(tentacle5) then
                doRemoveCreature(tentacle5)
            end
            if isMonster(tentacle6) then
                doRemoveCreature(tentacle6)
            end
            if isMonster(tentacle7) then
                doRemoveCreature(tentacle7)
            end
            if isMonster(tentacle8) then
                doRemoveCreature(tentacle8)
            end
            if isMonster(tentacle9) then
                doRemoveCreature(tentacle9)
            end
        end
    end
  
return TRUE
end
 
Last edited:
Hello!

I'm trying to create a boss room where the monsters and boss spawn only when a player enters the area. If there is no players in the area they should despawn. It works perfectly when there's 1 player online. If there's more players online however the script only works if all players are inside the area. If one player is outside it still despawns all the monsters inside.

I have the following script: (It's a global event that checks every 3 seconds)

Lua:
local from, to = {x = 31889, y = 31958, z = 8}, {x = 31991, y = 32019, z = 10}

function onThink(interval, lastExecution)

local tentaclepos1 = {x = 31934, y = 31982, z = 10, stackpos = 253}
local tentaclepos2 = {x = 31930, y = 31986, z = 10, stackpos = 253}
local tentaclepos3 = {x = 31939, y = 31987, z = 10, stackpos = 253}
local tentaclepos4 = {x = 31946, y = 31983, z = 10, stackpos = 253}
local tentaclepos5 = {x = 31935, y = 31990, z = 10, stackpos = 253}
local tentaclepos6 = {x = 31943, y = 31991, z = 10, stackpos = 253}
local tentaclepos7 = {x = 31938, y = 31995, z = 10, stackpos = 253}
local tentaclepos8 = {x = 31933, y = 31998, z = 10, stackpos = 253}
local tentaclepos9 = {x = 31946, y = 31996, z = 10, stackpos = 253}
local yogpos = {x = 31939, y = 31991, z = 10, stackpos = 253}

local tentacle1 = getThingfromPos(tentaclepos1).uid
local tentacle2 = getThingfromPos(tentaclepos2).uid
local tentacle3 = getThingfromPos(tentaclepos3).uid
local tentacle4 = getThingfromPos(tentaclepos4).uid
local tentacle5 = getThingfromPos(tentaclepos5).uid
local tentacle6 = getThingfromPos(tentaclepos6).uid
local tentacle7 = getThingfromPos(tentaclepos7).uid
local tentacle8 = getThingfromPos(tentaclepos8).uid
local tentacle9 = getThingfromPos(tentaclepos9).uid
local yog = getThingfromPos(yogpos).uid
 
    for _, cid in ipairs(getPlayersOnline()) do
        if not isInRange(getThingPosition(cid), from, to) and getGlobalStorageValue(43200) > -1 then
            setGlobalStorageValue(43200, -1)
        end
    
        if not isInRange(getThingPosition(cid), from, to) and isMonster(yog) then
            if isMonster(yog) then
                doRemoveCreature(yog)
            end
            if isMonster(tentacle1) then
                doRemoveCreature(tentacle1)
            end
            if isMonster(tentacle2) then
                doRemoveCreature(tentacle2)
            end
            if isMonster(tentacle3) then
                doRemoveCreature(tentacle3)
            end
            if isMonster(tentacle4) then
                doRemoveCreature(tentacle4)
            end
            if isMonster(tentacle5) then
                doRemoveCreature(tentacle5)
            end
            if isMonster(tentacle6) then
                doRemoveCreature(tentacle6)
            end
            if isMonster(tentacle7) then
                doRemoveCreature(tentacle7)
            end
            if isMonster(tentacle8) then
                doRemoveCreature(tentacle8)
            end
            if isMonster(tentacle9) then
                doRemoveCreature(tentacle9)
            end
        end
    end
 
return TRUE
end
Tried something else.. I just can't get it to work to check if the area has no players. Now it justs detects the boss as "not isPlayer".

Lua:
local from, to = {x = 31925, y = 31978, z = 10}, {x = 31953, y = 32004, z = 10}

function onThink(interval, lastExecution)

local tentaclepos1 = {x = 31934, y = 31982, z = 10, stackpos = 253}
local tentaclepos2 = {x = 31930, y = 31986, z = 10, stackpos = 253}
local tentaclepos3 = {x = 31939, y = 31987, z = 10, stackpos = 253}
local tentaclepos4 = {x = 31946, y = 31983, z = 10, stackpos = 253}
local tentaclepos5 = {x = 31935, y = 31990, z = 10, stackpos = 253}
local tentaclepos6 = {x = 31943, y = 31991, z = 10, stackpos = 253}
local tentaclepos7 = {x = 31938, y = 31995, z = 10, stackpos = 253}
local tentaclepos8 = {x = 31933, y = 31998, z = 10, stackpos = 253}
local tentaclepos9 = {x = 31946, y = 31996, z = 10, stackpos = 253}
local yogpos = {x = 31939, y = 31991, z = 10, stackpos = 253}

local tentacle1 = getThingfromPos(tentaclepos1).uid
local tentacle2 = getThingfromPos(tentaclepos2).uid
local tentacle3 = getThingfromPos(tentaclepos3).uid
local tentacle4 = getThingfromPos(tentaclepos4).uid
local tentacle5 = getThingfromPos(tentaclepos5).uid
local tentacle6 = getThingfromPos(tentaclepos6).uid
local tentacle7 = getThingfromPos(tentaclepos7).uid
local tentacle8 = getThingfromPos(tentaclepos8).uid
local tentacle9 = getThingfromPos(tentaclepos9).uid
local yog = getThingfromPos(yogpos).uid

        for x = from.x, to.x do
            for y = from.y, to.y do
                for z = from.z, to.z do
                    areapos = {x = x, y = y, z = z, stackpos = 253}
                    getPlayers = getThingfromPos(areapos)
                    if isPlayer(getPlayers.uid) and not isMonster(yog) and getGlobalStorageValue(43200) == -1 then
                        doCreateMonster("Tentacle", tentaclepos1)
                        doCreateMonster("Tentacle", tentaclepos2)
                        doCreateMonster("Tentacle", tentaclepos3)
                        doCreateMonster("Tentacle", tentaclepos4)
                        doCreateMonster("Tentacle", tentaclepos5)
                        doCreateMonster("Tentacle", tentaclepos6)
                        doCreateMonster("Tentacle", tentaclepos7)
                        doCreateMonster("Tentacle", tentaclepos8)
                        doCreateMonster("Tentacle", tentaclepos9)
                        doCreateMonster("Yog-Sothoth (Immune)", yogpos)
                    end
                    if not isPlayer(getPlayers.uid) then --This part needs to check if the area has no players.
                        setGlobalStorageValue(43200, -1)
                    end
                end
            end
        end
   
return TRUE
end
 
try this, idk

Lua:
local from, to = {x = 31889, y = 31958, z = 8}, {x = 31991, y = 32019, z = 10}

local creaturesToSpawn = {
	{position = {x = 31934, y = 31982, z = 10}, creatureName = "Tentacle"},
	{position = {x = 31930, y = 31986, z = 10}, creatureName = "Tentacle"},
	{position = {x = 31939, y = 31987, z = 10}, creatureName = "Tentacle"},
	{position = {x = 31946, y = 31983, z = 10}, creatureName = "Tentacle"},
	{position = {x = 31935, y = 31990, z = 10}, creatureName = "Tentacle"},
	{position = {x = 31943, y = 31991, z = 10}, creatureName = "Tentacle"},
	{position = {x = 31938, y = 31995, z = 10}, creatureName = "Tentacle"},
	{position = {x = 31933, y = 31998, z = 10}, creatureName = "Tentacle"},
	{position = {x = 31946, y = 31996, z = 10}, creatureName = "Tentacle"},
	{position = {x = 31939, y = 31991, z = 10}, creatureName = "Yog-Sothoth (Immune)"}
}

local creatures = {} -- used to hold creature information

function onThink(interval, lastExecution)

	local isPlayerInArea = false
	for _, cid in ipairs(getPlayersOnline()) do
		if isInRange(getThingPosition(cid), from, to) then
			isPlayerInArea = true
			break
		end
	end
	
	if isPlayerInArea then
		if getGlobalStorageValue(43200) ~= -1 then -- event already active previously, and player still inside. Do nothing.
			return true
		end
		-- event not active, spawn creatures
		setGlobalStorageValue(43200, 1)
		for v, k in pairs(creaturesToSpawn) do
			local creature = doCreateMonster(k.creatureName, k.position)
			creatures[#creatures + 1] = {creature} -- adding to table, so we can check against it directly.
		end
	else
		if getGlobalStorageValue(43200) == -1 then
			return true
		end
		setGlobalStorageValue(43200, -1)
		for i = 1, #creatures do
			local creature = creatures[i][1]
			if isMonster(creature) then -- might have to be creature.uid.. I don't remember
				doRemoveCreature(creature)
			end
		end
	end
	
	return true
end
 
Got help from a friend, works like a charm.

Lua:
local from, to = {x = 31925, y = 31978, z = 10}, {x = 31953, y = 32004, z = 10}

function onThink(interval, lastExecution)

local tentaclepos1 = {x = 31934, y = 31982, z = 10, stackpos = 253}
local tentaclepos2 = {x = 31930, y = 31986, z = 10, stackpos = 253}
local tentaclepos3 = {x = 31939, y = 31987, z = 10, stackpos = 253}
local tentaclepos4 = {x = 31946, y = 31983, z = 10, stackpos = 253}
local tentaclepos5 = {x = 31935, y = 31990, z = 10, stackpos = 253}
local tentaclepos6 = {x = 31943, y = 31991, z = 10, stackpos = 253}
local tentaclepos7 = {x = 31938, y = 31995, z = 10, stackpos = 253}
local tentaclepos8 = {x = 31933, y = 31998, z = 10, stackpos = 253}
local tentaclepos9 = {x = 31946, y = 31996, z = 10, stackpos = 253}
local yogpos = {x = 31939, y = 31991, z = 10, stackpos = 253}

local tentacle1 = getThingfromPos(tentaclepos1).uid
local tentacle2 = getThingfromPos(tentaclepos2).uid
local tentacle3 = getThingfromPos(tentaclepos3).uid
local tentacle4 = getThingfromPos(tentaclepos4).uid
local tentacle5 = getThingfromPos(tentaclepos5).uid
local tentacle6 = getThingfromPos(tentaclepos6).uid
local tentacle7 = getThingfromPos(tentaclepos7).uid
local tentacle8 = getThingfromPos(tentaclepos8).uid
local tentacle9 = getThingfromPos(tentaclepos9).uid
local yog = getThingfromPos(yogpos).uid
local playerTable = {}

        -- find all players within an area.
        for x = from.x, to.x do
            for y = from.y, to.y do
                for z = from.z, to.z do
                    -- store the number of creatures on current tile
                    local num_creatures = getTileInfo({x = x, y = y, z = z}).creatures
                    -- keep track of current stackpos
                    local current_stackpos = 1
                    -- get the first 'thing' on given tile and stackpos
                    local thing = getThingfromPos({x = x, y = y, z = z, stackpos = current_stackpos}).uid
                   
                    -- iterate while there's some-'thing'
                    while thing ~= 0 do
                        -- if 'thing' is player, save player to table
                        if isPlayer(thing) then
                            table.insert(playerTable, thing)
                            if num_creatures == #playerTable then
                                break
                            end
                        end
                        -- increment current_stackpos
                        current_stackpos = current_stackpos + 1
                        -- get the next 'thing'
                        thing = getThingfromPos({x = x, y = y, z = z, stackpos = current_stackpos}).uid
                    end
                end
            end
        end
       
        -- playerTable should now only contain players.
        if next(playerTable) == nil and getGlobalStorageValue(39350) > -1 then
            setGlobalStorageValue(39350, -1)
        elseif next(playerTable) == nil and isMonster(yog) then
            if isMonster(yog) then
                doRemoveCreature(yog)
            end
            if isMonster(tentacle1) then
                doRemoveCreature(tentacle1)
            end
            if isMonster(tentacle2) then
                doRemoveCreature(tentacle2)
            end
            if isMonster(tentacle3) then
                doRemoveCreature(tentacle3)
            end
            if isMonster(tentacle4) then
                doRemoveCreature(tentacle4)
            end
            if isMonster(tentacle5) then
                doRemoveCreature(tentacle5)
            end
            if isMonster(tentacle6) then
                doRemoveCreature(tentacle6)
            end
            if isMonster(tentacle7) then
                doRemoveCreature(tentacle7)
            end
            if isMonster(tentacle8) then
                doRemoveCreature(tentacle8)
            end
            if isMonster(tentacle9) then
                doRemoveCreature(tentacle9)
            end
        end
       
        -- If players are inside area
        if next(playerTable) and not isMonster(yog) and getGlobalStorageValue(39350) == -1 then
            doCreateMonster("Tentacle", tentaclepos1)
            doCreateMonster("Tentacle", tentaclepos2)
            doCreateMonster("Tentacle", tentaclepos3)
            doCreateMonster("Tentacle", tentaclepos4)
            doCreateMonster("Tentacle", tentaclepos5)
            doCreateMonster("Tentacle", tentaclepos6)
            doCreateMonster("Tentacle", tentaclepos7)
            doCreateMonster("Tentacle", tentaclepos8)
            doCreateMonster("Tentacle", tentaclepos9)
            doCreateMonster("Yog-Sothoth (Immune)", yogpos)
            doSendMagicEffect(tentaclepos1, CONST_ME_MORTAREA)
            doSendMagicEffect(tentaclepos2, CONST_ME_MORTAREA)
            doSendMagicEffect(tentaclepos3, CONST_ME_MORTAREA)
            doSendMagicEffect(tentaclepos4, CONST_ME_MORTAREA)
            doSendMagicEffect(tentaclepos5, CONST_ME_MORTAREA)
            doSendMagicEffect(tentaclepos6, CONST_ME_MORTAREA)
            doSendMagicEffect(tentaclepos7, CONST_ME_MORTAREA)
            doSendMagicEffect(tentaclepos8, CONST_ME_MORTAREA)
            doSendMagicEffect(tentaclepos9, CONST_ME_MORTAREA)
            doSendMagicEffect(yogpos, CONST_ME_MORTAREA)
        end
   
return TRUE
end
 
It may look ugly, but it works xD

I tried with:


Lua:
for _, cid in ipairs(getPlayersOnline()) do
        if isInRange(getThingPosition(cid), from, to) then
            isPlayerInArea = true
            break
        end
    end

But since it also takes into account the players outside the area when looping through everyone online it messes everything up. Players outside the are turns isPlayerInArea = false even if there are players inside.
 
Back
Top