Attempt to index nil with Name Error?

local tween = require(game:GetService("ServerStorage").Modules.Tweening)
local httpService = game:GetService("HttpService")
local plr = game:GetService("Players").LocalPlayer
local add = require(game.ServerStorage.Modules.AddingItems)

script.Parent.MouseButton1Click:Connect(function()
       print(plr.Name) -- if its just plr it will print 'nil'
       local location = script.Parent.Parent
       local pos1 = 0.5
       local pos2 = 1.5
       tween.out(plr, location, pos1, pos2)
       add.BuyWeapons(2, plr)
end)

This is my current script and for some reason i get error saying ā€œattempt to index nil with Nameā€. Not sure what the problems is, I’ve tried many different ways and none of them work.

You also need to have ā€œā€ around the name you want to print as it is a text string. So it should be like

print("plr.Name")

Hope this helps and sorry if it does not as I am new to scripting but this is quite basic.

1 Like

You can’t use LocalPlayer in a server script.

2 Likes

This wouldn’t work this would just print ā€œplr.Nameā€ in the output

1 Like

Were did he/she do that? That was a variable at the start.

I originally had script.Parent.MouseButton1Click:Connect(function(player) but that doesn’t work.

local plr = game:GetService(ā€œPlayersā€).LocalPlayer

That’s where.

but that doesn’t work.

Player isn’t a parameter for MouseButton1Click function.

What are you trying to do with the script then because you use print to print things in the output?

I am trying to print the players name so I know who pressed it, just to test it out.

I’m not too sure what the module script does, and I don’t really need to know, but you should use a remote event, and have the click function in a local script.

1 Like

You can’t get LocalPlayer in server scripts. Consider using a LocalScript, and use remotes to communicate with the server, or find another way to get the player.

2 Likes

I am not sure then sorry. You should have really put that in the description of what is wrong.

local tween = require(game:GetService(ā€œServerStorageā€).Modules.Tweening)
local httpService = game:GetService(ā€œHttpServiceā€)
local add = require(game.ServerStorage.Modules.AddingItems)

script.Parent.MouseButton1Click:Connect(function(Player)
print(Player.Name) – if its just plr it will print ā€˜nil’
local location = script.Parent.Parent
local pos1 = 0.
local pos2 = 1.5
tween.out(plr, location, pos1, pos2)
add.BuyWeapons(2, Player)
end)

Not sure if this fixes it :confused:

Already tried that, I’m going to use a local script.

Always use a LocalScript for UI related stuff. Also, LocalPlayer does not work in a regular script. All you need to change is that. Once you change it everything should work.

Just looking back into it and I just realised it said MouseButton1Click

use player and do

local tween = require(game:GetService("ServerStorage").Modules.Tweening)
local httpService = game:GetService("HttpService")
local add = require(game.ServerStorage.Modules.AddingItems)

game.Players.PlayerAdded:Connect(function(Player)
       script.Parent.MouseButton1Click:Connect(function()
       print(Player.Name) -- if its just plr it will print 'nil'
       local location = script.Parent.Parent
       local pos1 = 0.5
       local pos2 = 1.5
       tween.out(plr, location, pos1, pos2)
       add.BuyWeapons(2, Player)
       end)
end)

use ``` to format code

local tween = require(game:GetService(ā€œServerStorageā€).Modules.Tweening)
local httpService = game:GetService(ā€œHttpServiceā€)
local add = require(game.ServerStorage.Modules.AddingItems)

game.Players.PlayerAdded:Connect(function(Player)
script.Parent.MouseButton1Click:Connect(function()
print(Player.Name) – if its just plr it will print ā€˜nil’
local location = script.Parent.Parent
local pos1 = 0.5
local pos2 = 1.5
tween.out(plr, location, pos1, pos2)
add.BuyWeapons(2, Player)
end)
end)
1 Like

if that’s a server script then on the 3rd line you cant get .LocalPlayer because you can only get it through a local script so it returns Nil instead of the player