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

[Fixed Like 2 days ago] Keeps saying Attempt to index local (a nil value) help please?

13:47:26.241 - local po = game.Players.LocalPlayer; --Get the player loca:2: attempt to index local 'po' (a nil value) 13:47:26.242 - Stack Begin 13:47:26.242 - Script 'local po = game.Players.LocalPlayer; --Get the player loca', Line 2 13:47:26.243 - Stack End

local po = game.Players.LocalPlayer;	 --Get the player
local Mouse = po:GetMouse(); 		 	--Get the mouse

Mouse.KeyDown:connect(function(Key)			 --Activated the KeyDown function with GetMouse
	if (Key:lower() == "k") then 		   	--Pick key to use
		
		
		
		local po = game.Players.LocalPlayer
local Power = Instance.new("Animation")---name before the "=" neoNob
Power.AnimationId = "220836646"--Same here neoN0b
local animTrack = po.Character.Humanoid:LoadAnimation(Power)--name in parentases, neoN0b
animTrack:Play()--Play pls
end
   end)

How do I fix this?

3 comments

0

Ignore the comments unless they help you help me. My friend is a jerk sometimes.

0

Is this in a Local Script? It won't work if put into a regular script.

0

It is, why do you think theres local script in the tags

3 answers

✓ Accepted answer

First, tab your code correctly. That has solved the last two problems I've read, you should get in the habit.

local po = game.Players.LocalPlayer;
--Get the player
local Mouse = po:GetMouse();
--Get the mouse

Mouse.KeyDown:connect(function(Key)
	--Activated the KeyDown function with GetMouse
	if (Key:lower() == "k") then
		-- Pick key to use
		local po = game.Players.LocalPlayer
		local Power = Instance.new("Animation")
		-- name before the "=" neoNob
		Power.AnimationId = "220836646"--Same here neoN0b
		local animTrack = po.Character.Humanoid:LoadAnimation(Power)
		-- name in parentases, neoN0b
		animTrack:Play()--Play pls
	end
end)

Now the error is telling you that po is a nil value. That means it's not a thing.

po = game.Players.LocalPlayer so po is supposed to be the LocalPlayer. But there isn't a LocalPlayer.

That probably means this is a Script object instead of a LocalScript. Script objects can't use LocalPlayer.

1 comment

0

It is a local script. In starterpack.

First off, GetMouse is depricated. Don't use it anymore. You have to use UserInputService or ContextActionService. I prefer UserInputService.

local UIService = game:GetService("UserInputService")

UIService.InputBegan:connect(function(input)
	if input.KeyCode = Enum.KeyCode.K then
		local po = game.Players.LocalPlayer
		local Power = Instance.new("Animation")
		Power.AnimationId = "220836646"
		local animTrack = po.Character.Humanoid:LoadAnimation(Power)
		animTrack:Play()
	end
end)

UserInputService uses InputObjects. These can give you all the information based on mouse position, keycode, and other things. View the API of UserInputService here and InputObjects here.

Lol turns out I didnt type the whole animation ID Surprised you guys didnt see that.

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