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

Why does the output error "Attempt to index local player"?

I think the error is on line 2? How do I fix this? Workspace.Model.Sushi.Script:2: attempt to index local 'player' (a nil value)

local player = game.Players.LocalPlayer
repeat wait() until player.Character ~= nil
local hum = player.Character:WaitForChild("Humanoid")

local animation = script.ChefAnimation 
local AnimTrack = hum:LoadAnimation(animation)

debounce = false
function onClick(click, player)
	repeat wait() until player.Character ~= nil
    local hum = player.Character:WaitForChild("Humanoid")

    local animation = script.ChefAnimation 
    local AnimTrack = hum:LoadAnimation(animation)
	local player = game.Players:GetPlayerFromCharacter(click.Parent)
	local char = player.Character
	
	if debounce == false then
	debounce = true
	AnimTrack:Play()  
	wait(2)
    local a = game.ServerStorage.SpringRoll:Clone()
	a.Parent = player.Backpack
	wait(10)
	debounce = false	
	    end
	end


script.Parent.MouseButton1Down:connect(onClick)






3 comments

0

Server Script or Local Script? You need to provide more information with your posts other than just a error. We sometimes need to know what type of script you're using. Also, you overwrote the player value in the onClick function by placing them in the parenthesis, it will be considered a nil value.

0

It is a Local script and how do I fix this then?

0

This is in a server script. Not a local script. Put it in a local script and it will work. And as M39 said, you've overwritten the player variable inside the function.

1 answer

The problem is, the script runs before LocalPlayer finishes loading.

local player = game.Players.LocalPlayer
repeat wait() until player.Character ~= nil --player, AKA LocalPlayer, was nil when you referenced it
local hum = player.Character:WaitForChild("Humanoid")

Try the following solution:

repeat wait() until game.Players.LocalPlayer
local player = game.Players.LocalPlayer
local Character = player:WaitForChild("Character")
local hum = Character:WaitForChild("Humanoid")

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