Welcome to the Forums!
Join thousands of other Roblox members with a free account.
Create Forum Account

Error " attempt to index nil with 'LocalPlayer' " while running script. Please help

  • This website was created by Roblox members to replace the official forums (because Roblox shut them down) Join us if you like Roblox!

SophisticatedSponge

New Member
Reputation: 0 / 0 / 0
Nov 19, 2021
2
0
1
Roblox
SophisticatedNoob
Lua:
local Players = game:GetService(game.Players)
local userinputservice = game:GetService("UserInputService")

local player = Players.LocalPlayer -- line where the error is given
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:Wait()
end

local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")

local m1p1id = "rbxassetid://8044726911"
local m1p2id = "rbxassetid://8044742197"
local m1p3id = "rbxassetid://8044745165"

local m1p1 = Instance.new ("Animation")
m1p1.animationid =  m1p1id

local m1p1track = Animator:loadAnimation(m1p1)
m1p1track.priority = Enum.AnimationPriority.Action
m1p1track.looped = false

local m1p2 = Instance.new ("Animation")
m1p2.animationid = m1p2id

local m1p2track = Animator:loadAnimation(m1p2)
m1p2track.priority = Enum.AnimationPriority.Action
m1p2track.looped = false

local m1p3 = Instance.new ("Animation")
m1p3.animationid = m1p3id

local m1p3track = Animator:loadAnimation(m1p3)
m1p3track.priority = Enum.AnimationPriority.Action
m1p3track.looped = false

userinputservice.InputBegan:Connect(function(Input, gameprocessedevent)
  
    if Input.userinputtype == Enum.UserInputType.MouseButton1 then
      
        m1p1track:play ()
        humanoid.walkspeed = 0
        humanoid.jumppower = 0
      
        m1p1track.stopped:wait ()
        humanoid.walkspeed = 16
        humanoid.jumppower = 50
      
    end
  
  
end)

I'm pretty new to Lua so this might just be a rookie mistake. Please let me know if you find the solution. Thank you
 

CallMeKY

Moderator
Ultra
Reputation: 8 / 0 / 0
Feb 25, 2018
1,787
98
882
113
Maine, USA
Roblox
CallMeKY
Discord
CallMeKY#5012
Lua:
local Players = game:GetService(game.Players)
local userinputservice = game:GetService("UserInputService")

local player = Players.LocalPlayer -- line where the error is given
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:Wait()
end

local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")

local m1p1id = "rbxassetid://8044726911"
local m1p2id = "rbxassetid://8044742197"
local m1p3id = "rbxassetid://8044745165"

local m1p1 = Instance.new ("Animation")
m1p1.animationid =  m1p1id

local m1p1track = Animator:loadAnimation(m1p1)
m1p1track.priority = Enum.AnimationPriority.Action
m1p1track.looped = false

local m1p2 = Instance.new ("Animation")
m1p2.animationid = m1p2id

local m1p2track = Animator:loadAnimation(m1p2)
m1p2track.priority = Enum.AnimationPriority.Action
m1p2track.looped = false

local m1p3 = Instance.new ("Animation")
m1p3.animationid = m1p3id

local m1p3track = Animator:loadAnimation(m1p3)
m1p3track.priority = Enum.AnimationPriority.Action
m1p3track.looped = false

userinputservice.InputBegan:Connect(function(Input, gameprocessedevent)
 
    if Input.userinputtype == Enum.UserInputType.MouseButton1 then
     
        m1p1track:play ()
        humanoid.walkspeed = 0
        humanoid.jumppower = 0
     
        m1p1track.stopped:wait ()
        humanoid.walkspeed = 16
        humanoid.jumppower = 50
     
    end
 
 
end)

I'm pretty new to Lua so this might just be a rookie mistake. Please let me know if you find the solution. Thank you
GetService takes a string and gets or creates the service, you're trying to give it the players service. Just change line 1 to local players = game:GetService('Players')
 

SophisticatedSponge

New Member
Reputation: 0 / 0 / 0
Nov 19, 2021
2
0
1
Roblox
SophisticatedNoob
Lua:
local Players = game:GetService(game.Players)
local userinputservice = game:GetService("UserInputService")

local player = Players.LocalPlayer -- line where the error is given
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:Wait()
end

local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")

local m1p1id = "rbxassetid://8044726911"
local m1p2id = "rbxassetid://8044742197"
local m1p3id = "rbxassetid://8044745165"

local m1p1 = Instance.new ("Animation")
m1p1.animationid =  m1p1id

local m1p1track = Animator:loadAnimation(m1p1)
m1p1track.priority = Enum.AnimationPriority.Action
m1p1track.looped = false

local m1p2 = Instance.new ("Animation")
m1p2.animationid = m1p2id

local m1p2track = Animator:loadAnimation(m1p2)
m1p2track.priority = Enum.AnimationPriority.Action
m1p2track.looped = false

local m1p3 = Instance.new ("Animation")
m1p3.animationid = m1p3id

local m1p3track = Animator:loadAnimation(m1p3)
m1p3track.priority = Enum.AnimationPriority.Action
m1p3track.looped = false

userinputservice.InputBegan:Connect(function(Input, gameprocessedevent)

    if Input.userinputtype == Enum.UserInputType.MouseButton1 then
    
        m1p1track:play ()
        humanoid.walkspeed = 0
        humanoid.jumppower = 0
    
        m1p1track.stopped:wait ()
        humanoid.walkspeed = 16
        humanoid.jumppower = 50
    
    end


end)

I'm pretty new to Lua so this might just be a rookie mistake. Please let me know if you find the solution. Thank you
GetService takes a string and gets or creates the service, you're trying to give it the players service. Just change line 1 to local players = game:GetService('Players')
thank you very much! I'll try this right away