On death infection script bugs out

When the player dies, for some reason it ignores all checks and immediately infects the player. What changes can I make to fix this?

--//Services\\--
local Players = game:GetService("Players")
local repFirst = game:GetService("ReplicatedFirst")
local repStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

local library = require(repStorage.Assets.Modules.ServerLibrary)
local InfectionModule = require(repStorage.Assets.Modules.InfectionModule)
local gooFolder = game.Workspace.Map.Infection

--//Asset Related\\--
local firstAssets = repFirst.Assets
local storageAssets = repStorage.Assets

local infectedTypes = storageAssets.Infected

local infectAnims = firstAssets.Anims.infect
local sounds = firstAssets.Sounds

local tools = serverStorage.Tools
local debounce = false

--//Player & Character Handler\\--
Players.PlayerAdded:Connect(function(player)
		
	player.CharacterAdded:Connect(function(character)
		
		local plr = player
		local char = character

		local hum = char.Humanoid
		local plrStat = plr:WaitForChild("Status")
		local charStat = char:WaitForChild("Status")

		local Active = plrStat.Active
		local Infected = plrStat.Infected
		local Contagion = charStat.Contagion
		
		Infected.Value = false
		Active.Value = false
		Contagion.Value = 0

		--//Puddle Handler\\--
		for i, goo in pairs(gooFolder:GetChildren()) do

			--//Shark Puddles\\--
			for i, sharkPuddle in pairs(goo:GetChildren()) do

				local HPstat = infectedTypes.Shark.Stats.Health
				local SPDstat = infectedTypes.Shark.Stats.Speed

				if sharkPuddle.Name == "Puddle" or sharkPuddle.Name == "puddle" then

					sharkPuddle.Touched:Connect(function(touch)
						Active.Value = true

						if debounce == false then
							debounce = true
							task.wait(2)

							if touch.Parent:FindFirstChildOfClass("Humanoid") and Active.Value == true then

								while true do
									task.wait()
									if Infected.Value == false and Active.Value == true then

										if Contagion.Value >= 0 and Contagion.Value ~= 100 then

											Contagion.Value += 20
											library.play_sound({sound=sounds.Poison, parent=char.HumanoidRootPart})	
											task.wait(2)
										elseif Contagion.Value == 100 then

											Infected.Value = true
											charStat.InfectedColor.Value = sharkPuddle.Color
											charStat.InfectedType.Value = "Shark"
											--library.play_animation({anim=infectAnims['Puddle'], hum=hum})
											InfectionModule.gooSpread(player)
											print(plr.Name.." was Infected")

										end
									end
								end

							end

							debounce =  false
						end
					end)

					sharkPuddle.TouchEnded:Connect(function()

						Active.Value = false

					end)

				end

			end

		end
		
	end)

end)
Humanoid.Died:Connect(function()
   Contagion.Value = 0
end)

Have you tried this?

I highly recommend not using those type of sound effects btw.

The sound is just a placeholder, and yes

Is there a reason to put a touched connection inside CharacterAdded connection? Why not just put it somewhere else?

I’ll try moving it out and see what happens

When I moved it out it no longer detects whether they are infected or if it is active anymore
(After Death)

--//Services\\--
local Players = game:GetService("Players")
local repFirst = game:GetService("ReplicatedFirst")
local repStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

local library = require(repStorage.Assets.Modules.ServerLibrary)
local InfectionModule = require(repStorage.Assets.Modules.InfectionModule)
local gooFolder = game.Workspace.Map.Infection

--//Asset Related\\--
local firstAssets = repFirst.Assets
local storageAssets = repStorage.Assets

local infectedTypes = storageAssets.Infected

local infectAnims = firstAssets.Anims.infect
local sounds = firstAssets.Sounds

local tools = serverStorage.Tools
local debounce = false

--//Puddle Handler\\--
for i, goo in pairs(gooFolder:GetChildren()) do

	--//Shark Puddles\\--
	for i, sharkPuddle in pairs(goo:GetChildren()) do

		if sharkPuddle.Name == "Puddle" or sharkPuddle.Name == "puddle" then

			sharkPuddle.Touched:Connect(function(touch)				

				if debounce == false then
					debounce = true
					task.wait(2)

					if touch.Parent:FindFirstChildOfClass("Humanoid") then							
						
						local char = touch.Parent
						local plr = game.Players:GetPlayerFromCharacter(char)
						
						local hum = char.Humanoid
						local plrStat = plr:WaitForChild("Status")
						local charStat = char:WaitForChild("Status")

						local statsFolder = charStat:WaitForChild("Stats")

						local speed = statsFolder.Speed
						local health = statsFolder.Health
						local damage = statsFolder.Damage

						local Active = plrStat.Active
						local Infected = plrStat.Infected
						local Contagion = charStat.Contagion
						
						Active.Value = true

						hum.Died:Connect(function()
							Contagion.Value = 0
							Infected.Value = false
							Active.Value = false
							Contagion.Value = 0
						end)
						if Active.Value == true then
							
							while true do
								task.wait()
								if Infected.Value == false and Active.Value == true then

									if Contagion.Value >= 0 and Contagion.Value ~= 100 then

										Contagion.Value += 20
										library.play_sound({sound=sounds.Poison, parent=char.HumanoidRootPart})	
										task.wait(2)
									elseif Contagion.Value == 100 then

										Infected.Value = true
										charStat.InfectedColor.Value = sharkPuddle.Color
										charStat.InfectedType.Value = "Shark"

										speed.Value = 20

										hum.Health = health.Value
										hum.MaxHealth = health.Value
										hum.WalkSpeed = speed.Value

										--library.play_animation({anim=infectAnims['Puddle'], hum=hum})
										InfectionModule.gooSpread(plr)
										print(plr.Name.." was Infected")											

									end
								end
							end
							
						end
						


					end

					debounce =  false
				end
			end)

			sharkPuddle.TouchEnded:Connect(function(touch)
				local char = touch.Parent
				local plr = game.Players:GetPlayerFromCharacter(char)
				
				local plrStat = plr:WaitForChild("Status")
				local Active = plrStat.Active

				Active.Value = false

			end)

		end

	end

end


I’ll just re-write the whole thing and try to learn more…

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