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

The Model Humanoid must go transparent slowly once 0 health. What is wrong with the script?

23:21:13.612 - Workspace.Model.Tool.Part.Script:23: attempt to index nil with 'Destroy'

The parts of the Dummy Model are suppose to slowly turn transparent when health reaches 0. But it does not work. The error prints nil to Destroy, but the script for the parts disappearing works.

Part = script.Parent
local Dummy = game.Workspace.Dummy
local Players = game.Players.LocalPlayer
local Debounce = false 
local Model = game.Workspace.Dummy
local Killable = game.Workspace:GetChildren()

Part.Touched:Connect(function(hit)
		local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		if Dummy and Humanoid and Debounce == false then 
			Debounce = true
			Humanoid.Health = Humanoid.Health - 25
			
			wait(0.1)
		    Debounce = false
			
			
			if Humanoid.Health == 0 then
				for tranparency = 0, 1, 0.1 do
				Model:GetChildren().Transparency = 1
				end
				wait(5)
				Humanoid.Parent:Destroy()
			end
		
		end
end)
	

1 answer

GetChildren() returns an array, so you need to loop through it and turn the transparency off.

I changed the code a bit by using :GetDescendants(), if you have parts nested under parts under the dummy model, this is a better use.

Part = script.Parent
local Dummy = game.Workspace.Dummy
local Players = game.Players.LocalPlayer
local Debounce = false 
local Model = game.Workspace.Dummy
local Killable = game.Workspace:GetChildren()

Part.Touched:Connect(function(hit)
        local Humanoid = hit.Parent:FindFirstChild("Humanoid")
        if Dummy and Humanoid and Debounce == false then 
            Debounce = true
            Humanoid.Health = Humanoid.Health - 25
            wait(0.1)
            Debounce = false
			local partsOfModel = Model:GetDescendants()
            if Humanoid.Health == 0 then
                for i, v in pairs(partsOfModel)do
					v.Transparency = 1
                end
                wait(5)
                Humanoid.Parent:Destroy()
            end

        end
end)

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