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

how do i fix "attempt to index local 'player' (a nil value)" ?

Asked by
130363 67
8 years ago

Pretty much the question explains everything

Script:

01script.Parent.Touched:connect(function(hit)
02    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
03    local Victories = player.leaderstats.Victories
04    local human = hit.Parent:FindFirstChild("Humanoid")
05    if human.Sit then
06        local sound = Instance.new("Sound",game.Workspace.Objects.Sounds)
07        sound.SoundId = "rbxasset://sounds/victory.wav"
08        sound.MaxDistance = 10.000
09        sound:Play()
10        Victories.Value = Victories.Value + 1
11        human.Sit = false
12        wait(2)
13        sound:Destroy()
14    end
15end)

1 answer

Log in to vote
0
Answered by 8 years ago
01script.Parent.Touched:connect(function(hit)
02if hit.Parent:FindFirstChild("Humanoid") then --< You need to check it it's an actual player that is touching the part.
03 
04    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
05    local Victories = player.leaderstats.Victories
06    local human = hit.Parent:FindFirstChild("Humanoid")
07    if human.Sit then
08        local sound = Instance.new("Sound",game.Workspace.Objects.Sounds)
09        sound.SoundId = "rbxasset://sounds/victory.wav"
10        sound.MaxDistance = 10.000
11        sound:Play()
12        Victories.Value = Victories.Value + 1
13        human.Sit = false
14        wait(2)
15        sound:Destroy()
16    end
17end
18end)
0
Why not put line 6 before line 2? It's already defined for to search for the asset. To add, however, the problem at hand isn't necessarily the Humanoid, but in this case, it's that "player" may not be a player, and thus the error; either that, or "hit.Parent" returned nil. TheeDeathCaster 2368 — 8y
0
Thanks :) 130363 67 — 8y
Ad

Answer this question