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)