#Ancestry
The top ancestor (excluding the DataModel and the Players service) of a LocalScript is typically the LocalPlayer. You can place them in ReplicatedFirst and have them execute there but we aren't going to talk about that. And LocalScripts run for one client, you. Scripts run on the server, which is responsible for connecting the players together in your game.
#Why LocalPlayer is nil on the server
Think of it as your school classmates and your teacher. Think of your school classmates as other clients, and think of your teacher as the server. If you told your teacher to get the local student, your teacher would be very confused, because which student is the local student? For this reason, LocalPlayer is nil on the server. Your game could have multiple players in it, and when you try using LocalPlayer, it can't just randomly pick a player. It makes no sense.
#Not a mind reader
Code cannot read your mind. This is why it must be all typed out. So instead of doing this:
make a part inside of the workspace with color red
You must do this:
local part = Instance.new("Part")
part.BrickColor = BrickColor.new("Really red")
part.Parent = workspace
With that in mind let's fix up your script.
The Players service has a method called "GetPlayerFromCharacter" which returns a player from their character model, if said model belongs to the player. When a player touches a part it will be a body part of their character.
local tc = script.Parent
local function check(part)
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player then
player.TeamColor = BrickColor.new(194)
end
end
tc.Touched:Connect(check)
Additionally, I have made your check function from a global function to a local function. The usage of global variables is bad practice, as it makes your code messy.
######Hopefully this answered your question and if it did don't forget to hit that accept button. If you have any more questions feel free to leave them in the comments.
4 comments
this is supposed to be a local script but since your changing something serversided(team) or i assume its serversided your looking for i would use remote events
You cant use "Local Player" on a Script because The LocalPlayer doesn't exsist on the server.Send the Player as a paramater.
LOL i love the "GetPlayerFromCharacter" Method :D
LOL i love the "GetPlayerFromCharacter" Method :D