• Best way to sync timer.TimeLeft from server to client?
    3 replies, posted
So I have a weapon that sets doors to be locked and unlocks them after a specific period of time, all handled on the server. But I also want a quick way to grab that number for my DrawHUD function when you look at the door and I didn't know of any other way to effectively send that information to the client. So my solution was to create 2 timers, one clientside and one serverside both at the same time and just reference the clientside timer when I need it. My question is, am I doing this in the best way or is there another method for this timer? [code] if SERVER then door:EmitSound( "doors/door_metal_medium_close1.wav" ) door:Fire("lock", "", 0) CustomMsg(self.Owner, "Door locked!", Color(0,255,0)) timer.Create(door:EntIndex() .. "DoorLockedTime", self.LockTime, 1, function() door:Fire( "unlock", "", 0 ) door:EmitSound( "doors/door1_move.wav" ) CustomMsg(door:GetNWString("DoorOwner"), "One of your doors has unlocked due to time!", Color(255,255,0)) timer.Destroy(door:EntIndex() .. "DoorLockedTime") end) end if CLIENT then timer.Create(door:EntIndex() .. "ClientLockTime", self.LockTime, 1, function() timer.Destroy(door:EntIndex() .. "ClientLockTime") end) end [/code]
Don't sync the timeleft, sync the time when the door is going to be unlocked. In the server part you can just use: [CODE] door:SetNetworkedInt("lockeduntil", CurTime()+self.LockTime) [/CODE] Then in your draw hook you can use [CODE] local timeleft = door:GetNetworkedInt("lockeduntil", 0)-CurTime() [/CODE] to get the time left on the door. If it is negative that means that it either doesnt have a timer or it elapsed. You can also use SetNetworkedFloat instead, if you want to know milliseconds as well.
I cant believe I didnt think of using networked values. Whats the purpose of using current time though? Wouldn't it be simpler just to send the integer or is there a reason for that
The only thing that really needs to change is to use a Float instead of an Int. The reason you'd use a target time is so that you only need to set the time once - the client can solve the difference between the current time and when the timer is up. Meaning, time left is as simple as the example given instead of constantly having to update a value.
Sorry, you need to Log In to post a reply to this thread.