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

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

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

✓ Accepted answer
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)

2 comments

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.

0

Thanks :)

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