Receiving error for no reason

I am recieving the error: 00:32:42.867 Workspace.Testing.Chamber.RespawnScript:8: attempt to index nil with 'Parent' - Server - RespawnScript:8

-on my script, and I don’t know how to fix it.

How can I fix this and also optimize my script so that it is shorter?

Script
local proxPrompt = script.Parent.Button.Red.Prompt

proxPrompt.Triggered:Connect(function()
	
	local dummy = script.Parent.Dummies.TestDummy
		
	local dummyClone = dummy:Clone()
	dummyClone.Parent = game.ReplicatedStorage.Dummies
	dummyClone.Humanoid.Health = dummyClone.Humanoid.MaxHealth
	
	for _, ragdoll in pairs(script.Parent.Dummies:GetChildren()) do
		if ragdoll:IsA("Model") and ragdoll:FindFirstChild("RagdollHumanoid") then
			ragdoll:Destroy()
		end
	end
	
	for _, oldDummy in pairs(script.Parent.Dummies:GetChildren()) do
		if oldDummy:IsA("Model") and oldDummy:FindFirstChild("Humanoid") then
			oldDummy:Destroy()
		end
	end
	
	dummyClone.Parent = script.Parent.Dummies
	
end)

Try calling the dummy variable outside of the Triggered event, and also use WaitForChild.

Also, as stated by @DasKairo , you are probably destroying the main ‘dummy’ that you use to clone, which is what is causing this error. Try renaming the clones and make an if statement to check their names before destroying the cloned dummies

local proxPrompt = script.Parent.Button.Red.Prompt
local dummy = script.Parent:WaitForChild("Dummies"):WaitForChild("TestDummy")

proxPrompt.Triggered:Connect(function()	
	local dummyClone = dummy:Clone()
	dummyClone.Parent = game.ReplicatedStorage.Dummies
	dummyClone.Humanoid.Health = dummyClone.Humanoid.MaxHealth
	dummyClone.Name = "CloneDummy" -- changes the clone name

	for _, ragdoll in pairs(script.Parent.Dummies:GetChildren()) do
		if ragdoll:IsA("Model") and ragdoll:FindFirstChild("RagdollHumanoid") then
			ragdoll:Destroy()
		end
	end
	
	for _, oldDummy in pairs(script.Parent.Dummies:GetChildren()) do
		if oldDummy:IsA("Model") and oldDummy.Name == "CloneDummy" and oldDummy:FindFirstChild("Humanoid")  then
			oldDummy:Destroy()
		end
	end
	
	dummyClone.Parent = script.Parent.Dummies
	
end)

I think you are accidentally deleting the Dummy when using the for loop

1 Like

No errors in reproduction environment. This may probably be caused by the following:

  • The Dummy, if placed under Workspace or any object that allows geometry to be rendered, has fallen to the void.
  • The Dummy was removed by another script.
  • You simply don’t have the dummy, or the path is incorrect.

For scenario 1: Place it under ServerStorage (if your code is in a normal Script, not LocalScript).

For scenario 2: Keep a copy of the Dummy somewhere (recommended place: ServerStorage) and use that to clone a new Dummy instead.

For scenario 3: Type the (correct) path to it in your script.


Extra

Dummies are not inserted into game.ReplicatedStorage. Use the ServerStorage instead.