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.
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)
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.