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

Attempt to index nil with 'Name'?

Hi, so I have a sword shop where when you buy a weapon, the sword is cloned from replicated storage and put in the backpack. That part works fine. My problem is that the sword doesn't deal any damage but plays the animation even thought it worked fine when I put it in Starterpack. It just shows up with the error: ServerScriptService.SwordServer:30: attempt to index nil with 'Name'. I have a SwordServer script in ServerScriptService wiht a remote event named "Damage" in ReplicatedStorage.

Script inside the sword tool:

wait(1)

script.Parent.Equipped:Connect(function()
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animations.Idle)
	wait(0.1)
idle:Play()
script.Parent.Unequipped:Connect(function()
idle:Stop()
end)
end)

--//Attacking Animations\\--
local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local CanAttack = true
local Anim_Name = "Attack"
local Audio = script.Parent:WaitForChild("Handle"):WaitForChild("Swing")
local Attack_Animations = script.Parent:WaitForChild("Animations")
local Attack_Num = 1
local blade = script.Parent:WaitForChild("HeavySwordBlade")

blade.Touched:Connect(function(hit)
	if CanAttack == false then
		game.ReplicatedStorage.Damage:FireServer(hit)
	end
end)

Tool.Activated:Connect(function()
		local Animation = script.Parent.Parent.Humanoid:LoadAnimation(Attack_Animations[Anim_Name..Attack_Num])
		if CanAttack == true then
            CanAttack = false
			Animation:Play()
			Audio:Play()
			wait(1.5)
			CanAttack = true
			if Attack_Num == 1 then
				Attack_Num = Attack_Num + 1
				else if Attack_Num == 2 then
					Attack_Num = 1
				end
			end
		end
end)

Tool.Deactivated:Connect(function()
	script.Parent.Equipped:Connect(function()
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animations.Idle)
	wait(0.1)
idle:Play()
script.Parent.Unequipped:Connect(function()
idle:Stop()
end)
end)
end)

SwordServerScript:

local CanAttack = true

local weaponsAndDamages = {}
weaponsAndDamages ["StarterSword"] = 45
weaponsAndDamages ["Hammer"] = 85
weaponsAndDamages ["Scythe"] = 55
weaponsAndDamages ["TripleBladeScythe"] = 220
weaponsAndDamages ["HeavySword"] = 160
weaponsAndDamages ["Dagger"] = 35
weaponsAndDamages ["GoldenDagger"] = 75
weaponsAndDamages ["Spear"] = 60
weaponsAndDamages ["NightmareBlade"] = 80

local weaponsAndRates = {}
weaponsAndRates ["StarterSword"] = 0.75
weaponsAndRates ["Hammer"] = 1
weaponsAndRates ["Scythe"] = 0.75
weaponsAndRates ["TripleBladeScythe"] = 1.3
weaponsAndRates ["HeavySword"] = 1.5
weaponsAndRates ["Dagger"] = 0.6
weaponsAndRates ["GoldenDagger"] = 0.8
weaponsAndRates ["Spear"] = 0.85
weaponsAndRates ["NightmareBlade"] = 0.75

local lastAttack = {}
game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(player, hit)
    local weapon = player.Character:FindFirstChildWhichIsA("Tool") -- obtain the sword of the player attacking
    if hit.Parent:FindFirstChild("Humanoid") ~= nil and player.Character ~= nil then
        if hit.Parent.Humanoid.Health > 0 and player.Character.Humanoid.Health > 0 then -- check if the users are alive, we don't want dead players dealing damage, or players dealing damage to dead players.
            if lastAttack[player.Name] == nil or tick() - lastAttack[player.Name] > weaponsAndRates[weapon.Name] then
                if (hit.Parent.HumanoidRootPart.Position-player.Character.HumanoidRootPart.Position).magnitude < 40 then -- went with 40 just in case of serious latency. You can tweak this if wanted.
                    lastAttack[player.Name] = tick()
                    if weapon ~= nil then -- make sure it's not nil
                  		hit.Parent.Humanoid:TakeDamage(weaponsAndDamages[weapon.Name])
                        weapon.Handle.Swing:Play() -- You want to play the hit noise on the sword of the player who is attacking, not the sword in the StarterPack.
                    end
                end
            end
        end
    end
end)

game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(player, hit)
	local weapon = player.Character:FindFirstChildWhichIsA("Tool")
	 if hit.Parent:FindFirstChild("Humanoid") ~= nil and player.Character ~= nil then
        if hit.Parent.Humanoid.Health > 0 and player.Character.Humanoid.Health > 0 then

local damage = (weaponsAndDamages[weapon.Name]) -- we create a variable which stores the damage inside of it, i randomized the damage between 5 and 20
				
			if CanAttack == true then
            CanAttack = false
				local indicator = game:GetService("ReplicatedStorage"):FindFirstChild("DmgIndicator") -- we put the dmgindicator which in in the replicatedstorage inside a variable
				if indicator then -- we check whether the indicator exists, if so then...
					local gui = indicator:Clone() -- we clone it and store the cloned version inside a variable
					gui.Parent = hit.Parent:FindFirstChild("HumanoidRootPart") -- we put the cloned version inside the enemy's rootpart
					gui.Damage.Text = "-"..tostring(damage) -- we set the damage text to the damage we'll deal					
					gui.StudsOffset = Vector3.new(math.random(-2, 2), math.random(-2, 2), math.random(-2, 2)) -- we position our gui randomly on the enemy's body (between -2 and 2 studs)
					wait(weaponsAndRates[weapon.Name])
					game.Debris:AddItem(gui,0.1) 
					CanAttack = true -- we will destroy that indicator after 2 seconds
			end
		end
		end
		end
end)

The problem is in line 30 of SwordServerScript

1 comment

0

wth is weapon.Name

1 answer

You need to first check if the weapon variable is nil or not.

If you're using FindFirstChild() or anything else like that, you'll need to always do that, because if it finds nothing, it returns it as nil, therefore, there is no Name property.

1 comment

0

So how would I do that?

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