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

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

Asked by
Nidoxs 190
9 years ago

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)






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. M39a9am3R 3210 — 9y
0
It is a Local script and how do I fix this then? Nidoxs 190 — 9y
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. CodingEvolution 490 — 9y

1 answer

Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
9 years ago

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")
Ad

Answer this question