Code question about palyers live and die

With this code, how can I make the players who die cannot see the living?
Could someone help me with that please :frowning:

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		print("i am alive")
		character:WaitForChild("Humanoid").Died:Connect(function()
			print(player.Name .. " has died!")
		end)
	end)
end)

Hi, for simplicity check out this article Make Players locally invisible

In the answer is code that you should be able to paste into the died function, something like

local hide = true --Whether to hide, can be manipulated

local orignalTransparency = {}--Store original transparency

local function checkPart(part) --This will set the transparency of every part
	for a,b in pairs(part:GetChildren()) do 
		checkPart(b) --If there are more parts, also set the transparency for them too
	end
	if part:IsA("BasePart") or part:IsA("Decal") then--If the object is a part or decal, set it's transparency. (Face is a decal)
		if hide then --If we want to hide players, make their parts invisible
			if not orignalTransparency[part] then--Store data for later if we want to undo the effect
				orignalTransparency[part] = part.Transparency
			end
			part.Transparency = 1
		else --If we don't want to hide, set the transparency back to normal
			if orignalTransparency[part] then --If we stored it
				part.Transparency = orignalTransparency[part] --Set it
				orignalTransparency[part] = nil --Remove reference because we do not need it anymore, prevents memory leaks
			end
		end
	end
end

local players = game:GetService("Players")
local localPlayer = players.LocalPlayer

game:GetService("RunService").RenderStepped:Connect(function() --Every frame, set transparency
	--Iterate through players to make them invisible
	for i,v in pairs(players:GetPlayers()) do --Go through every player
		if v ~= localPlayer then --This makes sure we don't make ourself invisble. Remove this for everyone.
			local char = v.Character
			if char then--If the character exists
				checkPart(char)
			end
		end
	end
	--Prevent memory leaks, if a part does not exist anymore we do not need to reference it
	for part,trans in pairs(orignalTransparency) do
		if not part then
			orignalTransparency[part] = nil
		elseif not part.Parent then
			orignalTransparency[part] = nil
		end
	end
end)

while true do --Testing functionality
	hide = true
	wait(2.5)
	hide = false
	wait(2.5)
end

You will have to modify it to fit, but it should work. Good luck!

The code didnt worked idk why :frowning:

serverscript

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		print("i am alive")
		character:WaitForChild("Humanoid").Died:Connect(function()
			print(player.Name .. " has died!")
			
			--code inside
			
			local hide = true --Whether to hide, can be manipulated

			local orignalTransparency = {}--Store original transparency

			local function checkPart(part) --This will set the transparency of every part
				for a,b in pairs(part:GetChildren()) do 
					checkPart(b) --If there are more parts, also set the transparency for them too
				end
				if part:IsA("BasePart") or part:IsA("Decal") then--If the object is a part or decal, set it's transparency. (Face is a decal)
					if hide then --If we want to hide players, make their parts invisible
						if not orignalTransparency[part] then--Store data for later if we want to undo the effect
							orignalTransparency[part] = part.Transparency
						end
						part.Transparency = 1
					else --If we don't want to hide, set the transparency back to normal
						if orignalTransparency[part] then --If we stored it
							part.Transparency = orignalTransparency[part] --Set it
							orignalTransparency[part] = nil --Remove reference because we do not need it anymore, prevents memory leaks
						end
					end
				end
			end

			local players = game:GetService("Players")
			local localPlayer = players.LocalPlayer

			game:GetService("RunService").RenderStepped:Connect(function() --Every frame, set transparency
				--Iterate through players to make them invisible
				for i,v in pairs(players:GetPlayers()) do --Go through every player
					if v ~= localPlayer then --This makes sure we don't make ourself invisble. Remove this for everyone.
						local char = v.Character
						if char then--If the character exists
							checkPart(char)
						end
					end
				end
				--Prevent memory leaks, if a part does not exist anymore we do not need to reference it
				for part,trans in pairs(orignalTransparency) do
					if not part then
						orignalTransparency[part] = nil
					elseif not part.Parent then
						orignalTransparency[part] = nil
					end
				end
			end)

			while true do --Testing functionality
				hide = true
				wait(2.5)
				hide = false
				wait(2.5)
			end
			
			--end code inside
			
		end)
	end)
end)

How do you plan on using this in your game?

If your game is based around it you could have a server sided BoolValue called ā€œAliveā€ or something then when the player dies have it turn everyone with ā€œAliveā€ equal to true invisible.

I need to do something similar to vampire diarie that when you die you appear on your body and only those who are dead can see each other, how could something like this be done? Sorry for so many questions

We could make the system work, so that only alive people can see alive and only dead people can see the dead. Let me throw together a script for it really quick.

Thatā€™s exactly what I need ā€¦ thank you very much really, sorry for the inconvenience

Code didnā€™t work because it needs to be in a local script :confused: Did you read the article? It has the answer. . .

yes i did and didnā€™t worked, i put my code inside the gui and i also download that project and didnā€™t worked for me (test server 2 players)

You said

Needs to be a local script. . . Or it might just not work, didnā€™t check it, just saw the article :slightly_smiling_face:

i did it and still no workingā€¦ check it

pppp3

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		print("i am alive")
		character:WaitForChild("Humanoid").Died:Connect(function()
			print(player.Name .. " has died!")

			--code inside

			local hide = true --Whether to hide, can be manipulated

			local orignalTransparency = {}--Store original transparency

			local function checkPart(part) --This will set the transparency of every part
				for a,b in pairs(part:GetChildren()) do 
					checkPart(b) --If there are more parts, also set the transparency for them too
				end
				if part:IsA("BasePart") or part:IsA("Decal") then--If the object is a part or decal, set it's transparency. (Face is a decal)
					if hide then --If we want to hide players, make their parts invisible
						if not orignalTransparency[part] then--Store data for later if we want to undo the effect
							orignalTransparency[part] = part.Transparency
						end
						part.Transparency = 1
					else --If we don't want to hide, set the transparency back to normal
						if orignalTransparency[part] then --If we stored it
							part.Transparency = orignalTransparency[part] --Set it
							orignalTransparency[part] = nil --Remove reference because we do not need it anymore, prevents memory leaks
						end
					end
				end
			end

			local players = game:GetService("Players")
			local localPlayer = players.LocalPlayer

			game:GetService("RunService").RenderStepped:Connect(function() --Every frame, set transparency
				--Iterate through players to make them invisible
				for i,v in pairs(players:GetPlayers()) do --Go through every player
					if v ~= localPlayer then --This makes sure we don't make ourself invisble. Remove this for everyone.
						local char = v.Character
						if char then--If the character exists
							checkPart(char)
						end
					end
				end
				--Prevent memory leaks, if a part does not exist anymore we do not need to reference it
				for part,trans in pairs(orignalTransparency) do
					if not part then
						orignalTransparency[part] = nil
					elseif not part.Parent then
						orignalTransparency[part] = nil
					end
				end
			end)

			while true do --Testing functionality
				hide = true
				wait(2.5)
				hide = false
				wait(2.5)
			end

			--end code inside

		end)
	end)
end)

oH OH SORRY!!! ITS WORKED lol sorry about that i was bad :frowning:

Haha, np. I find myself working in circles all the time, checking something then realizing I did the wrong thing. I totally get it :slight_smile: If it actually did work, you might want to mark it as the solution, so future people can find the answer. Good luck with your game!

Exactly the same thing happens to me ā€¦ I have been working with lua for two weeks and many times I enter the circle but I have acquired some experience ā€¦ you have to keep learning! Thanks for everything and much success to you too in everything!

1 Like