• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua [TFS 0.4] Effect on bed if player sleep

Blade33711

Member
Joined
Aug 6, 2012
Messages
133
Solutions
1
Reaction score
5
Location
Poland
It's possible to do on each bed if player sleeping this effect?
doSendMagicEffect(pos, CONST_ME_SLEEP)
 
make a global event that checks every 2-3 sec if the item id is the ID of a bed with a player in it and if true then play the animation
 
How can I do it? Idk, really.
It just work with declare position :/

function onThink(cid, interval, lastExecution)
local pos = {x = 461, y = 516, z = 7}
local item = 7793
if getTileItemById(pos, item).uid > 0 then
doSendMagicEffect(pos, CONST_ME_SLEEP)
end
return true
end
 
Last edited:
I'm afraid there's no such function to scan the map for a certain itemid, you can only scan a certain position..
I would recommend that you don't do this in globalevents/scripts..
You should do this in the bed/sleep action script..
You work with addEvent and stopEvent..

I thought of something like the following, you might have to adjust it..
Example:
Assuming that your code bit works with a given position -> add this before your bed's function onUse:
LUA:
function sendBedEffect(position)
    if not position then
        return false
    end
    local item = 7793
    if getTileItemById(position, item).uid > 0 then
        doSendMagicEffect(position, CONST_ME_SLEEP)
        local sleepEvent = addEvent(sendBedEffect, 3000, position)
        -- will queue this function, so it executes again after 3s, this will cause a loop
    end
    return true
end
Then at the onUse script part where your player uses the bed add:
local sleepEvent = addEvent(sendBedEffect, 3000, bedpos)
-- will execute above function after 3000 ms
And at the script part where your player logs in/stops sleeping you put:
stopEvent(sleepEvent)
-- to break the addEvent loop


EDIT:
there might be an issue with the scope of the sleepEvents, so you might have to think through how to stop the event
 
register this code to any bed id that you click on
isPlayer(cid) should solve the issue of stopping the event (if the player is online it will return true, meaning they logged back in and the effect should not execute anymore, otherwise if they are offline they are still sleeping)
LUA:
local events = {}

local function sendBedEffect(cid, pos)
    if isPlayer(cid) then
        stopEvent(events[cid])
        events[cid] = nil
    end
    doSendMagicEffect(pos, CONST_ME_SLEEP)
    events[cid] = addEvent(sendBedEffect, 3000, cid, pos)
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    events[cid] = addEvent(sendBedEffect, 3000, cid, toPosition)
    return true
end
 
@up
When i click on bed I get an error:
Sometimes server crashed.

stack traceback:
[11:3:17.174] [C]: in function 'doSendMagicEffect'
[11:3:17.175] data/actions/scripts/bed.lua:8: in function <data/actions/scripts/bed.lua:3>

Send once the effect and not the player logs out.
 
the stack traceback is not the error, the error should be above it.
if the player doesn't log out you might not be able to do this, since beds are in sources
 
Back
Top