Issue with printing local player name with tool

Hi, I’m not really experienced in coding, and I still have to learn, also don’t ask why I want it printed, its just for fun.

So the problem is that I’m trying to print the player name with this code
image
but when I use my tool, and execute the code, it says this, and I don’t really know where it gets the “nill” from

1 Like

LocalPlayer can be called only by a local script and not a normal script

There isn’t a parameter for Activated functions, so its printing an error because you can’t get Name from it.

attempt to index nil with "Name" is from trying to index a nil value with the index “Name”. This error comes from them trying to index the Player variable with “Name”, but Player is nil because they’re running the code from a server script where Players.LocalPlayer doesn’t exist.

Good catch though, that’s also a problem.

Oh damn, I’m so stupid. I forgot it was LOCAL!!!
Thank you guys.

1 Like

Copy and paste this into a LocalScript and it should work as intended.

local Players = game:GetService("Players") -- Gets Player service

local player = Players.LocalPlayer

local tool = script.Parent

local sound = tool.buttonclick
local neon = tool.Neon
local beam = tool.Beam2.light

local active = false

tool.Activated:Connect(function()
	if not active then -- You can use logic to check if it's false instead of 'if active == false then'
		active = false
		tool.handle.Light.Enabled = true
		neon.Material = Enum.Material.Neon -- Use Enum.Material instead of setting it by a string
		beam.Enabled = true
		sound:Play()
		print(player.Name.." turned on their flashlight")
	else
		active = false
		tool.handle.Light.Enabled = false
		neon.Material = Enum.Material.Plastic
		beam.Enabled = false
		sound:Play()
		print(player.Name.." turned off their flashlight")
	end
end)

1 Like

Make sure it’s a LocalScript, Activated has no Parameters so you should remove it. Also the tool’s name won’t appear for anyone but the client so you need to use a remoteEvent.

1 Like