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

Help with 'player' a nil value'?

Asked by 9 years ago
local part = script.Parent
part.Touched:connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        hum.WalkSpeed = 0
        local player = game.Players:GetPlayerFromCharacter(hum)
        player.PlayerGui.PlantBombA.Frame.Visible = true
        if player.PlayerGui.PlantBombA.Frame.Visible.Changed then
            hum.WalkSpeed = 16
        end
    end
end)

The error is...

16:45:04.936 - Workspace.PlantDetector.Script:7: attempt to index local 'player' (a nil value)

Thanks in advance for any help you may provide!

0
try hum.Parent It might be that you are using Humanoid and not the character model User#5423 17 — 9y

1 answer

Log in to vote
2
Answered by
Marios2 360 Moderation Voter
9 years ago

When using GetPlayerFromCharacter(), you will want to refer to the character itself and not the humanoid, as the character has the player's name and not the humanoid.

That's pretty much the issue.

Your fixed script:

local part = script.Parent
part.Touched:connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        hum.WalkSpeed = 0
        local player = game.Players:GetPlayerFromCharacter(hit.Parent) --If hit.Parent.Humanoid then hit.Parent is a character
        player.PlayerGui.PlantBombA.Frame.Visible = true
        if player.PlayerGui.PlantBombA.Frame.Visible.Changed then
            hum.WalkSpeed = 16
        end
    end
end)
Ad

Answer this question