Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

is there any way to access a character without using characteradded or player events In ServScript?

So all i want to know is, if there is any way I can access the character without using any events such as characteradded or player added in a SERVER SCRIPT NOT A LOCAL SCRIPT

1 answer

First of all, interesting name. And for this you would use remote events. Remote events are a way to communicate between clients and servers. And like that, you can send the LocalPlayer from a client (pretty much a local script) to the server (which is the server script). this is a good document on them.

Remote Events

Now we got remote events, let me explain these. Remote Events are used to communicate between a client and a server, the client is the player that is currently playing, or pretty much all players, and the server, is roblox, (the server connects between all clients). Now when we need to use these? As I said, when we want a communication between server and client, when we want to exchange information beween them. So for example, in a Server script (which is the normal script) you cant use LocalPlayer, because local player is a local thing, it wouldn't make sense to refer to it from the server (and that is exactly your occasion). But we can from the client, so we need to transfer it from the client to the server. And that is pretty much the most common thing that you'd want to use remote events for. And that is exactly our case here. Remote Events are always placed in ReplicatedStorage, if you're wondering why, as I said the server has his limitaions, and the client (or local side) has his own limitaions, there are certain places and things that the client can do while the server can't, and vise-versa. If we put our remote event inside PlayerGui, that wouldn't make sense, because that place is local, and it can be only accessed from a local script. That's why we need to put our remote event in a place where both the server and the client can access, and ReplicatedStorage is the place for that, because it is accessed by both. (workspace can work too, but that wouldn't be efficient). And of course when using remote events, you need to have one same remote event connecting the local and server script, that's why sometimes you may have multiple remotes for multiple actions and communications. And remote events aren't only a Server/Client communication, they are also a Server/AllClients and a Client/Client. We also got RemoteFunctions, which aren't that different but have a not so similar way of working. And that's really it in terms of explaining. I didn't explain as much as I wanted because I wanted to keep this as simple as your question. But here is more info, this is a good document on them.

But now let's go to the scripting part. First insert a Remote Event that we will use

This will be your local script

local player = game.Players.LocalPlayer --our local player var
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent") --this is our remote event
local var = "hi'

--Now here, to send our message to the server, we use the :FireServer function, this will send the information to the server, and we say the server "listen"s to that information, which means he gets it. And the transformation of info from the client to the server is refered to as "Replicate".

remote:FireServer(player, 15212, true, var)
--Now you see, the info that were gonna send is gonna be set as the arguments for the function. and always, always put the player as the first arg, because he is the default arg and not putting him might mest up things. and as you can see its not just players, you can transfer values, instances, variables and many things.

Now this is the server script

local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent") --the same remote that we used

--Now for the server side, we would use an event called .OnServerEvent, and this event, as i said "listen"s or "recieve"s the info recieved from the client side.

Remote.OnServerEvent:Connect(function(plr, num, bool, var) -- and as you can see here we put the args that will represent the info that we sent from the client, and they obbviously dont have to be the same names.
	print(plr)
end)

And that's it, you'll see that it printed the local player succefully. And that is the basics of using remote events. Now we can hit our goal, to access the character from a server script.

We would have a local script, pretty much anywhere that is local, i'd recommend StarterGui and a server script, pretty much anywhere that is server side, such as ServerScriptService. And we would insert a remote to use for the communication, in ReplicatedStorage.

This would be your local script.

local plr = game.Players.LocalPlayer
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
--we can technaclly set do plr.Character to just transfer the character, but remember that the first argument is the player, so we wouldn't want that, and also its handy having the player in a server script.
remote:FireServer(plr)

ServerScript

local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local char
remote.OnServerEvent:Connect(plr)
	char = plr.Character
	print(plr)
	print(char)
end)

And that is it, simple!

This discussion is preserved as it was. New answers, comments, and votes are closed.