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

What is wrong with this function?

Hi. New to tables, this is one of my first attempts.

As far as I know, there aren't any problems in syntax, so I'm not sure why this script does not work.

If anyone can point me in the right direction, I'd be very grateful :)

function ghost()
		local parts = game.Workspace:FindFirstChild("Player1")
		for i,v in pairs (parts:GetChildren()) do
			v.Transparency = 0.5
	end
end

if game.Workspace:FindFirstChild("Player1").Humanoid.Health == 0 then
	ghost()
end

Note: This is just a test on the ROBLOX Studio character. I don't expect this to work in-game.

2 answers

function ghost()
        local parts = game.Workspace:FindFirstChild("Player1")
        for i,v in pairs (parts:GetChildren()) do
            if v:IsA("Part") then --If the child is a classname Part, then it makes it transparent, because you have other things like the Humanoid, which you can't make transparent, so it will error.
		v.Transparency = 0.5
	    end
    end
end

if game.Workspace:WaitForChild("Player1").Humanoid.Health == 0 then --It has to be WaitForChild because the player is not in the game yet, so it is waiting until he is
    ghost()
end

Also, the player will die, turn ghost, then respawn in this script, just fyi.

The issue here is that your script will only check if the player is dead once, instantly when it is executed.

function ghost()
        local parts = game.Workspace:FindFirstChild("Player1")
        for i,v in pairs (parts:GetChildren()) do
            v.Transparency = 0.5
    end
end

game.Workspace:FindFirstChild("Player1").Died:connect(ghost)

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