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

Help with 'player' a nil value'?

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!

1 comment

0

try hum.Parent It might be that you are using Humanoid and not the character model

1 answer

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)

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