player added roblox This is a topic that many people are looking for. bluevelvetrestaurant.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, bluevelvetrestaurant.com would like to introduce to you Roblox: Doing PlayerAdded Correctly. Following along are instructions in the video below:
“Let s talk about when a player enters the game and how to handle that that properly so i am making this video. Really quickly because i ve seen a of forum posts and things of that nature. Where people are having problems with player added. And they don t know what s the problem and it always seems to be this thing.
I m gonna show so let s just get right to it so if we have a function put. Player added. So we re trying to capture when a player s add is the server. So the event.
There is game that players player added. We re gonna connect that to our player added function so just like that right so when a player is added to the game past that event on to the player added function and we get the player object. As our argument. So we could print out player name as entered the server simple enough so if i click play i should see crazing 32s enter to the server so really simple awesome.
However there s a lot of people that have code like this they may have other things in here. Whatever and they complain that this isn t working that for some reason they re not getting this function of fire. You know they re not getting this printed out and they re really confused what s the problem. I want to show you what the problem to be so.
If i throw..
Which is so that s right here wait at the top and if i click play again ok. It s been about two seconds. You ll notice it doesn t say that i entered the server. So what s the problem.
The problem is actually really simple here right within this two seconds. My player has been added to the game. And then i have hooked up the event player added. This is only going to work for players added at this moment and afterwards not prior it doesn t add it doesn t have effect in like in the past you know.
There s no retroactive event firing here going on and so in essence. This has missed my player being added into the game. That s the problem here and a lot of times. You know people don t have a weight to at the top here.
But they might have other things going on maybe they re doing a web request or setting some other things up or loading a module that does some yielding any sort of thing that takes a little bit of time away from getting here could potentially cause your script to miss players being added into your server. So what is the workaround how do we fix this i ll throw that wait to back up here the way around this is pretty simple all we need to do is add another routine at the bottom here. Where we scan through all the players and call this function ourselves. So i m just gonna do that right here.
So just do a generic for loop..
And use the get players event on the players service and that will give us an array of all the players in the game. Currently and then we can call player added ourselves awesome. So really. It s that simple to fix this so if i go back here and i click play it s still gonna wait two seconds.
But then we see it knows that i entered at the server. So we fixed it however this is still not perfect. I think there s one other thing to be done to this that is helpful because if our player added function is doing any sort of yielding. You know maybe.
It s doing something like player died character. Added. Weights and this will actually cause problems to look at that in a second. But if we re doing something like that where we re waiting then it s possible that this is gonna be bad for the player.
It s not really a performance issue in terms of like bad performance like you typically. Think but imagine three players get captured right. Herei but you have to wait for each character be added. So i you re gonna have a problem.
Where you know the this is yielding and waiting for one player to be added..
Until it goes to the next player. And so that s not ideal and so what we want to do is wrap this in a spawn like this so. This is the simplest way of spawning. A new co routine essentially basically a new thread.
I and calling player added that way we can simultaneously collect all the players and call a player added at the same time so again pretty straightforward. That s all you have to do now kind of a gotcha with this that i think is really important still is in scenarios. Where we do get to the step and get players that are already in the game. Things like this could cause problems so imagine this wait by the time that happens not only as my player into the game.
But my character has been added also that means we have the same issue with this event. Where character added. Well the character already has been added and so we actually miss the character being added. And this is waiting for the next time my character responds so that s a problem so in this sort of instance.
Where we have code like this there s a few different ways we can go about solving this if we need the character object. We could do something like this local character equals and we could do just some little bullying and logic here so we can do player that character or if that s nil. We can actually use an or statement and say or player character added weight. And so this is kind of some fancy louis and tax here.
But basically what we re saying is resolve this variable character to the players character or if this is nil in this case use this instead character added weight..
And what the the weight event or the weight method of an event will return whatever that event spits out which would be the character in this case. And so we d still get the character object result for those that maybe from the other languages. This is not a ternary operation. This is just a logical or it s just the way variables would get resolved in lua.
It s just a fancy way of doing it you know this is the exact same way you could have done this instead if not character and character people s player that character added weight. But that s just lengthier for for this now. If you want to wrap it and capture every time the player character is added like we could do this instead so we could do we want to fry. It the first time in case that the player is already there so we do player character or player.
That character added weight. And then hook up that event again to that event just like that so. This is gonna again bypass a possibility that the character already exists in the game. And just a safer way of capturing all this information in that scenario.
So this is kind of the safe way in order to capture both players entering the game and the ” ..
Thank you for watching all the articles on the topic Roblox: Doing PlayerAdded Correctly. All shares of bluevelvetrestaurant.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.