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

i get the error "attempt to index local 'player' (a nil value)" can you please help?

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?

4 comments

0

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

0

You cant use "Local Player" on a Script because The LocalPlayer doesn't exsist on the server.Send the Player as a paramater.

0

LOL i love the "GetPlayerFromCharacter" Method :D

0

LOL i love the "GetPlayerFromCharacter" Method :D

3 answers

✓ Accepted answer

#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 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.


######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.

1 comment

1

thank you so much!

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)

2 comments

0

error: Workspace.Part.Script:4: attempt to index local 'player' (a nil value)

0

No. There is no second parameter assigned to Touched.

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)

1 comment

0

194 is a brick color

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