Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I reference every specific player in a script?

Asked by
mrcool2 75
9 years ago

The scripts I have been making in studio only actually work in solo mode, but they do not work online, I know the reason for this is because all of the scripts I have made are all based on 'Player', for example to make a players torso invisible:

game.Workspace.Player.Torso.Transparency = 1

How do I make it so that 'Player' is changed to any player on the servers username? For example, if I was on the server, the script would automatically change to:

game.Workspace.mrcool2.Torso.Transparency = 1

EDIT:

In an actual concept, I have a script which is used to change the properties of an item the player is holding in their hand when it interacts with the environment, so i need the script to be able to find the player in the workspace, to find said tool, for example,

game.Workspace.Player1.Torch.Fire.Enabled

My issue is including every player in the place of 'Player1' so that the script can pick up the item in any players hand, without their username having to be manually written down in the script.

I'm sorry if this is really badly explained, it's a quite a difficult concept to explain >_<

0
Is this a localscript or a regular script? Can I get some context on what this script is? Nexx 51 — 9y
0
this would be in a regular script, as most of my scripts involve detecting tools that the player is holding mrcool2 75 — 9y

3 answers

Log in to vote
1
Answered by
Nexx 51
9 years ago

If this is a localscript, it can be done easily.

local player = game.Players.LocalPlayer
game.Workspace[player.Name].Torso.Transparency = 1

If its in a regular script, and you want this to happen when they first join, you can do this.

game.Players.PlayerAdded:connect(function(player)

    game.Workspace[player.Name].Torso.Transparency = 1

end)


0
I'm afraid it's not something that the player will need to start off with, the torso transparency was just an example, I'll add an edit with the context now mrcool2 75 — 9y
0
Excuse meh? First off the player is not a child of the workspace. Second of all, the torso a direct member of the character, not the player. DUDE if you dont know what your talking about, shut up and walk away. LateralLace 297 — 9y
0
My comment above is not a reply to you mrcool. Sorry if you thought I was offending you. I'm talking about Nexx and Nexx's answer. Sorry!! LateralLace 297 — 9y
0
brooo, its okay but I think I am talking about referencing the player as a child of the workspace as I'm needing to be able to get the script to recognize items that the player is holding in their hands, which I think would be found in the Workspace. It's all very confusing and it's my fault I'm terrible at explaining haha mrcool2 75 — 9y
View all comments (3 more)
1
LOL LATERALFACE!!! The reason he had the torso to the player is cause he is getting it from WORKSPACE! so game.Workspace[player.Name] is the CHARACTER, and the player's character is a child of workspace. Make sure you know what you're talking about before making a comment. NinjoOnline 1146 — 9y
0
Ha! CAN YOU NOT READ, my answer vaguely says how the player is a member of the Players file, not the workspace. Breh If you think your silly mind is better than mine, go right ahead and give em an answer. Other why's, shut it! LateralLace 297 — 9y
0
LateralLace, when you go to the workspace you can get the player from the name. Your player object inside the players tab in the explorer has the same name as the object in the workspace. For example if your Roblox username was CoIorEvent and you had the name of an object with that name then you can get it from anywhere else. You can do it by doing: game.Workspace:FindFirstChild(plr.Name) CoIorEvent 20 — 3y
Ad
Log in to vote
0
Answered by 9 years ago

There are you ways of specifying the player/player(s). The easiest one would be to create a local script, place into the StarterPlayerScripts file (or any client side service). It would look something like this:

player = game.Players.LocalPlayer --the local player stands for the client (player) who is running the script
repeat wait() until player.Character ~= nil --this wait and pause the next few lines of script until the players character loads
player.Character.Torso.Transparency = 1 --NOTE that you can only grab the torso from the character of the player

The second way of doing it is to use a server side script. There's a specific way of doing this. You cant just grab the LocalPlayer like we did in the local script. Instead, we have to locate the specific child from the players file (theplayerfrom the Players file). To do this, we create a table. A table is a list of values that is represented by one variable (an array). Then, we iterate through that table. What this does is it runs through all of the values in the table, one by one. This is represtented by v. Finaly for each value, we set the players torso transparency to 1. It should look something like this:

players = game.Players:GetChildren() --this returns a list of all the players in the `Players` file
for i,v in pairs(players) do -- this is what an iteration should look like
    repeat wait() until player.Character ~= nil --we wait until the game has loaded the character appearance
    player.Character.Torso.Transparency = 1
end

Now if your trying to speciy a specific player, you need specific conditions/arguments. For example, if youd only want o set the transparency of one player's torso using certain conditions, you'd use a bool (If Then Statement).

I hope this answered your question. If you have any other questions, please put a comment below. Otherwise, I have a list of helpfull links below. Your welcome!!!

HELPFUL NOTES

-The only way to access the player is from the "Players" game service, not the Workspace -If you are more of an experienced scripter, I suggest you use server scripts. Local Scripts can be limited and for security it's easier for them to be hacked.

LINKS:

Tables: http://wiki.roblox.com/index.php?title=Table

Booleans: http://wiki.roblox.com/index.php?title=Boolean

LocalScripts: http://wiki.roblox.com/index.php?title=API:Class/LocalScript

LocalPlayer Example: http://wiki.roblox.com/index.php?title=API:Class/Players/LocalPlayer

The player: http://wiki.roblox.com/index.php?title=API:Class/Player

The Players Service: http://wiki.roblox.com/index.php?title=API:Class/Players

0
You see, I took the first part you said and labelled player = game.Players:GetChildren() and put it before the command, game.Workspace.player.Torch.Fire.Enabled, yet it still does not seem to be substituting specific usernames into 'player' and the script does not run.. mrcool2 75 — 9y
0
Ok. I'll try adding some stuff to my answer above. First of all, there's a lot more you need to add if you want to make this script work. Again, it'll take awhile, so I might not finish it tonight. LateralLace 297 — 9y
Log in to vote
-1
Answered by 9 years ago

I think yo uwould have to put in Player1

0
Yeah, I had tried that, but it still only works in solo mode :I mrcool2 75 — 9y

Answer this question