(Solved By YXFrosty) Infection system

Hello Iam Sam
I tried to make an infection system
After the player Touched the 008 They will turn into Zombie
I made a Folder called Infected so After they turned into zombie they will be children of that folder
I did that to make it easier finding who is infected
So if a player Touched a children of infected folder (aka Zombie)
They will turn into zombie and be children of that folder too

Doesnt have error
But the Code doesnt work

Basically
Line 1 will local the infected folder
Line 2 to 5 will define if a something touched infected children
Line 6 if something touched infected is a player
Line 7 to 21 turn them to zombie ( i mean turn them to green)
Line 22 Local Zombie = character that hit infected
line 23 Make it be a child of infected too
line 24 If the character touched it already a child of infected then
25 print He/She already a zombie

For more information You can reply me to know

Thanks if anyone is trying to help me

1 Like

try updating the for loop every time a child is added to the folder so basically make the for loop into a function and do the function every time a child added cuz your code only checks once for the touch event

1 Like

Isn’t the local Zombie Hit.Parent.Parent refers to the Workspace?

1 Like

it still doesnt work if i write Hit.Parent

1 Like

What is the error that the output give?

Edit: You should loop the whole entire thing or else it will just check when you start testing.

2 Likes

Sorry for a late reply But
Output doesnt have any error
How to loop it ?
Use while true do while wait do or anything ?
Can u give an Example or code Sample
Thanks

1 Like

You can’t use touched events for models. So instead do:

child.HumanoidRootPart.Touched:Connect(function(hit)

end)

Also, to make your code more readable, try indenting the code correctly.

1 Like

Good idea i will try that thanks

1 Like

Still not working it doesnt turn another into ZOmbie after i touched them

1 Like

Is there anything in the infected folder?

1 Like

So There is nothing Inside Studio
But After a player touched the 008 SCP it will be child of that folder

1 Like

for v, child in pairs(Infected:GetDescendants()) do
if child:IsA(“BasePart”) then
child.Touched:Connect(function(hit)

Maybe??

Also is this for a infection game or scp game…?

the :Connect event fires without looping so it doesn’t need a loop

Edit : Checking for new things into the infected folder needs loop
2nd Edit: Or make a child added function

1 Like

Just noticed this but that elseif wouldn’t work as well. It’d check if humanoid first

Sorry i dont get what u mean ?

instead of making the code long and redundant, just loop through the base parts in the body and set the color that way. since you are changing the color of every body part to the same color

That’s a good Idea. I didn’t thought about that.

What does exactly don’t work? You should do something like:

if hit:IsDescendantOf(Infected) or hit.Parent.Parent == Infected then
	print("Already zombie")
else
	hit.Parent.Parent = Infected
end
1 Like

Maybe do this:

local Infected = game.Workspace.Infected
local DefaultWait = 0.4

local function GoInfected(BodyColor) --Infected function
	BodyColor.HeadColor = BrickColor.new("Lime green")
	wait(DefaultWait)
	BodyColor.LeftArmColor = BrickColor.new("Lime green")
	wait(DefaultWait)
	BodyColor.RightArmColor = BrickColor.new("Lime green")
	wait(DefaultWait)
	BodyColor.LeftLegColor = BrickColor.new("Lime green")
	wait(DefaultWait)
	BodyColor.RightLegColor = BrickColor.new("Lime green")
	wait(DefaultWait)
	BodyColor.TorsoColor = BrickColor.new("Lime green")
end

--Get infected People
while wait() do
	for v, child in pairs(Infected:GetChildren()) do
		--Get infected Part
		for _, part in ipairs(child:GetDescendants()) do
			--Infected Script
			part.Touched:Connect(function(hit)
				print("Touched")
				if hit.Parent:FindFirstChild("Humanoid") then
					wait(0.8)

					local BodyColors = hit.Parent:FindFirstChild("Body Colors")

					GoInfected(BodyColors)

					local Zombie = hit.Parent

					if Zombie.Parent == Infected then
						print("Already Infected")
					else
						Zombie.Parent = Infected
					end
				end
			end)
		end
	end
end

This could be more optimized and cleaner, but I just got back to scripting. Also I haven’t tested this.

1 Like

You should be applying the touched event to every body part of the zombie or just the arms. It’ll be expensive, but you can attach the same touched function you made to apply to all descendants of each child in the first loop. That way, for each child, and each of their body parts, it applies the infection logic.

Code Example:

for _, zombie in pairs (AllZombies:GetChildren()) do
   for _, zombieBodyPart in pairs (zombie:GetDescendants()) do
      if zombieBodyPart:IsA("BasePart") then
         zombieBodyPart.Touched:Connect(function()
            -- put infection logic here
         end)
      end
   end
end

Alternatively, you can also just attach the infection logic to the zombie’s arms, assuming they are humanoids, which would be more efficient

Code Example:

local function infect()
-- infection logic
end

for _, zombie in pairs (AllZombies:GetChildren()) do
   local zombieLeftArm = zombie:FindFirstChild("LeftArm")
   local zombieRightArm = zombie:FindFirstChild("RightArm") 

   if zombieLeftArm and zombieRightArm then -- do both the the left and right arms exist?
      zombieLeftArm.Touched:Connect(infect)
      zombieRightArm.Touched:Connect(infect)
   end
end

I hope this helps out!

1 Like