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

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

Asked by 5 years ago

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?

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 Donut792 216 — 5y
0
You cant use "Local Player" on a Script because The LocalPlayer doesn't exsist on the server.Send the Player as a paramater. Tizzel40 243 — 5y
0
LOL i love the "GetPlayerFromCharacter" Method :D Tizzel40 243 — 5y
0
LOL i love the "GetPlayerFromCharacter" Method :D Tizzel40 243 — 5y

3 answers

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

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
thank you so much! SodaZere 31 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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)
0
error: Workspace.Part.Script:4: attempt to index local 'player' (a nil value) SodaZere 31 — 5y
0
No. There is no second parameter assigned to Touched. piRadians 297 — 5y
Log in to vote
0
Answered by
Tizzel40 243 Moderation Voter
5 years ago

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)
0
194 is a brick color SodaZere 31 — 5y

Answer this question