Infection gamemode problem

So I’m making events for my game (here) and I made an event called infection. This event is supposed to select a player, infect them, and whoever touches an infected player also becomes infected. When you get infected the user gets turned into the original infected player’s character. The thing is that the first infected player can infect others, but all the others cannot spread the infection as well.

I don’t know if this is because I am loading the character before I call the function on the other player or if it’s because of something else.

CODE:

local function infect(infected : Model)
		print(infected)
		local infectedPlayer = players:GetPlayerFromCharacter(infected)
		
		connection = infected.Humanoid.Touched:Connect(function(hit)
			local char = hit.Parent
			
			if char:FindFirstChild("Humanoid") and char.ClassName == "Model" then
				local plr = players:GetPlayerFromCharacter(char)
				local infectedValue = plr:FindFirstChild("Infected")
				
				if infectedValue.Value == false then
					infectedValue.Value = true

					plr.CharacterAppearanceId = player.UserId
					local cframe = char.HumanoidRootPart.CFrame
					plr:LoadCharacter()
					infect(char)
				end
			end
		end)
	end
Entire Code:
event.Infection = function(player : Player, duration : number)
	local players = game:GetService("Players")
	local connection

	if player == 0 then
		local randomNum = math.random(1, #players:GetPlayers())
		
		player = players:GetPlayers()[randomNum]
	end
	
	
	for i, v in pairs(players:GetPlayers()) do
		local infected = Instance.new("BoolValue", v)
		infected.Name = "Infected"
	end
	
	local function infect(infected : Model)
		print(infected)
		local infectedPlayer = players:GetPlayerFromCharacter(infected)
		
		connection = infected.Humanoid.Touched:Connect(function(hit)
			local char = hit.Parent
			
			if char:FindFirstChild("Humanoid") and char.ClassName == "Model" then
				local plr = players:GetPlayerFromCharacter(char)
				local infectedValue = plr:FindFirstChild("Infected")
				
				if infectedValue.Value == false then
					infectedValue.Value = true

					plr.CharacterAppearanceId = player.UserId
					local cframe = char.HumanoidRootPart.CFrame
					plr:LoadCharacter()
					infect(char)
				end
			end
		end)
	end
	
	infect(player.Character)
	RS:WaitForChild("events"):WaitForChild("eventText"):FireAllClients("Infection", player.DisplayName.." IS INFECTED, GET AWAY!")

	task.spawn(function()
		wait(duration)
		
		for i, v in pairs(players:GetPlayers()) do
			local char = v.Character or v.CharacterAdded:Wait()
			local value = v:FindFirstChild("Infected")
			
			if value then
				value:Destroy()
			end
			
			v.CharacterAppearanceId = v.UserId
			local cframe = char.HumanoidRootPart.CFrame
			v:LoadCharacter()
			connection:Disconnect()
		end
		
		RS:WaitForChild("events"):WaitForChild("eventText"):FireAllClients("Infection", "INFECTION IS OVER", true)
	end)
end

Thanks,
dza

It was because I was reloading the player’s character. I fixed it by changing one line with infect(plr.Character) from infect(char)

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