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
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
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
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
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.
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