Help with Infection Script

So I have a infection script thats inside server script service

local coughAnimation = script.CoughAnimation
local Ocough1 = script.Cough1
local Ocough2 = script.Cough2
local Ocough3 = script.Cough3
local Ocough4 = script.Cough4

local function infection(character, humanoid)

local cough1 = Ocough1:Clone()
local cough2 = Ocough2:Clone()
local cough3 = Ocough3:Clone()
local cough4 = Ocough4:Clone()

cough1.Parent = character
cough2.Parent = character
cough3.Parent = character
cough4.Parent = character

for i = 1,10 do
wait(10)
if i == 10 then
humanoid.Health = 0
else
print(“cough”)

  	local coughTrack = humanoid:LoadAnimation(coughAnimation)

  	coughTrack:Play()
  	wait(0.1)
  	cough1:Play()
  	wait(1.1)
  	cough2:Play()
  	wait(1.2)
  	cough3:Play()
  	wait(1)
  	cough4:Play()
  end

end
end

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local infected = character:WaitForChild(“Infected”)
local humanoid = character.Humanoid

  infected.Changed:Connect(function()
  	if infected.Value == true then
  		print(player.Name .. " is infected")
  		infection(character, humanoid)
  	end

  end)

end)
end)

and basically I want it so if another player is near the infected player when the infected player is coughing then that player near the infected player gets infected aswell, how would I accomplish this?

Inside a server script, get the magnitude (distance) between two player’s humanoidRootParts. If the distance is < then a set amount (how close to get infected) then run your infection script. You can check this on heartbeat or something, This is a very basic implementation and I don’t know the performance impacts… You’d want to add something like this:

local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
   local players = game.Players:GetPlayers()
   for index, thisPlayer in pairs(players) do
      if thisPlayer.infected == true then --check if the player is infected
      for index, player in pairs(players) do -- if they're infected, check their position against all other players
	         if player.Character then
		         local target = player.Character
		         --get player's current distance from local player
		         local distance = (thisPlayer.Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude 

		         --how close the player has to be to get infected
		         local radius = 10

		         --check to see if player is within radius
		         if distance < radius then
			         infect(player)--call your code to infect the player
		         else
			         --dont infect
		         end
	         end
         end
   else --dont check if they're not infected
      end
   end
end)

local coughAnimation = script.CoughAnimation
local Ocough1 = script.Cough1
local Ocough2 = script.Cough2
local Ocough3 = script.Cough3
local Ocough4 = script.Cough4

local function infection(character, humanoid)
local cough1 = Ocough1:Clone()
local cough2 = Ocough2:Clone()
local cough3 = Ocough3:Clone()
local cough4 = Ocough4:Clone()

cough1.Parent = character
cough2.Parent = character
cough3.Parent = character
cough4.Parent = character

for i = 1, 10 do
	wait(10)
	if i == 10 then
		humanoid.Health = 0
	else
		print("cough")

		local coughTrack = humanoid:LoadAnimation(coughAnimation)

		coughTrack:Play()
		wait(0.1)
		cough1:Play()
		wait(1.1)
		cough2:Play()
		wait(1.2)
		cough3:Play()
		wait(1)
		cough4:Play()
	end
end

end

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local infected = character:WaitForChild(“Infected”)
local humanoid = character.Humanoid

	infected.Changed:Connect(function()
		if infected.Value == true then
			print(player.Name .. " is infected")
			infection(character, humanoid)
		end
	end)
end)

end)

local RunService = game:GetService(“RunService”)
RunService.Heartbeat:Connect(function()
local players = game.Players:GetPlayers()
for _, thisPlayer in pairs(players) do
if thisPlayer.Character and thisPlayer.Character:FindFirstChild(“Infected”) then
if thisPlayer.Character.Infected.Value == true then
for _, player in pairs(players) do
if player ~= thisPlayer and player.Character and player.Character:FindFirstChild(“Infected”) then
local target = player.Character
local distance = (thisPlayer.Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
local radius = 10
if distance < radius then
player.Character.Infected.Value = true
end
end
end
end
end
end
end)
do u think somehting like this would work?

and also how would i make it so when the player coughs thats when they get infected.

it works now but it always makes them infected if they are near i want it to be when they cough in that time range they get infected

ok so now i fixed it heres my finalized code

local coughAnimation = script.CoughAnimation
local Ocough1 = script.Cough1
local Ocough2 = script.Cough2
local Ocough3 = script.Cough3
local Ocough4 = script.Cough4

local isCoughing = false

local function infection(character, humanoid)
local cough1 = Ocough1:Clone()
local cough2 = Ocough2:Clone()
local cough3 = Ocough3:Clone()
local cough4 = Ocough4:Clone()

cough1.Parent = character
cough2.Parent = character
cough3.Parent = character
cough4.Parent = character

for i = 1, 10 do
wait(10)
isCoughing = true
if i == 10 then
humanoid.Health = 0
isCoughing = false
else
print(“cough”)

  	local coughTrack = humanoid:LoadAnimation(coughAnimation)

  	coughTrack:Play()
  	wait(0.1)
  	cough1:Play()
  	wait(1.1)
  	cough2:Play()
  	wait(1.2)
  	cough3:Play()
  	wait(1)
  	cough4:Play()
  	isCoughing = false
  	
  end

end
end

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local infected = character:WaitForChild(“Infected”)
local humanoid = character.Humanoid

  infected.Changed:Connect(function()
  	if infected.Value == true then
  		print(player.Name .. " is infected")
  		infection(character, humanoid)
  	end
  end)

end)
end)

local RunService = game:GetService(“RunService”)

RunService.Heartbeat:Connect(function()
if isCoughing == true then
local players = game.Players:GetPlayers()
for _, thisPlayer in pairs(players) do
if thisPlayer.Character and thisPlayer.Character:FindFirstChild(“Infected”) then
if thisPlayer.Character.Infected.Value == true then
for _, player in pairs(players) do
if player ~= thisPlayer and player.Character and player.Character:FindFirstChild(“Infected”) then
local target = player.Character
local distance = (thisPlayer.Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
local radius = 10
if distance < radius then
player.Character.Infected.Value = true
end
end
end
end
end
end
end
end)

spookete who i know will prob see this will prob laugh at me :frowning:

2 Likes

hahahahhhahaha yes i laugh at you, also format the code better in devforum lol that looks bad

(use ``` like in dc, instead of >)

called it

30 yaay yayay whatever

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.