How to fix the error "attempt to index nil with name"?


  1. I’m creating a ragdoll on death system that will allow the player to respawn without the ragdoll disappearing however; I keep running into an error.

  2. image

  3. I’ve tried waiting for the child, changing where I clone the player at; I’ve tried using an if statement and nothing works.


local Ragdoll = require(game.ServerScriptService["Modules | Ragdoll"].RagdollModule)
local P = game:GetService("PhysicsService")
local c = script.Parent
local Hum = c.Humanoid
Hum.BreakJointsOnDeath = false
Hum.RequiresNeck = false

Ragdoll.RagdollFunctions["Build Rig"].Function(c)
Hum.Died:Connect(function()
	if Ragdoll.Settings.Toggles.DeathRagdoll then
		if game.Workspace:FindFirstChild(c.Name) then
			local Clone = game.Workspace:WaitForChild(c.Name):Clone()
			Clone.Name = c.Name.." Ragdoll" -- This is the line that gives an issue.
			Clone.Parent = workspace
		end
		Ragdoll.RagdollFunctions["Death Ragdoll"].Function(c,c.Name)
	end
end)


Thank you for any help you can give.

4 Likes

Generally, what the error means is: You tried to index a variable or value (basically use the dot syntax on an object. If you did object.Value you’re indexing the object with “Value”) that evaluated to nil, so what the interpreter thinks you’re doing in runtime is nil.Name.

As to why this error occurs, I assume you’re trying to clone a player’s character. The problem with this is that every player’s character has their Archivable property to true (possibly the same goes for their actual parts?) which makes the usage of the Clone function futile, because the property will make the function return nil.

And just a little note: you shouldn’t name your variables identifiers like P and c as they don’t convey an unambiguous meaning, especially when they refer to completely different things. Instead, a variable that references PhysicsService could just be named “PhysService”, and a variable that references the character could just be named “Char” or “Character”

To fix it, you can temporarily set the Archivable property of all of their instances to true, clone it, then set them back to false:

function setArchivable(char, bool)
    char.Archivable = bool
    for _, inst in ipairs(char:GetDescendants()) do
        inst.Archivable = bool
    end
end

-- when you want to clone the character:

setArchivable(Character, true)
local Clone = Character:Clone()
setArchivable(Character, false)
1 Like

I completely forgot about that feature!!! Thank you so much! Also, I usually use the Char and Character word as a variable but I rescripted this exact code 18 times… hence the lazy writing.

i was coppy but output is
Players.heckerkt007.PlayerScripts.LocalScript:2: attempt to index nil with ‘Archivable’