YouTube2Text

How to use State Bags (FiveM Lua Scripting Master Series) — Transcript

by CharlesHacks · 2,553 words · 402 segments · language en · Watch on YouTube

Full transcript

  1. 0:05[Music]
  2. 0:12all right hello everyone and welcome to
  3. 0:15the first video in my new series that
  4. 0:17we're calling the master series
  5. 0:19and in the master series we're looking
  6. 0:21at some more advanced features and
  7. 0:23functionality of 5m things you might not
  8. 0:24be aware of or things you might not know
  9. 0:26how to use well and we're going to try
  10. 0:28to do a little bit of a deep dive into
  11. 0:30these topics and see what we can learn
  12. 0:32about them and find out how you can use
  13. 0:34them in your projects your resources
  14. 0:36your servers and for this very first
  15. 0:38master series episode we are going to be
  16. 0:40looking at state bags which are an
  17. 0:42amazing feature of one sync if you're
  18. 0:45coming over from the zero to hero series
  19. 0:47today we talked about sharing data
  20. 0:48between client and server communicating
  21. 0:50using events and commands and in this
  22. 0:53master series we're going to look at
  23. 0:54another way of sharing information
  24. 0:56between client server and that's with
  25. 0:58state bags
  26. 0:59so let's talk a little bit about what
  27. 1:01state bags are and you can think of
  28. 1:03state bags as a table of information
  29. 1:06that can be attached to any networked
  30. 1:08entity
  31. 1:10so we can have a state bag attached to a
  32. 1:12ped uh or we could have it attached to a
  33. 1:14vehicle or maybe a network networked
  34. 1:17object we've created
  35. 1:18and this state can be accessed by the
  36. 1:21server and accessed by any other
  37. 1:24player client who is in scope of that
  38. 1:26entity
  39. 1:27there's also state bag change handlers
  40. 1:29which behave a little bit like events
  41. 1:30where the function will be called when
  42. 1:32the state bag on something that we're
  43. 1:34listening for changes
  44. 1:35now there's also a global state bag
  45. 1:37which is uh basically one big state bag
  46. 1:40that is shared between client server
  47. 1:42it's not tied to any specific entity it
  48. 1:44just kind of exists and all of the
  49. 1:46clients can access it now one thing
  50. 1:48that's important to know is that in its
  51. 1:49default form
  52. 1:51without any other modifications state
  53. 1:54back global state can only be modified
  54. 1:56by the server so all of the clients can
  55. 1:58access it but only the server can change
  56. 2:01it so it's authoritative in that way
  57. 2:03whereas on the client side clients can
  58. 2:05change the state of entities that they
  59. 2:08own
  60. 2:08so let's look at how this works and what
  61. 2:11we're going to start out with first is
  62. 2:12looking at global state all right so
  63. 2:15this is going to be a very simple
  64. 2:16example we're just going to keep a
  65. 2:17counter of a number
  66. 2:19that we can increment on the server and
  67. 2:21we'll see how that's reflected on the
  68. 2:22client so i'm just going to go in here
  69. 2:25and
  70. 2:26set up just a little thread here where
  71. 2:28we will say global state which is how we
  72. 2:30access a global the global state bag and
  73. 2:32then we'll just say counter is equal to
  74. 2:34zero and then let's also register a
  75. 2:37command
  76. 2:39and this command will only be available
  77. 2:40server side we're gonna be calling this
  78. 2:41from the server side and uh when we call
  79. 2:44the increment command we're just gonna
  80. 2:45say globalstate.counter is equal to
  81. 2:47global
  82. 2:50state dot counter plus one all right and
  83. 2:53then on the client let's just create a
  84. 2:55thread here
  85. 2:58and then we'll say well true
  86. 3:01and we'll run this every half second and
  87. 3:03we will just print
  88. 3:05the global state dot counter out now
  89. 3:07i've got two clients here one of them
  90. 3:09seems to be a little stuck up there it
  91. 3:11goes which is something we're going to
  92. 3:12be looking at later but if we look up at
  93. 3:14the top left 5m client we can see that
  94. 3:17we're printing out zero because we
  95. 3:18haven't incremented at all
  96. 3:20now let me just bring this over here and
  97. 3:22then if i run the increment command
  98. 3:25uh on the server
  99. 3:27you'll see that we're now reflecting
  100. 3:28that on the client with one
  101. 3:31two and three
  102. 3:34so you'll note that we're not sending
  103. 3:35any events back and forth all the server
  104. 3:37had to do
  105. 3:38was change this global state
  106. 3:40and all of the clients immediately have
  107. 3:42access to that now let's look at another
  108. 3:44potential way we can do this i'm just
  109. 3:46going to stop state bags so we're not
  110. 3:48getting spammed while we look at this
  111. 3:50now instead of running this in a thread
  112. 3:52i mentioned that you can also use change
  113. 3:53handlers to hook into these so let's
  114. 3:55take a look at that
  115. 3:57and so we can see add state back change
  116. 3:59handler key filter bag filter and then
  117. 4:02the handler and the handler has a couple
  118. 4:03parameters on so let me just split these
  119. 4:05windows really quick make this a little
  120. 4:06bit bigger so you can see it
  121. 4:08and let's look at implementing this so
  122. 4:10add state bag change handler
  123. 4:14and our key filter is going to be
  124. 4:17counter because that's the key that
  125. 4:18we're looking for and the bag filter is
  126. 4:20going to be global
  127. 4:23at least i'm pretty sure it is we're
  128. 4:24going to find out and then our handler
  129. 4:26is going to have bag name
  130. 4:29e value reserved and replicated which uh
  131. 4:34some of these aren't going to matter but
  132. 4:36let's just go ahead and print all of
  133. 4:37them out so now instead of running a
  134. 4:38thread to keep an eye on that global
  135. 4:40state we can just react when it changes
  136. 4:44so we can see uh we have that initial
  137. 4:47one uh which our value is going to be
  138. 4:49our third parameter there and let me
  139. 4:51just do increment
  140. 4:53perfect
  141. 4:54and so that's a little bit about how
  142. 4:55state bag change handlers work now so
  143. 4:58far we've only been looking at global
  144. 5:00state bags so now let's look at some
  145. 5:02uh other state bags and the one we're
  146. 5:04going to look at is player state
  147. 5:07so let's talk about how we
  148. 5:10set player state so when we are on the
  149. 5:13client the way we're going to set player
  150. 5:14state is we're just going to say
  151. 5:15localplayer.state
  152. 5:18dot you know foo equals bar
  153. 5:21now this is one way of doing it i like
  154. 5:23to use the set function
  155. 5:27because it's a little bit more clear to
  156. 5:28me what's happening here and we can also
  157. 5:29control if it's replicated or not so we
  158. 5:32could say foo is equal to bar and true
  159. 5:34because we want to replicate that to the
  160. 5:36server
  161. 5:37so this is how we can set state on
  162. 5:39ourselves on the client
  163. 5:41now if we're on the server
  164. 5:44and we want to change the state of a
  165. 5:45player let's say our player's server id
  166. 5:49is one
  167. 5:50we're going gonna say player and then
  168. 5:52player id dot state
  169. 5:55and then same thing we can either use
  170. 5:56set
  171. 5:59uh and then we can access it like
  172. 6:04that
  173. 6:05so let's try that and what we're going
  174. 6:07to do is we're just going to register
  175. 6:10a command called client increment on the
  176. 6:14client
  177. 6:18so what we're going to do we're just
  178. 6:19going to grab the old counter or zero
  179. 6:22and then oops
  180. 6:24and then set state to
  181. 6:26the old number plus one
  182. 6:29and then on the server why don't we
  183. 6:32why don't we just do
  184. 6:34this as an example so
  185. 6:37we're gonna get into our while loop
  186. 6:40and once again we'll just wait half a
  187. 6:42second and we're gonna do
  188. 6:45so we're just gonna get all players
  189. 6:48and we are just going to print their
  190. 6:50server id and then player
  191. 6:53server id.state.counter
  192. 6:57all right let's restart
  193. 7:00our state bag script and you'll see that
  194. 7:04one and two are both showing up here and
  195. 7:06it's nil because we haven't ran the cl
  196. 7:08inc command yet so let's do that on
  197. 7:11client one
  198. 7:13do cl inc
  199. 7:14perfect
  200. 7:15and now we can see that well one is
  201. 7:17equal to two
  202. 7:19now three
  203. 7:20and four
  204. 7:21and if we jump over to our other client
  205. 7:23and do cl inc here we should see that
  206. 7:27reflected on client two perfect
  207. 7:31all right so now we've learned how to
  208. 7:33use player state to set it on the client
  209. 7:36and reflect that on the server and once
  210. 7:38again you could you could set it on the
  211. 7:40server and reflect it on the client
  212. 7:43that's a little bit trickier for the
  213. 7:46the state back change handler if you
  214. 7:47wanted to use that
  215. 7:49effectively what you're going to want to
  216. 7:51do
  217. 7:52is
  218. 7:53your
  219. 7:55bag filter is going to be player colon
  220. 7:58and then the player server id so
  221. 8:00um not the cleanest format but it does
  222. 8:03work and you can kind of listen for
  223. 8:05state back changes on yourself that way
  224. 8:07so now let's uh
  225. 8:09let's dive in and look at setting and
  226. 8:13managing state on entities that aren't
  227. 8:15players
  228. 8:16so i'm just going to
  229. 8:18nuke all of my code here
  230. 8:22oops
  231. 8:23and restart the resource just to get it
  232. 8:24to stop printing and let's think about
  233. 8:26what we want to accomplish here so let's
  234. 8:28say we want to set
  235. 8:30uh a state bag on a vehicle that tells
  236. 8:33you who the last driver of the vehicle
  237. 8:35was so i'm going to use a couple things
  238. 8:37here i'm going to use base events to
  239. 8:39determine when a player enters a vehicle
  240. 8:43and if we jump over to the docks we
  241. 8:45should let me just kind of organize
  242. 8:47things here
  243. 8:49let's see stock resources base events
  244. 8:52and entered vehicle okay perfect so
  245. 8:57let's build this out really quick
  246. 9:02so we should get this event and let me
  247. 9:04just turn on word wrap so we can see
  248. 9:06what we're doing okay so we should on
  249. 9:08the server this event should be called
  250. 9:10when a player enters a vehicle and we're
  251. 9:12just going to say if c index is not
  252. 9:15equal to negative 1 then return
  253. 9:18otherwise
  254. 9:21what we want to do is set a state bag on
  255. 9:23the vehicle so for vehicles instead of
  256. 9:25using player we use entity and one thing
  257. 9:28that's important to note is you need to
  258. 9:29use the entity handle not the entity's
  259. 9:32network id here
  260. 9:34so in this case the uh current vehicle
  261. 9:37is likely going to be the server id i
  262. 9:41believe
  263. 9:42you know what after a uh a quick look at
  264. 9:46the base events resource there's
  265. 9:47actually an undocumented
  266. 9:49parameter here
  267. 9:53vehicle net id
  268. 9:55which is going to be the fourth one so
  269. 9:56i'm just going to say
  270. 9:59we're going to assign a variable vehicle
  271. 10:01entity and we're just going to say
  272. 10:02get entity
  273. 10:06id from network id i'm pretty sure
  274. 10:08that's correct let me double check
  275. 10:10network get entity from network id there
  276. 10:12we go and we're going to provide the
  277. 10:14vehicle net id
  278. 10:17all right
  279. 10:19and then we're going to say entity
  280. 10:21vehicle entity dot state
  281. 10:24dot last driver
  282. 10:26or actually let's use sat set
  283. 10:30last driver is equal to
  284. 10:33um the id
  285. 10:37person who triggered this event which is
  286. 10:38going to be sourced there so we'll say
  287. 10:40that
  288. 10:41and then player id
  289. 10:43all right so that should
  290. 10:45assuming we've done everything right
  291. 10:46here this should set a state bag called
  292. 10:48last driver on the vehicle itself with
  293. 10:51the id
  294. 10:52of the player who last got in the
  295. 10:54driver's seat and now we just need to
  296. 10:57implement a way to check that on the
  297. 10:58client side so down here on the client
  298. 11:00i'm just going to register a command
  299. 11:02called get last driver
  300. 11:05and now i also have a raycasting
  301. 11:08resource
  302. 11:09utility resource that i'm going to be
  303. 11:10using for this
  304. 11:11and so uh all i need to do is say entity
  305. 11:14id is equal to export
  306. 11:17stock raycast dot get hit entity and
  307. 11:21then uh we can just print uh
  308. 11:25entity entity id dot
  309. 11:30state dot last driver
  310. 11:32and
  311. 11:33we're kind of winging that
  312. 11:35everything right
  313. 11:37i'm gonna start base events i'm gonna
  314. 11:39start raycast and i'm going to restart
  315. 11:43state bags which
  316. 11:45okay
  317. 11:45perfect
  318. 11:46so let's see if this worked out for us
  319. 11:49um
  320. 11:50so you can see my my raycast utility
  321. 11:53here is just going to draw to entities
  322. 11:55and highlight them
  323. 11:57and then i can get whatever i'm looking
  324. 11:59at so
  325. 12:00if we get into our vehicle
  326. 12:03and then get out of it and then we get
  327. 12:06the state bag for it you can see that it
  328. 12:08is two now what's great here is it's not
  329. 12:11just that client uh any other client so
  330. 12:13we're gonna take this guy over here and
  331. 12:15we're gonna have him come take a look at
  332. 12:17it
  333. 12:18and they can also access the state bag
  334. 12:20that says it's two and now if we were to
  335. 12:23get in it
  336. 12:24and then use our second client to try to
  337. 12:26get the last driver again you can see
  338. 12:28it's been changed to one so state bags
  339. 12:30are incredibly powerful for keeping
  340. 12:32information about an entity around some
  341. 12:34good examples of things i've used state
  342. 12:36bags for in the past include if a player
  343. 12:38is handcuffed or not or if they have any
  344. 12:41injuries
  345. 12:42as well as for vehicles obviously fuel
  346. 12:45level
  347. 12:46for peds also if their seat belts on so
  348. 12:48there's just an endless amount of things
  349. 12:50that you can use state bags for and
  350. 12:52sharing information information between
  351. 12:53client and server
  352. 12:55really simply so just imagine the
  353. 12:57scenario let's take the handcuff
  354. 12:58scenario let's say that a player is
  355. 13:00handcuffed and you have you know maybe a
  356. 13:02radial menu for police where they can
  357. 13:03uncuff right
  358. 13:05and we want this radial menu to not suck
  359. 13:07so when the player's not cuffed we want
  360. 13:09them the option to say handcuff and when
  361. 13:11they are cuffed we want it to say uncuff
  362. 13:14well how would you do that before maybe
  363. 13:16when they open the radial menu and
  364. 13:18they're looking at a player you go call
  365. 13:19the server and ask for information about
  366. 13:21that player that's not a great time
  367. 13:22right so if we just have a state bag on
  368. 13:24the player is cuffed that's set to true
  369. 13:26or false
  370. 13:27then the client can access that
  371. 13:29information about the player they're
  372. 13:31looking at without ever having to talk
  373. 13:33to the server about it or like maintain
  374. 13:35a crazy table list of everybody who's
  375. 13:37handcuffed at the moment or any weird
  376. 13:39solutions like that so there's a lot of
  377. 13:40really really great ways to use state
  378. 13:42bags
  379. 13:44and with all of that said i think this
  380. 13:46is probably going to wrap up this first
  381. 13:48master series on state bags i've got a
  382. 13:50ton of useful links down below to
  383. 13:51information about state bags
  384. 13:53as well some forum posts things like
  385. 13:54that stuff that will hopefully get you
  386. 13:56started and definitely let me know down
  387. 13:58in the comments below if you found this
  388. 14:00useful if you learned anything let me
  389. 14:02know what you're excited to use state
  390. 14:03bags for
  391. 14:04be sure to hit that subscribe button
  392. 14:06check the bell icon so you get notified
  393. 14:08when new videos come out and as always
  394. 14:10i've got my discord link down below
  395. 14:11we've got a growing community of really
  396. 14:13helpful people there i'm active there if
  397. 14:15you have any scripting questions come in
  398. 14:17drop them i'm happy to do my best to try
  399. 14:19to help you out anyways thanks again for
  400. 14:21this uh joining us on this inaugural
  401. 14:23episode of master series and i hope to
  402. 14:25see you back soon

About this transcript

This page contains the full transcript of How to use State Bags (FiveM Lua Scripting Master Series) by CharlesHacks, generated from the public captions YouTube serves with the video. The transcript has 2,553 words across 402 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.