Sometimes works, Sometimes not

Hello!

I’m making a zombie survival game and i made that for the survivors all of the people in the ghost team is invisible.

It works but sometimes it breaks completely.

while wait(0.5) do
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == game.Teams.Dead then
			for i,charparts in pairs(v.Character:GetChildren()) do
				if charparts:IsA("Part") then
					if isDead == true then
						charparts.Transparency = 0.5
						if charparts.Name == "HumanoidRootPart" then
							charparts.Transparency = 1
						end
					else
						charparts.Transparency = 1
						charparts.CanCollide = false
					end
				end
			end
		end
	end
end

Line 4

I really need help.

2 Likes

use player.CharacterAdded instead
bye
:sweat_smile:
https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded

1 Like

I believe the reason your script is breaking is because you aren’t checking whether player is nil, or if the player’s character is nil. Based on your situation, the above reply makes sense in that you can use event based structure to make your code better as well.

In line 4, you are getting the character’s children, but you can’t get the children of something that is nil. (A character is nil when a player is in the game but their character isn’t spawned.)

1 Like
local players = game:GetService("Players")
local isDead = nil

players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		if player.Team.Name == "Dead" then
			for _, charPart in ipairs(character:GetChildren()) do
				if isDead then
					charPart.Transparency = 0.5
					if charPart.Name == "HumanoidRootPart" then
						charPart.Transparency = 1
					end
				else
					charPart.Transparency = 1
					charPart.CanCollide = false
				end
			end
		end
	end)
end)