code:
local tc = script.Parent function check(part) local player = game.Players.LocalPlayer --Find Player name local nameofplayer = player.Character.Name--Get the player in workspace game.Players.LocalPlayer.Team.TeamColor = BrickColor.new(194) end tc.Touched:Connect(check)
when you touch the brick it is supposed to change your team to the medium stone grey team but instead i get the error : Workspace.Part.Script:5: attempt to index local 'player' (a nil value) how would i fix this?
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 LocalScript
s run for one client, you. Script
s run on the server, which is responsible for connecting the players together in your game.
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.
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 may not be what touched the part! We should check first if it did 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.
Finding the Local Player won't work in a server script, so you can try connecting the player who touched the part to the function instead.
local tc = script.Parent function check(part,player) local nameofplayer = player.Name player.Team.TeamColor = BrickColor.new(194) end tc.Touched:Connect(check)
please try this
-And make sure its a script inside the part that's being touched
local part = script.Parent local use = false-------"Use" is a type of debounce local cooldown = 2 --You can change your cool down or wait time here! :) part.Touched:Connect(function(hit,player) --hit is basicly that first thing that touched the part.So if you Leg touches the part that becomes hit if hit and hit.Parent:FindFirstChild("Humanoid") then if use == false then use = true local nameofplayer = player.Name player.Team.TeamColor = BrickColor.new(194)---194??? i dont think its a brick color.... could it? --Usally its --> BrickColor.new("Baby blue") wait(cooldown) use = false end end end)