Infection system

I need help, I have 008 script that makes you infected once you touch part, but I do not know how to make, so when infected player touch other player, they get infected too. Here’s script:

local part = game.Workspace.sussy

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

local function GoInfected(player)
	player:FindFirstChild("Pants").PantsTemplate = "http://www.roblox.com/asset/?id=0"
	player:FindFirstChild("Shirt").ShirtTemplate = "http://www.roblox.com/asset/?id=0"
	if player:FindFirstChild("Shirt Graphic") then
		player:FindFirstChild("Shirt Graphic").Graphic = "http://www.roblox.com/asset/?id=0"
	end
	for _, desc in ipairs(player:GetDescendants()) do
		if desc:IsA('Accessory') and desc:FindFirstChild('Handle') and desc.Handle:FindFirstChild('HairAttachment') then desc:Destroy() end
	end
end



part.Touched:Connect(function(hit)
	local BodyColors = hit.Parent:FindFirstChild("Body Colors")
	local Player = hit.Parent
	local humanoid = Player:FindFirstChild("Humanoid")
	Player.Parent = game.Workspace.infected
	wait(5)
	ColorChanger(BodyColors)
	GoInfected(Player)
	wait(5)
	for i = 100, 0, -1 do
		humanoid.Health = i
		wait(5)
	end
end)

(I would also like any feedback on this code)

3 Likes
  • You can add some error handling to your code to ensure that it runs smoothly. For example, you can check if the Body Colors instance exists before calling the ColorChanger function, and you can check if the player has a Humanoid before reducing their health.
  • It’s a good practice to use a while loop instead of a for loop to gradually reduce the player’s health. This way, you can stop the loop if the player dies or if they are no longer touching the part.
1 Like

Hi, thank you for this, just it is zombie virus, so basically player had to touch it once, not all the time to reduce health

I get error from InfectPlayer function “Parent of Player can not be changed”

Server-script inside startercharacter.

local Character = script.Parent
local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
local CollectionService = game:GetService("CollectionService")
function Infect(Character:Model?)
	if not CollectionService:HasTag(Character, "Zombie") then
		CollectionService:AddTag(Character, "Zombie")
	end
end
local function ColorChanger(Player:Player?) --Infected function
	local BC = Player.Character:FindFirstChildOfClass("BodyColors")
	for _,v in Enum.BodyPart:GetEnumItems() do
		BC[v.Name.."Color"] = BrickColor.new("Lime green")
		task.wait(0.1)
	end
end

local function GoInfected(player)
	player:FindFirstChild("Pants").PantsTemplate = "http://www.roblox.com/asset/?id=0"
	player:FindFirstChild("Shirt").ShirtTemplate = "http://www.roblox.com/asset/?id=0"
	if player:FindFirstChild("Shirt Graphic") then
		player:FindFirstChild("Shirt Graphic").Graphic = "http://www.roblox.com/asset/?id=0"
	end
	for _, desc in ipairs(player:GetDescendants()) do
		if desc:IsA('Accessory') and desc:FindFirstChild('Handle') and desc.Handle:FindFirstChild('HairAttachment') then desc:Destroy() end
	end
end



for _,v in Character:GetChildren() do
	if v:IsA("BasePart") then
		if v.Name == "Right Arm" or v.Name == "Left Arm" and Character.Parent == game.Workspace.Infected then
			v.Touched:Connect(function(Part)
				local VictimPLAYER = game:GetService("Players"):GetPlayerFromCharacter(Part)
				if VictimPLAYER ~= nil and VictimPLAYER ~= Player and VictimPLAYER.Character and CollectionService:HasTag(VictimPLAYER.Character, "Zombie") and CollectionService:HasTag(Character, "Zombie") or Character.Parent == game.Workspace.Infected then
					VictimPLAYER.Character.Parent = game.Workspace.Infected
					Infect(VictimPLAYER.Character)
					ColorChanger(VictimPLAYER)
					GoInfected(VictimPLAYER)
					task.wait(5)
					task.spawn(function()
						for i = 100, 0, -1 do
							VictimPLAYER.Character:FindFirstChildOfClass("Humanoid").Health = i
							task.wait(1)
						end
					end)
				end
			end)
		end
	end
end
1 Like

I dont understand anything from this + im aware it doesnt work

Any errors in output?

no there isnt any error

dfsffsdf

Any warnings? What happens if you touch a humans with your arms?

Nvermind there is a warning. “IsTouching is not a valid member of Part “Workspace.sussy””

I don’t use IsTouching… Are you using you’re using my snippet?

Oh yeah my bad, it is not your script, there is no warnings. When I touch other player as a infetced, literally nothing happens.

Debugging, I also fixed some issues.

for _,v in Character:GetChildren() do
	if v:IsA("BasePart") then
		if v.Name == "Right Arm" or v.Name == "Left Arm" then then
			v.Touched:Connect(function(Part)
				print("Touched")
				local VictimPLAYER = game:GetService("Players"):GetPlayerFromCharacter(Part)
				if VictimPLAYER ~= nil and VictimPLAYER ~= Player and VictimPLAYER.Character and not CollectionService:HasTag(VictimPLAYER.Character, "Zombie") or VictimPLAYER.Character ~= game.Workspace.Infected and CollectionService:HasTag(Character, "Zombie") or Character.Parent == game.Workspace.Infected then
					VictimPLAYER.Character.Parent = game.Workspace.Infected
					print("infecting")
					Infect(VictimPLAYER.Character)
					ColorChanger(VictimPLAYER)
					GoInfected(VictimPLAYER)
					task.wait(5)
					task.spawn(function()
						for i = 100, 0, -1 do
							VictimPLAYER.Character:FindFirstChildOfClass("Humanoid").Health = i
							task.wait(1)
						end
					end)
				else
					print("player is already infected")
				end
			end)
		end
	end
end

Tell me if any those print statements print.

Maybe Im doing something wrong, nothing printed

That’s weird when I tested it, it printed touched. I edited my snippet, check it out.

I am now 100% sure Im doing something wrong, it doesnt work either

Is the script a serverscript inside startercharacter?

yes it is
dfdsfdsfdsff dsfdsfsdf

this is really funny because i wanted to make a infection system too!

1 Like

The ColorChanger() and GoInfected() function are not defined in the code you shared, so the script will throw an error when trying to execute those functions. You should either add the functions to the script or make sure they are being imported from elsewhere in your code. Also, I would recommend adding additional checks to make sure BodyColors and humanoid are not null before using them. For example:

part.Touched:Connect(function(hit)
    local BodyColors = hit.Parent:FindFirstChild("Body Colors")
    local Player = hit.Parent
    local humanoid = Player:FindFirstChild("Humanoid")
    if BodyColors and humanoid then
        Player.Parent = game.Workspace.infected
        wait(5)
        ColorChanger(BodyColors)
        GoInfected(Player)
        wait(5)
        for i = 100, 0, -1 do
            humanoid.Health = i
            wait(5)
        end
    end
end)

This way you prevent code from breaking in case BodyColors or humanoid are null.

2 Likes