TorNix~|nR Posted February 4, 2021 Share Posted February 4, 2021 (edited) Hello guys I have a little problem with this code, when a player enter the jail it start counting the timer without a problem but when another player enter the jail too (means they become 2 in jail), the text timer stop counting for the first player (it freezes for example in Time Left: 0:40 seconds) and start counting for only the second player Code function startJailTimer(source,jailtimex) -- to start mission timer .. if source then TimerDisplay = textCreateDisplay() m,s,cs = msToTimeStr(jailtimex) fullTime = m..":"..s TimerText = textCreateTextItem ( "Time Left: "..tostring(jailTime).."", 0.39, 0.7 ,"medium",255,255,255,255,2.0,"left","center",255) textDisplayAddText ( TimerDisplay, TimerText ) textDisplayAddObserver ( TimerDisplay, source ) sortTimer:~(source,TimerText,jailtimex) end end function msToTimeStr(ms) if not ms then return '' end if ms < 0 then return "0","00","00" end local centiseconds = tostring(math.floor(math.fmod(ms, 1000)/10)) if #centiseconds == 1 then centiseconds = '0' .. centiseconds end local s = math.floor(ms / 1000) local seconds = tostring(math.fmod(s, 60)) if #seconds == 1 then seconds = '0' .. seconds end local minutes = tostring(math.floor(s / 60)) return minutes, seconds, centiseconds end function sortTimer:~(source,timer,time) -- to sort timer's :~ .. if timer and time then if isTimer(timer:~Timer) then killTimer(timer:~Timer) end timer:~Timer = setTimer(function(source) time = time - 60 m,s,cs = msToTimeStr(time) fullTime = m..":"..s textItemSetText(timer,""..tostring(fullTime).."") end , 50 , 0 ,source ) end end function stopJailTimer(source) textDestroyDisplay(TimerDisplay) if TimerText then textDestroyTextItem(TimerText) end if isTimer(timer:~Timer) then killTimer(timer:~Timer) end end any help please? Edited February 4, 2021 by TorNix~|nR Link to comment
Hoffmann Posted February 5, 2021 Share Posted February 5, 2021 Could you show the part of code where startJailTimer is called? Link to comment
TorNix~|nR Posted February 5, 2021 Author Share Posted February 5, 2021 I use these in the events time = math.floor(jailTime) startJailTimer(source,time) Link to comment
Tekken Posted February 6, 2021 Share Posted February 6, 2021 :~ what is this? some swer word? I see why, you should use tables to store timers like this: local allJailTimers = {}; function jailPlayer(player, time) if not isTimer(allJailTimers[player]) then allJailTimers[player] = setTimer(jailHim, time, 1, player); else outputChatBox("Already in jail"); end end This is as simple as it can be, if you need further help just ask ! Link to comment
TorNix~|nR Posted February 6, 2021 Author Share Posted February 6, 2021 Sorry, that :~ I think is caused by changing the LUA to HTML I think, it's not like that in my code You think I can use this? local allJailTimers = {}; function jailPlayer(plr,timer,time) if not isTimer(allJailTimers[player]) then if plr and timer and time then if isTimer(timer:~Timer) then killTimer(timer:~Timer) end timer:~Timer = setTimer(function(plr) time = time - 60 m,s,cs = msToTimeStr(time) fullTime = m.." M "..s.." S" textItemSetText(timer,""..tostring(fullTime).."") end , 50 , 0 ,plr ) end end end Link to comment
Tekken Posted February 6, 2021 Share Posted February 6, 2021 (edited) First of all I highly encourage you to carefully read this tutorial for better understanding Lua tables Second of all local allJailTimers = {}; function jailPlayer(plr, timer, time) if plr and timer and time then if isTimer(allJailTimers[plr]) then killTimer(allJailTimers[plr]); end allJailTimers[plr] = setTimer(function(plr) time = time - 60; m,s,cs = msToTimeStr(time); fullTime = m.." M "..s.." S"; textItemSetText(timer, ""..tostring(fullTime)); end, 50, 0, plr); end end -- 'allJailTimers' this is a table; In Lua a table is generally used to store data; -- You need a way of storing UNIC timers so we can make use of Lua tables like this in order to make unic tables local table = {}; -- an empty table. -- In order for this table to be unic for eatch player we add a SUB TABLE like this table[player] = {}; --When adding subtables we first need to be sure our table was created *see above; -- note that we added the player with [] square brackets, so this will make something like this; -- note2 when defining a subtable we never call it local local table = { -- the table [player] = { -- this will be a subtable }, [player_2] = { -- this will be another subtable for another player }, --etc }; I have tried, you may not understand so I highly recommend you read the tutorial above! Edited February 6, 2021 by Tekken typo Link to comment
TorNix~|nR Posted February 7, 2021 Author Share Posted February 7, 2021 (edited) Yeah it's working @Tekken the working code: local timerTimer = {}; function sortTimer(plr,timer,time) if plr and timer and time then if isTimer(timerTimer[plr]) then killTimer(timerTimer[plr]); end timerTimer[plr] = setTimer(function(plr) time = time - 60; m,s,cs = msToTimeStr(time); fullTime = m.." M "..s.." S"; textItemSetText(timer,""..tostring(fullTime)..""); if ( tonumber(m) <= 0 and tonumber(s) <= 0 and tonumber(cs) <= 0 ) then stopJailTimer(plr, timer) end end , 50 , 0 ,plr ); end end function stopJailTimer(plr) textDestroyDisplay(TimerDisplay) ------HERE IS THE PROBLEM if TimerText then textDestroyTextItem(TimerText) ------HERE IS THE PROBLEM end if isTimer(timerTimer[plr]) then killTimer(timerTimer[plr]) end end but a little more problem when 2 players are in jail and the timer is counting, when they get out, only the last player get the destroy of the text, but the other it doesn't disappear here's the code: function startJailTimer(plr,jailTime) if plr then TimerDisplay = textCreateDisplay() m,s,cs = msToTimeStr(jailTime) fullTime = m..":"..s TimerText = textCreateTextItem ( ""..tostring(jailTime).."", 0.39, 0.7 ,"medium",255,255,255,255,2.0,"left","center",255) textDisplayAddText ( TimerDisplay, TimerText ) textDisplayAddObserver ( TimerDisplay, plr ) sortTimer(plr,TimerText,jailTime) end end help please? Edited February 7, 2021 by TorNix~|nR Link to comment
Tekken Posted February 7, 2021 Share Posted February 7, 2021 You have to apply the same method I’ve showed you, with tables. All I can do It’s to encourage you to learn how tables work! Just follow the example I gave you. Good luck! 1 Link to comment
Tekken Posted February 7, 2021 Share Posted February 7, 2021 TimerText[plr] = THE FUNCTION HERE --etc 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now