I need help with a zombie infection spread script

Hi, i’m making a zombie infection script, like the ones you see in lab games, the problem is i can’t get the spread done, i tried to make a code so when you touch a player it will infect them.

I already have the zombie script itself done and the way it works is simple, inside the player’s character there’s a bool value, when it’s true, it means the player is infected, and when it’s false the player will turn back to normal.

So i tried making a code that when the player is touched, it sees if the touched.parent is a player and if it is to turn the boolvalue to true.

Code:

script.Parent.UpperTorso.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local infecstatus = hit.Parent:WaitForChild("InfectionStatus")
		if infecstatus.Value == false then
			script.Parent:WaitForChild("InfectionStatus").Value = true
		end
	end
end)

If someone can find an error or solution to my script any help would be appreciated. Thanks!

Just a reminder, the code goes inside the player’s character in workspace, aswell as the boolvalue wich is named InfectionStatus.

Why don’t you simply make a hitbox weld and then do the infection script there. Then all you have to do is do this script.

script.Parent.InfectionHitBox.Touched:Connect(function(hit)
for _, v in pairs(hit.Parent:GetChildren())
if hit.Parent:FindFirstChild("Humanoid") then
if hit ~= v then
local duplicatehitbox = script.Parent:Clone()
duplicatehitbox.Parent = hit.Parent
duplicatehitbox.Position = hit.Parent.HumanoidRootPart

local WeldCont = Instance.new("WeldConstraint")
WeldCont.Part0 = hit.Parent.HumanoidRootPart
WeldCont.Part1 = duplicatehitbox

                   end
            end 
     end
end


Also, you want the hitbox covering the Whole rig.

Reminder

You can also make a string attached to each one infected, that if they are touched the parts will not be created. This can reduce lag.

You can also use oop to do part of the script, but you don’t have to.

So what exactly am i supposed to do, i didn’t make that because i don’t know how it works, so am i supposed to make a part and put a script to infect the player inside it?

Also, if so how is the part supposed to be, i mean Achored, Collidable, etc.

You’re supposed to make the part person with the infection, “the original” which I believe you can do. Also, you put the script inside of the infectionhitbox, as putting it on the workspace would not work as it would only be affected by one hit box. I’ve also edited the post so that it is

 script.Parent.Touched:Connect(function()

instead of

 game.Workspace.Touched:Connect(function()

because the workspace would be with a ton of the same names, and I’m also not a fan of randomizing names imo.

also the part should be anchored + cancollideable. If the weld doesn’t work just unanchor it

1 Like

Simple fix:

local torso = script.Parent:WaitForChild("UpperTorso")
torso.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("InfectionStatus") then
		local infecstatus = hit.Parent:FindFirstChild("InfectionStatus")
		if infecstatus.Value == false then
			script.Parent:WaitForChild("InfectionStatus").Value = true
		end
	end
end)
1 Like