Avagard Posted May 6, 2017 Share Posted May 6, 2017 (edited) hello i have time as data, like: setElementData(source,"time",129) -- 129 are 2:15 mins i want to turn them into minutes at the score(a script up in the screen it shows time left) how can i do that? you shouldn't worry of i do it i just want to convert it. Edited May 6, 2017 by Avagard Link to comment
#BrosS Posted May 6, 2017 Share Posted May 6, 2017 Try that , i'm not sure because i'm in phone right now local seconds = 129 local min = math.floor ( ( seconds % 3600 ) /60 ) local sec = ( seconds %60 /60*100 ) setElementData(source,"time",""..min.." : "..sec.."") if you wanna test it try that seconds = 129 addCommandHandler('1', function() local min = math.floor ( ( seconds % 3600 ) /60 ) local sec = ( seconds %60 /60*100 ) outputChatBox(""..min.." : "..sec.."") end) Link to comment
Avagard Posted May 7, 2017 Author Share Posted May 7, 2017 error attempt to perform arithmetic on a string value my code: local seconds = 129 local min = math.floor ( ( seconds % 3600 ) /60 ) local sec = ( seconds %60 /60*100 ) RoundTime1 = createObject ( 964, 174.30078125, -58.458984375 +0, 1.578125 -1, 0, 0, 0 ) setElementData(RoundTime1,"RoundTime1",""..min.." : "..sec.."") setTimer ( function() if getElementData(RoundTime1,"RoundTime1") == 0 then setElementData(RoundTime1,"RoundTime1",""..min.." : "..sec.."") else setElementData (RoundTime1,"RoundTime1",getElementData (RoundTime1,"RoundTime1") - 1) local timeObject = getElementData (RoundTime1,"RoundTime1") triggerClientEvent ( "getTimeBRO1",resourceRoot,timeObject) end end, 1000, 0 ) client isnt important Link to comment
botder Posted May 7, 2017 Share Posted May 7, 2017 setElementData (RoundTime1,"RoundTime1",getElementData (RoundTime1,"RoundTime1") - 1) You can't do minus one on a string, thus you got attempt to perform arithmetic on a string value. Link to comment
Forum Administrators Lpsd Posted May 7, 2017 Forum Administrators Share Posted May 7, 2017 (edited) RoundTime1 is a string value. On line 8 in your example you're trying to compare a string with 0. You could use something like this to split the RoundTime1 string by a certain character, in this case a colon. function split_string(inputstr, sep) if sep == nil then sep = "%s" end local t={} ; i=1 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do t[i] = str i = i + 1 end return t end However, given the current logic of your script I can see a few problems with doing this. As I understand you're trying to make a timer which goes from 129 seconds, counts down to 0, then resets back to 129 and restarts. Something like this would be better. local timer_start = 129 --timer default starting value in seconds local min_tick = 0 function split_string(inputstr, sep) if sep == nil then sep = "%s" end local t={} ; i=1 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do t[i] = str i = i + 1 end return t end RoundTime1 = createObject ( 964, 174.30078125, -58.458984375 +0, 1.578125 -1, 0, 0, 0 ) setTimer ( function() local time = split_string(getElementData (RoundTime1,"RoundTime1"), ":") if tonumber(time[2]) == 0 then --if seconds 0 or not created local min = math.floor ( ( timer_start % 3600 ) /60 ) local sec = ( timer_start %60 /60*100 ) setElementData(RoundTime1,"RoundTime1",min..":"..sec) else min_tick = min_tick + 1 local min = tonumber(time[1]) local sec = tonumber(time[2]) - 1 --take a second off if min_tick == 60 then --if 60 seconds past, take 1 from minute min_tick = 0 min = min - 1 end setElementData (RoundTime1,"RoundTime1",min..":"..sec) local timeObject = getElementData (RoundTime1,"RoundTime1") triggerClientEvent ( "getTimeBRO1",resourceRoot,timeObject) end end, 1000, 0 ) It's not pretty, but it should do the job. You should think about handling this on the clientside rather than using a triggerClientEvent every second. You could sync the timers onPlayerJoin for each client Edited May 7, 2017 by LopSided_ Link to comment
Avagard Posted May 7, 2017 Author Share Posted May 7, 2017 (edited) 6 minutes ago, LopSided_ said: It's not pretty, but it should do the job. You should think about handling this on the clientside rather than using a triggerClientEvent every second. You could sync the timers onPlayerJoin for each client i want every one in the server to have the exact same time so that is why i put it in serverside thank you very much btw how do i reset it(i want it to go back to 2 mins when all players dies)? Edited May 7, 2017 by Avagard Link to comment
Forum Administrators Lpsd Posted May 7, 2017 Forum Administrators Share Posted May 7, 2017 (edited) local timer_start = 129 --timer default starting value in seconds function split_string(inputstr, sep) if not inputstr then return false end if sep == nil then sep = "%s" end local t={} ; i=1 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do t[i] = str i = i + 1 end return t end function resetTime() local min = math.floor ( ( timer_start % 3600 ) /60 ) local sec = ( timer_start %60 /60*100 ) - (min * 60) setElementData(RoundTime1,"RoundTime1",min..":"..sec) end addCommandHandler("timereset", resetTime) RoundTime1 = createObject ( 964, 174.30078125, -58.458984375 +0, 1.578125 -1, 0, 0, 0 ) setTimer ( function() local time = split_string(getElementData (RoundTime1,"RoundTime1"), ":") if not time then --if seconds 0 or not created local min = math.floor ( ( timer_start % 3600 ) /60 ) local sec = ( timer_start %60 /60*100 ) - (min * 60) setElementData(RoundTime1,"RoundTime1",min..":"..sec) else local min = tonumber(time[1]) local sec = tonumber(time[2]) - 1 --take a second off if sec == -1 then sec = 59 end if sec == 0 then --if 60 seconds past, take 1 from minute min = min - 1 end setElementData (RoundTime1,"RoundTime1",min..":"..sec) local timeObject = getElementData (RoundTime1,"RoundTime1") triggerClientEvent ( "getTimeBRO1",resourceRoot,timeObject) end end, 1000, 0 ) I added a function for you, resetTime. Maybe you can figure out how to use this function once all players have died. I also cleaned up the logic of the timers too. Good luck! Edited May 7, 2017 by LopSided_ Link to comment
Avagard Posted May 7, 2017 Author Share Posted May 7, 2017 (edited) there is a problem in the scoreboard the time is 2:-129 idk why also it goes up like 2:-130 , 2:-131 Edited May 7, 2017 by Avagard Link to comment
Forum Administrators Lpsd Posted May 7, 2017 Forum Administrators Share Posted May 7, 2017 (edited) local timer_start = 129 --timer default starting value in seconds function split_string(inputstr, sep) if not inputstr then return false end if sep == nil then sep = "%s" end local t={} ; i=1 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do t[i] = str i = i + 1 end return t end function resetTime() local min = math.floor( timer_start / 60 ) local sec = timer_start - (min * 60) setElementData(RoundTime1,"RoundTime1",min..":"..sec) end addCommandHandler("timereset", resetTime) RoundTime1 = createObject ( 964, 174.30078125, -58.458984375 +0, 1.578125 -1, 0, 0, 0 ) setTimer ( function() local time = split_string(getElementData (RoundTime1,"RoundTime1"), ":") if not time then --if seconds 0 or not created local min = math.floor( timer_start / 60 ) local sec = timer_start - (min * 60) setElementData(RoundTime1,"RoundTime1",min..":"..sec) else local min = tonumber(time[1]) local sec = tonumber(time[2]) - 1 --take a second off if sec == -1 then sec = 59 end if sec == 0 then --if 60 seconds past, take 1 from minute min = min - 1 end setElementData (RoundTime1,"RoundTime1",min..":"..sec) local timeObject = getElementData (RoundTime1,"RoundTime1") triggerClientEvent ( "getTimeBRO1",resourceRoot,timeObject) end end, 1000, 0 ) Try this, I noticed a problem with the original mins/secs maths which I copied from BrosS Edited May 7, 2017 by LopSided_ 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