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
7 years ago

Pretty much the question explains everything

Script:

script.Parent.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local Victories = player.leaderstats.Victories
    local human = hit.Parent:FindFirstChild("Humanoid") 
    if human.Sit then
        local sound = Instance.new("Sound",game.Workspace.Objects.Sounds)
        sound.SoundId = "rbxasset://sounds/victory.wav"
        sound.MaxDistance = 10.000
        sound:Play()
        Victories.Value = Victories.Value + 1
        human.Sit = false
        wait(2)
        sound:Destroy()
    end
end)

1 answer

Log in to vote
0
Answered by 7 years ago
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then --< You need to check it it's an actual player that is touching the part.

    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local Victories = player.leaderstats.Victories
    local human = hit.Parent:FindFirstChild("Humanoid") 
    if human.Sit then
        local sound = Instance.new("Sound",game.Workspace.Objects.Sounds)
        sound.SoundId = "rbxasset://sounds/victory.wav"
        sound.MaxDistance = 10.000
        sound:Play()
        Victories.Value = Victories.Value + 1
        human.Sit = false
        wait(2)
        sound:Destroy()
    end
end
end)
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 — 7y
0
Thanks :) 130363 67 — 7y
Ad

Answer this question