Disease Script - .Touched Contagious Infection

Heya! Recently I’ve been trying to make a disease script. Most of it works except one detail, getting the disease when touching an infected individual.

  1. What do you want to achieve? Keep it simple and clear!
    I want it so when a player touches and infected player, they will also be infected.

Additionally, my Universal Cure tool cures everyone instead of the person who has the tool, I’m more concerned on the infection rather than the cure right now though.

  1. What is the issue? Include screenshots / videos if possible!
    It doesn’t make the other player sick when touched.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried using a remote event and bindable event but neither worked. Searching up infection on google or anywhere just gets those model viruses.

There’s a lot of code, I’ll compress the ones that I believe aren’t related to the issue.

Local Script - StarterPlayerScripts
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local CollectionService = game:GetService("CollectionService")

local virusremote = game.ReplicatedStorage.VirusRemote

wait(10)

ColdTurnOff = false
FluTurnOff = false
TuberTurnOff = false

game.ReplicatedStorage.VirusCold.OnClientEvent:Connect(function(disease)
	if disease == "Cold" then
		ColdTurnOff = true
	elseif disease == "Flu" then
		FluTurnOff = true
	elseif disease == "Tuber" then
		TuberTurnOff = true
	end
end)




while true do
	
	
	local newRandom
	local oldRandom = newRandom
	
	local chosenDisease
	local oldchosenDisease= chosenDisease
	
	
	if TuberOn == true then
		for count = 1, 60 do
			wait(1)
			if TuberTurnOff == true then
				TuberTurnOff = false
				break
			end
		end
		TuberOn = false
		
	elseif FluOn == true then
		for count = 1, 300 do
			wait(1)
			if FluTurnOff == true then
				FluTurnOff = false
				break
			end
		end
		FluOn = false
		
	elseif ColdOn == true then
		for count = 1, 30 do
			wait(16)
			if ColdTurnOff == true then
				ColdTurnOff = false
				break
			end
		end
		ColdOn = false
	end
	
	
	do repeat
			newRandom =  math.random(20, 30)
			wait()
		until
		newRandom ~= oldRandom
	end
	
	do repeat
			chosenDisease = math.random(0, 100)
			wait()
		until
		chosenDisease ~= oldchosenDisease
	end
	
	
	wait(newRandom)
	print(chosenDisease)
	
	TuberOn = false
	FluOn = false
	ColdOn = false
	
	if chosenDisease >= 90 then
		print("Tuberculosis!")
		virusremote:FireServer("Tuber", Player.Character)
		TuberOn = true
		
	elseif chosenDisease >= 85 then
		print("Flu!")
		virusremote:FireServer("Flu", Player.Character)
		FluOn = true
		
	elseif chosenDisease >= 65 then
		print("Cold!")
		virusremote:FireServer("Cold", Player.Character)
		ColdOn = true
		
	elseif chosenDisease >= 0 then
		print("None!")
	end
end
StarterPack Tools (Multiple tools)
Local Script - Cure
local tool = script.Parent
local player = game.Players.LocalPlayer
local Debounce = true
local virusremote = game.ReplicatedStorage.VirusRemote
local player = game.Players.LocalPlayer



tool.Activated:Connect(function()
	
	Debounce = false
	virusremote:FireServer("remove disease", player.Character)
	wait(1)
	Debounce = true
	
end)
Local Script - ColdInfective
local tool = script.Parent
local player = game.Players.LocalPlayer
local Debounce = true
local virusremote = game.ReplicatedStorage.VirusRemote
local player = game.Players.LocalPlayer


tool.Activated:Connect(function()
	
	Debounce = false
	virusremote:FireServer("Cold", player.Character)
	wait(1)
	Debounce = true
	
end)
Local Script - FluInfective
local tool = script.Parent
local player = game.Players.LocalPlayer
local Debounce = true
local virusremote = game.ReplicatedStorage.VirusRemote
local player = game.Players.LocalPlayer


tool.Activated:Connect(function()
	
	Debounce = false
	virusremote:FireServer("Flu", player.Character)
	wait(1)
	Debounce = true
	
end)
Local Script - TuberInfective
local tool = script.Parent
local player = game.Players.LocalPlayer
local Debounce = true
local virusremote = game.ReplicatedStorage.VirusRemote
local player = game.Players.LocalPlayer


tool.Activated:Connect(function()
	
	Debounce = false
	virusremote:FireServer("Tuber", player.Character)
	wait(1)
	Debounce = true
	
end)

Server Script - ServerScriptService

local virusremote = game.ReplicatedStorage.VirusRemote
local virusbindable = game.ReplicatedStorage.VirusBindable
local coldOn = game.ReplicatedStorage.VirusCold
local contagion = game.ReplicatedStorage.Contagion
local collectionservice = game:GetService("CollectionService")

virusremote.OnServerEvent:Connect(function(originalplr,disease,infectedplr)
	virusbindable:Fire(disease,infectedplr)
	collectionservice:AddTag(infectedplr,disease)
	if originalplr == infectedplr then
		if disease == "remove disease" then
			coldOn:FireClient(disease)
		end
	end
end)

contagion.Event:Connect(function(originalplr,disease,infectedplr, hit)
	virusbindable:Fire(disease,hit)
	collectionservice:AddTag(hit,disease)
	if originalplr == hit then
		if disease == "remove disease" then
			coldOn:FireClient(disease)
		end
	end
end)

Server Script - ServerScriptService

local collectionservice = game:GetService("CollectionService")
local virusbindable = game.ReplicatedStorage.VirusBindable
local cloning = script:Clone()
local virusremote = game.ReplicatedStorage.VirusRemote
local contagion = game.ReplicatedStorage.Contagion

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		virusbindable.Event:Connect(function(disease,plr)
			if disease == "Cold" then
				if disease == "Cold" then
					for i, v in pairs (character:GetChildren()) do
						if v:IsA('BasePart') then
							v.Touched:Connect(function(hit)
								contagion:Fire("Cold", hit.Parent)
							end)
						end
					end
				end
				if disease == "remove disease" then
					collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
					cloning.Parent = script.Parent
					plr.Humanoid.WalkSpeed = 16
					script:Destroy()
				end
				
			elseif disease == "Flu" then
				if disease == "Flu" then
					
				end
				if disease == "remove disease" then
					collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
					cloning.Parent = script.Parent
					plr.Humanoid.WalkSpeed = 16
					script:Destroy()
				end
			elseif disease == "Tuber" then
				if disease == "Tuber" then
					
				end
				if disease == "remove disease" then
					collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
					cloning.Parent = script.Parent
					plr.Humanoid.WalkSpeed = 16
					script:Destroy()
				end
			elseif disease == "remove disease" then
				collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
				cloning.Parent = script.Parent
				plr.Humanoid.WalkSpeed = 16
				script:Destroy()
			end
			if disease == "remove disease" then
				collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
				cloning.Parent = script.Parent
				plr.Humanoid.WalkSpeed = 16
				script:Destroy()
			end
		end)
		
		
		virusbindable.Event:Connect(function(disease,plr)
			if disease == "Cold" then
				if disease == "Cold" then
					local Cough1Copy = game.ServerStorage.Cough1:Clone()
					Cough1Copy.Parent = plr.Head
					
					local Cough2Copy = game.ServerStorage.Cough2:Clone()
					Cough2Copy.Parent = plr.Head
					for count = 1, 30 do
						wait(8)
						Cough2Copy:Stop()
						Cough1Copy:Play()
						plr.Humanoid.WalkSpeed = plr.Humanoid.WalkSpeed - .4
						wait(8)
						Cough1Copy:Stop()
						Cough2Copy:Play()
					end
					Cough1Copy:Destroy()
					Cough2Copy:Destroy()
				end
				if disease == "remove disease" then
					collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
					cloning.Parent = script.Parent
					plr.Humanoid.WalkSpeed = 16
					script:Destroy()
				end
				
			elseif disease == "Flu" then
				if disease == "Flu" then
					for count = 1,300 do
						wait(1)
						plr.Humanoid.Health = plr.Humanoid.Health - .1
						wait()
						plr.Humanoid.WalkSpeed = plr.Humanoid.WalkSpeed - .005
					end
					if disease == "remove disease" then
						collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
						cloning.Parent = script.Parent
						plr.Humanoid.WalkSpeed = 16
						script:Destroy()
					end
				end
			elseif disease == "Tuber" then
				if disease == "Tuber" then
					for count = 1,60 do
						wait(1)
						plr.Humanoid.WalkSpeed = plr.Humanoid.WalkSpeed - .1
						wait()
						plr.Humanoid.Health = plr.Humanoid.Health - 1
					end
					if disease == "remove disease" then
						collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
						cloning.Parent = script.Parent
						plr.Humanoid.WalkSpeed = 16
						script:Destroy()
					end
				end
			elseif disease == "remove disease" then
				collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
				cloning.Parent = script.Parent
				plr.Humanoid.WalkSpeed = 16
				script:Destroy()
			end
			if disease == "remove disease" then
				collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
				cloning.Parent = script.Parent
				plr.Humanoid.WalkSpeed = 16
				script:Destroy()
			end
		end)
	end)
end)

1 Like

In order for us to help with the simplification of your problem could you put some prints in and show us to help us understand which parts are working so we can eliminate those.

Adding in the prints, I found some errors of my own, I’ll show the updated scripts here.
However, it got itself into a loop and still doesn’t infect the other player.

Updated Server Script - ServerScriptService
local collectionservice = game:GetService("CollectionService")
local virusbindable = game.ReplicatedStorage.VirusBindable
local cloning = script:Clone()
local virusremote = game.ReplicatedStorage.VirusRemote
local contagion = game.ReplicatedStorage.Contagion

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		virusbindable.Event:Connect(function(disease,plr)
			if disease == "Cold" then
				if disease == "Cold" then
					for i, v in pairs (character:GetChildren()) do
						if v:IsA('BasePart') then
							v.Touched:Connect(function(hit)
								print("FiredInfection")
								contagion:Fire("Cold", hit.Parent)
							end)
						end
					end
				end
				if disease == "remove disease" then
					collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
					cloning.Parent = script.Parent
					plr.Humanoid.WalkSpeed = 16
					print("DestroyedScriptBeans")
					script:Destroy()
				end
				
			elseif disease == "Flu" then
				if disease == "Flu" then
					
				end
				if disease == "remove disease" then
					collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
					cloning.Parent = script.Parent
					plr.Humanoid.WalkSpeed = 16
					print("DestroyedScriptBanana")
					script:Destroy()
				end
			elseif disease == "Tuber" then
				if disease == "Tuber" then
					
				end
				if disease == "remove disease" then
					collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
					cloning.Parent = script.Parent
					plr.Humanoid.WalkSpeed = 16
					print("DestroyedScriptTree")
					script:Destroy()
				end
			elseif disease == "remove disease" then
				collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
				cloning.Parent = script.Parent
				plr.Humanoid.WalkSpeed = 16
				print("DestroyedScriptGrass")
				script:Destroy()
			end
			if disease == "remove disease" then
				collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
				cloning.Parent = script.Parent
				plr.Humanoid.WalkSpeed = 16
				print("DestroyedScriptRazor")
				script:Destroy()
			end
		end)
		
		
		virusbindable.Event:Connect(function(disease,plr)
			if disease == "Cold" then
				if disease == "Cold" then
					local Cough1Copy = game.ServerStorage.Cough1:Clone()
					Cough1Copy.Parent = plr.Head
					
					local Cough2Copy = game.ServerStorage.Cough2:Clone()
					Cough2Copy.Parent = plr.Head
					print("BeganCold")
					for count = 1, 30 do
						wait(8)
						Cough2Copy:Stop()
						Cough1Copy:Play()
						plr.Humanoid.WalkSpeed = plr.Humanoid.WalkSpeed - .4
						wait(8)
						Cough1Copy:Stop()
						Cough2Copy:Play()
						print("DoingCold")
					end
					Cough1Copy:Destroy()
					Cough2Copy:Destroy()
					print("EndedCold")
				end
				if disease == "remove disease" then
					collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
					cloning.Parent = script.Parent
					plr.Humanoid.WalkSpeed = 16
					script:Destroy()
				end
				
			elseif disease == "Flu" then
				if disease == "Flu" then
					for count = 1,300 do
						wait(1)
						plr.Humanoid.Health = plr.Humanoid.Health - .1
						wait()
						plr.Humanoid.WalkSpeed = plr.Humanoid.WalkSpeed - .005
					end
					if disease == "remove disease" then
						collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
						cloning.Parent = script.Parent
						plr.Humanoid.WalkSpeed = 16
						script:Destroy()
					end
				end
			elseif disease == "Tuber" then
				if disease == "Tuber" then
					for count = 1,60 do
						wait(1)
						plr.Humanoid.WalkSpeed = plr.Humanoid.WalkSpeed - .1
						wait()
						plr.Humanoid.Health = plr.Humanoid.Health - 1
					end
					if disease == "remove disease" then
						collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
						cloning.Parent = script.Parent
						plr.Humanoid.WalkSpeed = 16
						script:Destroy()
					end
				end
			elseif disease == "remove disease" then
				collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
				cloning.Parent = script.Parent
				plr.Humanoid.WalkSpeed = 16
				script:Destroy()
			end
			if disease == "remove disease" then
				collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
				cloning.Parent = script.Parent
				plr.Humanoid.WalkSpeed = 16
				script:Destroy()
			end
		end)
	end)
end)
Updated Server Script - ServerScriptService
local virusremote = game.ReplicatedStorage.VirusRemote
local virusbindable = game.ReplicatedStorage.VirusBindable
local coldOn = game.ReplicatedStorage.VirusCold
local contagion = game.ReplicatedStorage.Contagion
local collectionservice = game:GetService("CollectionService")

virusremote.OnServerEvent:Connect(function(originalplr,disease,infectedplr)
	virusbindable:Fire(disease,infectedplr)
	collectionservice:AddTag(infectedplr,disease)
	print("Fired!")
	if originalplr == infectedplr then
		if disease == "remove disease" then
			print("FiredCure!")
			coldOn:FireClient(disease)
		end
	end
end)

contagion.Event:Connect(function(originalplr,disease,infectedplr)
	virusbindable:Fire(disease,infectedplr)
	collectionservice:AddTag(infectedplr,disease)
	print("I Think I Refired")
	if originalplr == infectedplr then
		if disease == "remove disease" then
			print("Refired")
			coldOn:FireClient(disease)
		end
	end
end)

Updated Local Script - Cure
local tool = script.Parent
local player = game.Players.LocalPlayer
local Debounce = true
local virusremote = game.ReplicatedStorage.VirusRemote
local player = game.Players.LocalPlayer



tool.Activated:Connect(function()
	
	Debounce = false
	virusremote:FireServer("remove disease", player.Character)
	print("FiredCureTool")
	wait(1)
	Debounce = true
end)

Output (When used cold tool on self):

Output (When used cure tool on self THEN infected self with cold tool)
image
image

Output (When trying to infect others):
(Keep in mind I printed walkspeed of the other player to see if they were actually infected.)

image

when you do a FireClient can you confirm the first argument is a player.

Yes, I added a print after this message and every time I touched someone it printed their name, but I do not know if the FireClient still keeps that data.

Honestly might give up on the script because it’s a little complex for me.

Objections/advices:

LocalScript: Always use “:FindFirstChild” when searching modules to make sure you will get it before advancing with the script
local virusremote = game:GetService(“ReplicatedStorage”):FindFirstChild(“VirusRemote”)
Advice: care what arguments are passed on arguments as it ieasy to sixt them up (learned teh hard way), add lots of print(arguments…) when debugging

ServerScripts: NEVER trust objects passed by a remote event from players and always extra-check what they are

General rule: split your code into parts by using functions/ModuleScripts so it is easier to read and get help

For example:

local function applyPlayerDisease(targetPlayer, disieaseName)
	if disieaseName == "remove disease" then
		collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
		cloning.Parent = script.Parent
		plr.Humanoid.WalkSpeed = 16
		script:Destroy()
	elseif disieaseName == "Flu" then
	-- 	... flu logic here
	elseif disease == "Tuber" then
	-- 	... Tuber logic here
	--... and so on for all effects
	end
end
virusbindable.Event:Connect(applyPlayerDisease)

Also, why are you checking twice the same thing?
if disease == "Cold" then if disease == "Cold" then

Make sure when using :FireClient to put what player you want to fire on as the first parameter! If this isn’t a solution please describe the problem better.

Ex.