I was searching for a team change GUI tutorial, and found one that seemed to work. However, at one point I needed to copy paste a script. It works now, but I do not understand the script.
local p = script.Parent.Parent.Parent.Parent.Parent.Name
script.Parent.MouseButton1Click:Connect(function()
game.Players[p].TeamColor = BrickColor.new("Really blue")
game.Workspace[p].Humanoid.Health = 0
script.Parent.Parent.Parent.Enabled = false
end)
p is equal to ‘workspace’. This is what I have so far.
local p = script.Parent.Parent.Parent.Parent.Parent.Name --defining workspace
script.Parent.MouseButton1Click:Connect(function() -- when button is clicked
game.Players[p].TeamColor = BrickColor.new("Really blue") -- I do not understand why putting [p] would make it work.
game.Workspace[p].Humanoid.Health = 0 -- Same as last line
script.Parent.Parent.Parent.Enabled = false -- what would making the screen GUI's enabled variable do?
end)
So basically, I do not understand this part of the script:
game.Players[p].TeamColor = BrickColor.new("Really blue") -- I do not understand why putting [p] would make it work.
game.Workspace[p].Humanoid.Health = 0 -- Same as last line
You should read about tables to understand how those two lines work. Using a square bracket means it looks inside the table to find the value that matches what is inside the brackets.
So it appears here that this takes place inside of a local script I presume? I am actually relatively surprised that this works considering FireServers aren’t used… but ok. Similar to what someone said before, p is supposed to not be workspace. p is meant to be either put inside of a for i,v in pairs(game.Players:GetChildren()) loop, or P is supposed to reference an index within that loop, I have 0 clue. Regardless, the 2 lines of code that you wrote that you don’t understand…
The first piece of code, changes the player’s team color to “Really blue”. If you look in your teams folder located in your explorer, you’ll be able to see where the teams are located. If you click on a team, and view it’s properties, you’ll be able to see the team’s color assigned to it. So, if you wanted the player to change to the “Firefighter” team, and that team color is “Really red”, you would do: game.Players[p].TeamColor = BrickColor.new(“Really red”). That would successfully change them to that team (at least according to the syntax of your script).
As for the 2nd line you are confused about, that simply kills the player. You access the player’s character, get their humanoid, and then set their health equal to 0.
I hope this helps! Let me know if you have questions.
Note that the tutorial you’re using is outdated and although it may seem like your team has changed on your screen. In reality it hasn’t replicated to the server and you are in fact not on the team you selected. You need to utilize remote events! But I’ll try my best to answer your original question below:
First you need to understand what the variable p is.
local p = script.Parent.Parent.Parent.Parent.Parent.Name -- Note that its .Name there for referencing the name of the object (which is a string).
I’m guessing that this basically references the players name. So think of p as:
local p = "nicknickphoenix" -- Pretend that it was referencing your name
Now all you’re doing is referencing the object in game.Players using the string p.
local p = "nicknickphoenix"
print(game.Players[p]) -- If the player p actually exist in game.Players than it will print the player instance. If not it will just print nil cause it doesn't exist.
Think of it as being the same as:
game.Players.nicknickphoenix -- Still referencing a player but note this may not work with special characters and therefore has to be wrapped in brackets ["nicknickphoenix"] or use :FindFirstChild("nicknickphoenix"), etc```
Hopefully the explanation makes sense!
Heres a way to somewhat visualise it. Go into studio → Make sure your in client view → than in players select your player and change their team color on the client. Like below:
Now I want you to click “Current: Client” (which i circled in red) so you can switch to the server view. You will see that on the server your team actually hasn’t changed at all and is still the original color it was before (for me that would be white):
When I first join the game in roblox studio, I automatically become cop. After I change team to be a criminal, it shows that I am a criminal. Even when I look from server view (going through workspace < players < nicknickphoenix, it shows I am a criminal.
||game.Players[p].TeamColor = BrickColor.new("Really blue") -- I do not understand why putting [p] would make it work.|
game.Workspace[p].Humanoid.Health = 0 -- Same as last line|
I’m assuming game.Players[p] is to find “P”. It defines P as a variable, so it’s trying to find it.
Like @Jxl_s pointed out, [p] is referring to a specific player and not workspace.
game.Players[p].TeamColor = BrickColor.new("Really blue")
-- Changes the player's team color to blue
game.Workspace[p].Humanoid.Health = 0
-- Sets the player's health to 0