Any clue how to make this not error?

i just made a quick respawn script for a dummy and it works fine but gives this error

Workspace.Dummy.RespawnScript:2: attempt to index nil with 'Humanoid’
Heres the script

while wait() do
	if script.Parent.Humanoid.Health == 0 then
		local dummy = game.ReplicatedStorage.Dummy
		local cd = dummy:Clone()
		cd.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame
		cd.Parent = game.Workspace
		script.Parent:Destroy()
	end
end

any idea why it errors and how to fix it?

Assuming that script.Parent is Character of the dummy, try doing:

local char = script.Parent or script.Parent.Parent.CharacterAdded:Wait()

The character instance has to load in workspace when player/dummy is instanced, and the script may run before it, so we’re polling to wait for the character and continue code.

Not sure if i used the code correctly but it errors now with
Humanoid is not a valid member of Model "Dummy"
here’s the code now

local Dummy = script.Parent or script.Parent.Parent.CharacterAdded:Wait()

while wait() do
	if Dummy.Humanoid.Health == 0 then
		local dummy = game.ReplicatedStorage.Dummy
		local cd = dummy:Clone()
		cd.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame
		cd.Parent = game.Workspace
		script.Parent:Destroy()
	end
end

Try this:

if Dummy:WaitForChild("Humanoid").Health == 0 then

If that doesn’t work maybe try this:

local Dummy = script.Parent or script.Parent.Parent.CharacterAdded:Wait()

local Humanoid = Dummy:WaitForChild("Humanoid")

while wait() do
	if Humanoid.Health == 0 then
		local dummy = game.ReplicatedStorage.Dummy
		local cd = dummy:Clone()
		cd.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame
		cd.Parent = game.Workspace
		script.Parent:Destroy()
	end
end

No longer got the not a valid member error but now have a infinite yield error
Infinite yield possible on 'Dummy:WaitForChild(“Humanoid”)'
heres the code

local Dummy = script.Parent or script.Parent.Parent.CharacterAdded:Wait()

while wait() do
	if Dummy:WaitForChild("Humanoid").Health == 0 then
		local dummy = game.ReplicatedStorage.Dummy
		local cd = dummy:Clone()
		cd.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame
		cd.Parent = game.Workspace
		script.Parent:Destroy()
	end
end

tried the second bit of code and it errors with
Workspace.Dummy.RespawnScript:9: attempt to index nil with 'HumanoidRootPart’

Try renaming Dummy, the script believes you are referencing the actual Dummy itself.

local chr = script.Parent or script.Parent.Parent.CharacterAdded:Wait()

while wait() do
	if chr.Humanoid.Health == 0 then
		local dummy = game.ReplicatedStorage.Dummy
		local cd = dummy:Clone()
		cd.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame
		cd.Parent = game.Workspace
		script.Parent:Destroy()
	end
end

Hope this works

Try this instead

Summary

Charrrs

1 Like

Usually when you get the “index nil with” error that means that the object that it’s saying you’re indexing nil with doesn’t exist. In this case the Dummy. I’m assuming it’s meaning the Dummy clone.

As for fixing it I’m not sure. Sorry.

no longer errors but no longer works aswell

switched it to just D and it still gets inf yielded

local D = script.Parent or script.Parent.Parent.CharacterAdded:Wait()

while wait() do
	if D:WaitForChild("Humanoid").Health == 0 then
		local dummy = game.ReplicatedStorage.Dummy
		local cd = dummy:Clone()
		cd.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame
		cd.Parent = game.Workspace
		script.Parent:Destroy()
	end
end

try this one, it got inf yileded because script.Parent.Parent.CharacterAdded:Wait() is I believe workspace

local char = script.Parent
local hum = char:WaitForChild("Humanoid")

hum.HealthChanged:Connect(function()
	if hum.Health == 0 then
		task.wait(1) -- you can delete this
		local dummy = game:GetService("ReplicatedStorage"):WaitForChild("Dummy"):Clone()
		dummy.Parent = game.Workspace
		dummy:WaitForChild("HumanoidRootPart").CFrame = char:WaitForChild("HumanoidRootPart").CFrame
		script.Parent:Destroy()
	end
end)
2 Likes

If you have this script in the cloned dummy your destroying that dummy aswell

1 Like
local rs = game:GetService("ReplicatedStorage")

local dummy = workspace.Dummy
local humanoid = dummy.Humanoid

while wait() do
	if humanoid.Health <= 0 then
		dummy:Destroy()
		
		local dummy = game.ReplicatedStorage.Dummy
		local cd = dummy:Clone()
		
		cd.HumanoidRootPart.CFrame = dummy.HumanoidRootPart.CFrame
		cd.Parent = game.Workspace
	end
end

Use this in server script service

in just a normal script in server script service or in dummy in server script service?

Normal script in server script service

Also remove the other script from the dummy

I did this and it errors with
HumanoidRootPart is not a valid member of ServerScriptService "ServerScriptService"

what should i change script.Parent to?

dummy.HumanoidRootPart.CFrame

alright it respawns, but it only respawns once and the name looks a little funny
image
Doesnt error though :smiley: :+1:

also just to add onto it, it starts lagging out the game for some reason

Because is a loop and start spawning a lot of dummys when the if condition is met