FiveM Scripting #10 - Player Spawning & Saving Location (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys it's alpha and in today's video i'm going to be showing you how to handle player spawning and saving the player's last location to your database um within your framework um if you haven't watched the previous tutorial 5m scripting tutorial number nine uh that first covered how to like first set up your database uh not database framework and um this tutorial is also really adding on to the framework that we started in the previous previous episode so if you haven't watched that please watch that one and follow that tutorial before you start this one anyway what we're going to be doing is going into our framework file we're going to be editing the same resource that we were using in the previous tutorial and in today's episode we're actually gonna be adding a client.lua so we're gonna go into the fx manifest and actually add client script and then this one's called client.lua you can save that and then we're actually going to make a client lure in the directory oh that's the wrong one new text document and call it client.lua change the file name boom and we're going to open this up and in here we're going to be handling um when the player spawns so we're just going to add a comment so when it's born say when spawn player pad oh hold up when spawn player head now we're gonna do add event handler and then player spawned so basically we've the one of the default resources for a 5m server which is spawn manager there's an event called player spawned and we're just going to add an event handler which means that this will happen when you know when that event triggers which is when a player spawns um so inside of this event handler we're going to trigger server event and then this is one that we're about to make this server event doesn't actually exist yet but we're going to call it framework colon spawn player so and then on the next line we're going to just add a quick debugging print statement this isn't required it's just so we know when all of the like events are being triggered like so and then that event handler is complete now we're gonna go down then we're going to set up the player position so we're going to register a net event capital register net event and this one we're going to call framework on last position actually no no actually capital last position and then on the next line i'm going to do add event handler uh obviously as you saw i was changing the capital there just make sure wherever you are so you type this you just make sure it's the same syntax then we're gonna do event handler for framework position and as you may be able to tell by now we're using framework at the start this isn't required it's just uh like all of the events that are within the framework you want to you know that it's from the framework uh obviously with a framework you might have a lot of resources so you just want to know that they're all working from the same framework so that's why i'm doing that it's just good practice to get into if you're going to be working with frameworks a lot and then so this one will be getting the position with three parameters position x position y and position z and then we'll drag down this uh final bracket and then end and then within here we're gonna do citizen dot weight one and then we're going to do local default model equal get e and what this is doing is going to be loading the default model um if you've played role play servers before you'll know there's like the um the multiplayer like the free mode um default player model which is like the bold guy most people refer to him as default danny um and that's basically what this is doing because obviously in a in a default 5m server it's just like you get a random skater pad or something like that uh this will be just be changing us to the the free mode pad and we're going to request model default then while not as model loaded this is basically making it making sure that the model is loaded before we set the player 2 to that model as model loaded default model it is in what way and then here we're going to set player model player id brackets and then colon are not colon uh comma default model then we're going to do set head default component variation and we're going to do player d brackets like so and then on the next line we're going to do set model as no oh as no longer needed and default model because once we've designed everything we need to do with it we can just set it as no needed so it doesn't put strain on the server and then we're going to do set entity chords and this is getting our player bed so this is us getting ipad minus one and then setting them to position x position y and position so and then also we're going to do one zero zero one these are just some other um parameters that need to be passed when you set an entities coordinates i just do one zero zero one and then we'll do a print statement just after that as well just to say entity coordinates set and then once you've done that we can go outside of the net event that we've just registered and then we're going to update layer position and then this one's going to be a thread uh which basically means it'll just run uh endlessly uh on the client yeah on the client basically um and then it's basically however often we want so we're not going to do it too often because we don't want to put a lot of strain but we don't need to be too intense of a script so we're going to do it uh like every five seconds so while true do and then we're gonna do citizen dot wait and we'll do 5 000 so this is in milliseconds so 5 000 milliseconds is five seconds uh you know every five seconds it'll uh save the player's position let me just add an end and then we're gonna do last x last y last z equals table dot unpack get entity towards get player head minus one outside of the first close bracket we're gonna do comma true and then the next line we're going to do trigger server event then we'll do we'll call this one framework on save layer position like so and then we're gonna need three um parameters so we're gonna do last x last y last z like so and on the next line we're just going to do another prints to make sure it's working player position saved so now we can save that and then we're actually done with the client.lure and now we're going to go and open our server.lure and we need to add a little bit to this we're gonna have to update um a bit inside of our player connecting uh like script area um so as you can see if you've got it like laid out the same way as me it should be line 41 it might be on a different line for you but basically where there's the my mysql async execute uh just below here just before the print if you've got the print you don't have to but if you do just before that we're going to add another execution here uh so we're gonna type my sql dot async dot execute once again just like on the line above and then in this one we're gonna do insert into user information so this won't be um user identifiers we're gonna make a new table in a moment as this one's gonna be called user information and then we're gonna do in brackets steam name beam id i'm going to do values beam name that's steam d and i'm going to close that one and then just after the um speech mark but before the uh close bracket we're going to do a comma and then on the next line we're going to do an open curly bracket and open square bracket and we're going to do that theme name and outside of the square bracket we'll do equals get player name source and we get comma d equals like so and that's all we need to add just there oh that needs to be indented a bit more just like that there we go that's all you need to add in this area but then once we've actually done that i'm going to go down a couple lines we're actually going to add a new section but this one's going to be handling the uh like the player spawning which is obviously what we're doing in today's video so we're going to be adding two so obviously we've got section one up top section one player connecting section two this one's gonna be player spawning and then we're gonna do a server event so register server event and then we're going to call this one framework player like so and then add event at the end just like so and inside of here uh so the first section within here we're going to do variable creation not indent so variable creation really local source equals source and we do local identifiers and if fires equals get layer identifiers source and then we're gonna do four [Music] k b in i pairs identifiers and in here we're gonna do if string dot match the beach mark steam then and then that will do steam id and the next line we're going to break so that'll break the loop because obviously we've found the steam id and that's all we need to do this may look very similar to what you've done up here and that's because it is actually the same of what we've done up here we just need to grab the steam id again um in the end of this event and once you've grabbed that we'll catch you outside of this just here and then the next section is going to be getting the user's position from the database so get the user's position on the database so then we're going to do a a fetch command from mysql so mysql.async dot i believe it's fetch all with the capital a and then it will select asterisk from user underscore information where steam id cmid is equal to at steam id and then we're going to do a comma and then an open curly bracket here i'm going to take this down and then inside of here we're going to do it's square bracket and then steam id that steam id is equal to d and then just here where we've got the close curly bracket on the next line we're going to do a comma and function result like so and then this final close bracket just here we're actually going to do an end over here just like so a couple of lines down and then on this line just here below the function result we do local spawn oz for spawn position and then we'll do json.decode uh it'll be result one dot position like so and then we're gonna do a couple lines down trigger client event with this one framework colon last position like so and then it would be source born oz and then we'll do one comma think of my spawn oz three like so and then on the next line we just go to another print for debugging uh very good oh i need to do it in beach marks very good client event last position like so and then outside of this server event we're going to do another server event to save the player position now we do register server event and this one i think we already rinsed this one uh yeah save player position so we're gonna do frame work save player position so and then we're gonna add an event handler so and then we've work a player position oh edition and we're going to need to do function and inside of here we're going to need the parameters pause x plus y was z like so i'm gonna take that down and do an end as usual and we're gonna do local source equals and then here again we're gonna need to do the same thing as um as up here so we're just going to grab this and copy it again so obviously with the local sourcing source and local identifiers and uh we'll just grab that and it's obviously got this this loop just here where it's grabbing the steam id uh so just below that one we're gonna do my sql dot async dot execute i'm gonna do update user information set position equals position where steam id equals at steam id and then we did a comma and open curly bracket and then open it onto the next line and then i'm sure they're getting used to this by now and i'm gonna do that theme id this one's going to be equal to steam id and then we'll go to a comma because we've got a next one this one's going to be at position and this one is actually going to be we're going to do a speech mark and then uh like a curly bracket and space and then it's gonna have to be do this two dots and then pause x and then space two dot space and we have to do a comma like so and with a space and add pause wire so basically what these two dots mean is it's concatenating so it's adding this piece of text to this and this piece of text to this and they're all adding to make one piece of text um it may seem confusing but if you know what i'm talking about it'll make perfect sense to you um so i'm gonna add another one another comma with space dot dot and then pause z suppose x y z and then dot dot and then plus close curly bracket like so and then once we've done that we can actually go to the next line after that then we're gonna do one final debug of print layer position saved like so and once we've done that we've actually finished off in the server.lua and after that we're going to go to our framework.sql file and open that with visual studio code like so and then so obviously we've created this in the previous tutorial but today we're actually going to be creating a new table then what we're going to be doing is create label and we have to use the um tilde again not the quote marks as you guys will remember from the previous episode so these are not like speech marks or quote marks or anything like that so these are speech marks these are quote marks what are these we're not using any of those we're using tildes which are the key just below the escape key for most people um if you if you don't have it just copy and paste it from the internet or whatever so it's going to be user information and then open and then here we're going to need a lot of um these so i'm just going to grab these and then copy and paste them so i'm sure the first three can only have three uh fields within this table so we're going to do steam name and steam id they're exactly the same then license we're actually going to change to position and then it's also going to be null instead of not null and it's gonna be null default and then here we're actually gonna put um the default coordinates so i'll leave these in the description so these coordinates are just inside one of the buildings in the center of the city uh it's a very common commonly known building so yeah if you want to use those you can obviously if you want to change the default if it's like the first spawn position you can do but if you want to use those go for it and then on the next line we're actually going to do primary oh primary oh it needs to be capital not indented primary key and then this is going to be theme id dmid like so and then comma on the next line we're going to do constraint and then we're going to call this one uh like fk fk means foreign key if you know about databases you should know what foreign keys are if not don't worry about it just do as i do so we're going to do fk user information user identifiers like so oh i've used the wrong symbol here i need to use tildes not bookmarks like so and then we're going to do foreign key and this one's going to be steam id like so so this basically means it's going to grab the steem id from user identifiers so if this one's ever changed up here it will also change down here and then we're gonna do references user identifiers and then the column that it references is steam id and then on update cascade and on delete cascade i don't know why that one's white but it should work anyway so basically what this means is uh the primary key for this table is steam id but it's also a foreign key which means it's grabbed from this table and then um on update cascade on delete cascade basically if you don't know what that means don't worry just do it as it is and then we'll save that oh but just before we finish that we do need to add a semicolon on the end because that is the syntax for mysql and then what we'll do is we'll minimize that because we should be all finished with that and what we're actually going to do is we're going to open this with heidi sql um so obviously what you'll need to do is open up exact examp and make sure that mysql is started like so and i'm also going to open up heidi sql which as you guys should know is the database management system and when we open that what i'm actually going to do um if you've already got the framework you can just delete it by just right right click the um database and press drop and that will totally delete the database that's perfectly fine for about to get it back anyway and then what we're going to do is file load sql file and we're going to grab it from in here okay so i had a small little error here on line 69 where uh i didn't actually put capitals for sql just here so it needs to be mysql with capital sq and l so i'm just going to save that and then restart the server and hopefully that should fix the error that i just got now as you guys can see it says started resource framework dash init and there's no errors so that's perfect so if we start up 5m it should work so as we're loading in here it should be grabbing all of our identifiers and popping them into the database like it did in the previous episode but then obviously because we have two tables now it should also put our identifiers into the user information table along with giving us the default position that is stated uh so we'll just check here yeah perfect so it's just placed us into the default position this is where it is inside of this building here you may know this building and also our ped is also set to the free mode default head which is what we wanted to do so that's perfect and also if i press f8 to open the console as you can see every five seconds it is saving my player position so i should do it now yep perfect so basically if i run out and run over to say under this umbrella just here and wait for it to save my position like so and then if i quit the server we can actually check in here if i refresh like so as you can see it's grabbed all of my identifiers like we did in the previous episode but then if we go to the other table user information as you can see it's grabbed my steam name steam id and also save my position that was saved of where i was in the server just before i left but now if i boot up 5m once again and then rejoin the server as you can see it has spawned me where i was last saved so that is all working correctly that is all for today's video um in terms of like coding uh but what i what i also can show you is if i quit out of 5m as you guys obviously know we did add a foreign key which basically means that um any this is basically the master table mysterious user identifiers so if i edit this road that has this theme id obviously because this table also has the same steam id if i change this to let's say if i get rid of those like so it also edits on this table if i delete this row it also deletes off of this table as you can see uh which is perfect so basically it just means that if you if you wanted to get rid of a user in your database it will get rid of it out of all tables you don't need to go one by one every table uh it basically makes things a lot easier in the long run uh but that is everything for today's tutorial if you enjoyed please leave a like uh if you need any support please join my discord in the description and please leave a comment of what else you'd like to see me do and please share it with your friends if they're also interested in 5m and i'll see you guys in the next tutorial take care
Info
Channel: Alfa
Views: 675
Rating: undefined out of 5
Keywords: ultimate, best, the, most, insane, minecraft, gta, call, of, duty, zombies, online, offline, glitch, cheat, money, out, map, new, working, feb, aug, mar, jun, jul, may, real, no, fake, downloads, ads, pc, xbox, one, 360, ps3, ps4, wii, siege, 2020, funny moments, utub, alfa, fivem, scripting, tutorial, 2021, server, client, resource, citizenfx, esx, vrp, essentialmode, FiveM Scripting #10 - Player Spawning & Saving Location (Tutorial), saving location, database, framework, coordinates, thread, mysql
Id: z9xWh-fydqU
Channel Id: undefined
Length: 28min 57sec (1737 seconds)
Published: Sat Dec 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.