Attempt to index nil with 'Name'

I am making a gun system and whenever I shoot sometimes it comes back with this error. Players.918_M.Backpack.Glock 17.Main.ServerHandler:55: attempt to index nil with ‘Name’ - Server - ServerHandler:55. https://gyazo.com/4b9115e850335f59ef0ad45d5871cf45 If someone could help as I am not sure but I think I know.

Can we see the script and the line that’s giving the error.

1 Like

Provide us with a script so we better understand your problem

The reason for that error is because what you used .Name on can’t be detect by your script.

I’m really hoping you just forgot to attach the script, instead of thinking we’re pyschic and can see the same thing you see on your screen.


I’m assuming you tried this:

print(game.Players.LocalPlayer.Name, "shot a gun.")

The server doesn’t have a local player, you need to get the player like this:

local tool = script.Parent
local player = game.Players:GetPlayerFromCharacter(tool.Parent)
3 Likes

I wasn’t sure so let me attempt this, and I’ll get back to you.

please format this correctly by using the code button and put a comment on the line with the issue so we can help

--// 918_M
local tool = script.Parent.Parent
local reloadtime = script.Parent.ReloadConfig.ReloadTime
local ammo = script.Parent.ReloadConfig.Ammo
local isReloading = false

local emptysound = script.Parent.Reload.bulletsound

--//weld obviously
for _, v in pairs(tool:GetDescendants()) do
	if v:IsA("BasePart") then
		local weld = Instance.new("ManualWeld")
		weld.C0 = tool.Handle.CFrame:Inverse() * v.CFrame
		weld.Part0 = tool.Handle
		weld.Part1 = v
		weld.Parent = v
	end
end

for _, v in pairs(tool:GetDescendants()) do
	if v:IsA("BasePart") then
		v.Anchored = false
	end
end

--//server fire function
script.Parent.Fire.OnServerEvent:Connect(function(player, mouseHit)
	if isReloading == false then
		if ammo.Value <= 0 then
			script.Parent.Reload.OnServerEvent:Connect(function()
				coroutine.wrap(function()
					isReloading = true
					script.Parent.Reload.sound:Play()
					task.wait(reloadtime.Value)
					ammo.Value = ammo.Parent.MaxAmmo.Value
					isReloading = false
				end)()
			end)
		else
			ammo.Value -= 1
			local ray = Ray.new(tool.Shoot.CFrame.p, (mouseHit.p - tool.Shoot.CFrame.p).unit*100)
			local hit, position = workspace:FindPartOnRay(ray, tool.Parent)
			local distance = (position - tool.Shoot.CFrame.p).magnitude
			local part = Instance.new("Part")
			part.Anchored = true
			part.CanCollide = false
			part.Transparency = 1
			part.BrickColor = BrickColor.new("Light Brown")
			part.Material = Enum.Material.Neon
			part.Size = Vector3.new(0,0,distance)
			part.CFrame = CFrame.new(position, tool.Shoot.CFrame.p) * CFrame.new(0,0,-distance/2)
			part.Parent = workspace
			task.wait(0.1)

			Line 55:if string.lower(hit.Name) == ("glass") then
				print("glass")
				local newAudio = game.ReplicatedStorage.clonedItems.sound:Clone();
				newAudio.Parent = hit;
				newAudio:Play();
				hit:Destroy();
			end;

			Here--//actually damage people if they get shot
			local hitCharacter = hit.Parent
			if not hitCharacter then return end
			if hitCharacter:IsA("Accessory") then
				hitCharacter.Parent.Humanoid:TakeDamage(25)
			end

			if hit then
				if hit.Parent then
					local humanoid = hit.Parent:FindFirstChild("Humanoid")
					if humanoid then
						if hit.Name == "Head" then
							hit.Parent.Humanoid:TakeDamage(35) --//may change?
							script.Parent.Hit:FireClient(player)
							print("head")
						else
							hit.Parent.Humanoid:TakeDamage(25)
							script.Parent.Hit:FireClient(player)
							if hit.Name == "Torso" then
							end
						end
					end
				end
			end
			--//gun effects
			local function randomMuzzle()
				local chance = math.random(1,6)
				if chance == 4 then
					tool.Shoot.Muzzle.Enabled = true
					tool.Shoot.MuzzleLight.Enabled = true
					task.wait(0.05)
					tool.Shoot.Muzzle.Enabled = false
					tool.Shoot.MuzzleLight.Enabled = false
				end
			end

			randomMuzzle()

			tool.Shoot.fire:Play()

			tool.bolt1.Transparency = 1
			tool.bolt2.Transparency = 0
			task.wait(0.05)
			tool.bolt1.Transparency = 0
			tool.bolt2.Transparency = 1

			if ammo.Value <= 0 then
				tool.bolt1.Transparency = 1
				tool.bolt2.Transparency = 0
				script.Parent.Reload.OnServerEvent:Connect(function()
					tool.bolt1.Transparency = 0
					tool.bolt2.Transparency = 1
				end)
			end

			coroutine.wrap(function()
				tool.Shoot.SmokeEffect.Enabled = true
				task.wait(0.1)
				tool.Shoot.SmokeEffect.Enabled = false
			end)()
			game.Debris:AddItem(part, 0.1)
		end
	end

end)

I’ve modified it and arranged some structures
Try it:

local tool = script.Parent.Parent
local reloadtime = script.Parent.ReloadConfig.ReloadTime
local ammo = script.Parent.ReloadConfig.Ammo
local isReloading = false

local emptysound = script.Parent.Reload.bulletsound

-- Welding parts
for _, v in pairs(tool:GetDescendants()) do
	if v:IsA("BasePart") then
		local weld = Instance.new("ManualWeld")
		weld.C0 = tool.Handle.CFrame:Inverse() * v.CFrame
		weld.Part0 = tool.Handle
		weld.Part1 = v
		weld.Parent = v
	end
end

for _, v in pairs(tool:GetDescendants()) do
	if v:IsA("BasePart") then
		v.Anchored = false
	end
end

-- Server fire function
script.Parent.Fire.OnServerEvent:Connect(function(player, mouseHit)
	if isReloading then
		return
	end

	if ammo.Value <= 0 then
		return
	end

	ammo.Value -= 1

	-- Raycasting
	local ray = Ray.new(tool.Shoot.CFrame.p, (mouseHit.p - tool.Shoot.CFrame.p).Unit * 100)
	local hit, position = workspace:FindPartOnRay(ray, tool.Parent)
	local distance = (position - tool.Shoot.CFrame.p).Magnitude

	-- Creating bullet part
	local bullet = Instance.new("Part")
	bullet.Name = "Bullet"
	bullet.Transparency = 1
	bullet.BrickColor = BrickColor.new("Light Brown")
	bullet.Material = Enum.Material.Neon
	bullet.Size = Vector3.new(0, 0, distance)
	bullet.CFrame = CFrame.new(position, tool.Shoot.CFrame.p) * CFrame.new(0, 0, -distance / 2)
	bullet.Parent = workspace
	game.Debris:AddItem(bullet, 0.1)

	if hit and string.lower(hit.Name) == "glass" then
		print("glass")
		local newAudio = game.ReplicatedStorage.clonedItems.sound:Clone()
		newAudio.Parent = hit
		newAudio:Play()
		hit:Destroy()
	end

	-- Damage players if hit
	local hitCharacter = hit and hit.Parent
	if hitCharacter and hitCharacter:IsA("Accessory") then
		hitCharacter.Parent.Humanoid:TakeDamage(25)
	end

	if hitCharacter then
		local humanoid = hitCharacter:FindFirstChild("Humanoid")
		if humanoid then
			if hit.Name == "Head" then
				hitCharacter.Humanoid:TakeDamage(35)
				script.Parent.Hit:FireClient(player)
				print("head")
			else
				hitCharacter.Humanoid:TakeDamage(25)
				script.Parent.Hit:FireClient(player)
			end
		end
	end

	-- Gun effects
	local function randomMuzzle()
		local chance = math.random(1, 6)
		if chance == 4 then
			tool.Shoot.Muzzle.Enabled = true
			tool.Shoot.MuzzleLight.Enabled = true
			wait(0.05)
			tool.Shoot.Muzzle.Enabled = false
			tool.Shoot.MuzzleLight.Enabled = false
		end
	end

	randomMuzzle()

	tool.Shoot.fire:Play()

	tool.bolt1.Transparency = 1
	tool.bolt2.Transparency = 0
	wait(0.05)
	tool.bolt1.Transparency = 0
	tool.b

the error is emitted because the ray didn’t find any part. from tool.Shoot.CFrame.p to (MouseHit.p - tool.Shoot.CFrame.p).unit * 100. pretty sure u meant tool.Shoot.CFrame.p + (MouseHit.p - tool.Shoot.CFrame.p).unit * 100. also add hit checks like @CuboidalBrake06 did.

Thank you for the assistance, have a goodnight

Hm now it won’t reload with no error

pretty sure that’s because like i said it can’t find the part using Ray.new (idk what u mean, are you having a different problem?)

No problem m8
Have a goodnight too :wink:

yes, having a diff issue. i fixed the first one cuz he helped

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.