Disease System and Cures

So I went in and made edits to my original scripts based off of your game, I still get this:

It makes me wonder if it has something to do with the script that includes the chances. I’ll put in all four updated scripts.

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)



while true do
	
	
	
	local newRandom
	local oldRandom = newRandom
	
	local chosenDisease
	local oldchosenDisease= chosenDisease
	
	
	if TuberOn == true then
		for count = 1, 60 do
			wait(1)
			Character.Humanoid.Health = Character.Humanoid.Health - 1
			Character.Humanoid.WalkSpeed = Character.Humanoid.WalkSpeed - .1
		end
		TuberOn = false
		
	elseif FluOn == true then
		for count = 1, 300 do
			wait(1)
			Character.Humanoid.Health = Character.Humanoid.Health - .1
			Character.Humanoid.WalkSpeed = Character.Humanoid.WalkSpeed - .005
		end
		FluOn = false
		
	elseif ColdOn == true then
		CollectionService:AddTag(Character.Humanoid, "Cold")
		for count = 1, 30 do
		wait(16)
		end
		
		ColdOn = false
		CollectionService:RemoveTag(Character.Humanoid, "Cold")
	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!")
		TuberOn = true
		
	elseif chosenDisease >= 85 then
		print("Flu!")
		FluOn = true
		
	elseif chosenDisease >= 0 then
		print("Cold!")
		ColdOn = true
		
	elseif chosenDisease >= 0 then
		print("None!")
	end
end

Local Script (Tool)

local tool = script.Parent
local player = game.Players.LocalPlayer
local Debounce = true
local virusremote = game.ReplicatedStorage.VirusRemote
local player = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
	tool.Handle.Touched:Connect(function(hit)
				Debounce = false
				virusremote:FireServer("Cold", hit.player.Character)
				print("firedcold")
				wait(1)
				Debounce = true
		end)
end)

Server Script (ServerScriptService)

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

virusremote.OnServerEvent:Connect(function(originalplr,disease,infectedplr)
	print(infectedplr.Name)
	virusbindable:Fire(disease,infectedplr)
	collectionservice:AddTag(infectedplr,disease)
end)

Server Script (ServerScriptService)
(Handler)

local collectionservice = game:GetService("CollectionService")
local virusbindable = game.ReplicatedStorage.VirusBindable




virusbindable.Event:Connect(function(disease,plr)
	print(plr.Name)
	if disease == "Cold" then
		if disease == "Cold" then
--			local Cough1Copy = game.ReplicatedStorage.Cough1:Clone()
--			Cough1Copy.Parent = plr.Head
			
--			local Cough2Copy = game.ReplicatedStorage.Cough2:Clone()
--			Cough2Copy.Parent = plr.Head
			
			print("GotCold!")
			wait(8)
			print(plr.Parent.Name)
--			Cough1Copy:Play()
			plr.Humanoid.WalkSpeed = plr.Humanoid.WalkSpeed - .4
			wait(8)
--			Cough2Copy:Play()
		end
		if disease == "remove disease" then
			collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
		end
	end
	if disease == "remove disease" then
		collectionservice:RemoveTag(plr,collectionservice:GetTags(plr))
	end
end)

DiseaseSystemTesting.rbxl (24.7 KB)

ServerScriptService → Diseases

local Diseases = {
	["Flu"] = {
		["WalkspeedReduction"]=.1,
		["WalkspeedReductionInterval"] = 8,
		["HealthReduction"]=2.5,
		["HealthReductionInterval"]=1,
		["Duration"]=60,
	},
	["Tuberculosis"] = {
		["WalkspeedReduction"]=.4,
		["WalkspeedReductionInterval"] = 8,
		["HealthReduction"]=2.5,
		["HealthReductionInterval"]=0.6,
		["Duration"]=120,
	},
	["Cold"] = {
		["WalkspeedReduction"]=.2,
		["WalkspeedReductionInterval"] = 8,
		["HealthReduction"]=2.5,
		["HealthReductionInterval"]=1,
		["Duration"]=30,
	}
}


local PlayerInfections={}


function PickRandomDisease()
	local Disease = nil
	local DiseasesAmount = 0
	local DiseaseName = nil
	for i,v in pairs(Diseases) do
		DiseasesAmount += 1
	end
	local RandomNumber = math.random(1,DiseasesAmount)
	local Index = 0
	for i,v in pairs(Diseases) do
		Index += 1
		if Index == RandomNumber then
			Disease = Diseases[i]
			DiseaseName = i
			break
		end
	end
	return {Disease, DiseaseName}
end


function InfectPlayer(Player, Disease)
	-- {(PlayerObject)Player, (Table)DiseaseObjects}
	local Character = Player.Character or Player.CharacterAdded:Wait()
	repeat
		wait(0.25)
		Character = Player.Character or Player.CharacterAdded:Wait()
	until
		Character ~= nil
	
	local Found = nil
	for i,v in pairs(PlayerInfections) do
		if v[1] == Player then
			Found = PlayerInfections[i]
			break
		end
	end
	if Found ~= nil then
		table.insert(Found[2], Disease)
	else
		Disease.ContractionTime = tick()
		table.insert(PlayerInfections, {Player, {Disease}})
	end
end


function InfectRandomPlayerWithRandomDisease()
	local Players = game.Players:GetPlayers()
	local RandomNumber = math.random(1,#Players)
	local Player = Players[RandomNumber]
	local Disease = PickRandomDisease()
	InfectPlayer(Player, Disease[1])
	return {Player, Disease}
end


function CurePlayer(Player, Disease)
	if type(Disease) == "string" then
		if Disease == "-all" then
			for i,v in pairs(PlayerInfections) do
				-- {(PlayerObject)Player, (Table)DiseaseObjects}
				if v[1] == Player then
					v[2] = {}					
					return true
				end
			end
		end
	end
	for i,v in pairs(PlayerInfections) do
		-- {(PlayerObject)Player, (Table)DiseaseObjects}
		if v[1] == Player then
			for x,c in pairs(v[2]) do
				if tostring(c) == tostring(Disease) then
					table.remove(v[2], x)
					return true
				end
			end
		end
	end
end



coroutine.wrap(function()
	while true do
		wait()
		for i,v in pairs(PlayerInfections) do
			-- {(PlayerObject)Player, (Table)DiseaseObjects}
			for x,c in pairs(v[2]) do
				if c.ContractionTime ~= nil then
					if tick() - c.ContractionTime >= c.Duration then
						CurePlayer(v[1], c)
						continue
					end
				else
					c.ContractionTime = tick()
				end
				if c.LastHealthReduction ~= nil then
					if tick() - c.LastHealthReduction >= c.HealthReductionInterval then
						c.LastHealthReduction = tick()
						local Character = v[1].Character
						if Character ~= nil then
							local Humanoid = Character["Humanoid"]
							if Humanoid ~= nil then
								Humanoid:TakeDamage(c.HealthReduction)
							end
						else
							CurePlayer(v[1], "-all")
							break
						end
					end
				else
					c.LastHealthReduction = tick()
					local Character = v[1].Character
					if Character ~= nil then
						local Humanoid = Character["Humanoid"]
						if Humanoid ~= nil then
							Humanoid:TakeDamage(c.HealthReduction)
						end
					else
						CurePlayer(v[1], "-all")
						break
					end
				end
				
				if c.LastWalkspeedReduction ~= nil then
					if tick() - c.LastWalkspeedReduction >= c.WalkspeedReductionInterval then
						c.LastWalkspeedReduction = tick()
						local Character = v[1].Character
						if Character ~= nil then
							local Humanoid = Character["Humanoid"]
							if Humanoid ~= nil then
								Humanoid.WalkSpeed -= c.WalkspeedReduction
							end
						else
							CurePlayer(v[1], "-all")
							break
						end
					end
				else
					c.LastWalkspeedReduction = tick()
					local Character = v[1].Character
					if Character ~= nil then
						local Humanoid = Character["Humanoid"]
						if Humanoid ~= nil then
							Humanoid.WalkSpeed -= c.WalkspeedReduction
						end
					else
						CurePlayer(v[1], "-all")
						break
					end
				end
			end
		end
	end
end)()


local CurePlayerBindableFunction = script:WaitForChild("CurePlayer")
CurePlayerBindableFunction.OnInvoke = function(Player, Disease)
	CurePlayer(Player, Disease)
	return true
end


while true do
	wait()
	if #game.Players:GetPlayers() > 0 then
		local Result = InfectRandomPlayerWithRandomDisease()
		--print("Infected "..Result[1].Name.." With "..Result[2][2])
		wait(180)
	end
end

StarterPack → Universal Cure → CureScript

local Tool = script.Parent
local Handle = Tool.Handle
local Needle = Tool.Needle
local Tank = Tool.Tank
local TotalUses = 1
local CureFunction = game.ServerScriptService:WaitForChild("Diseases").CurePlayer
local Uses = 0
local InUse = false
local Target = nil
local TargetSelectedPart = nil
local Cooldown = 1
local LocalPlayer = nil


Tool.Name = "Univseral Cure ("..TotalUses-Uses..")"


Needle.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player ~= LocalPlayer and LocalPlayer ~= nil and Player ~= nil then
		Target = {Player, Player.Character}
		TargetSelectedPart = hit
		Tool.Name = "UC ("..TotalUses-Uses..") - Inject: "..Player.Name
	end
end)


Needle.TouchEnded:Connect(function(hit)
	if Target ~= nil then
		if (Target[2].HumanoidRootPart.Position - Handle.Position).Magnitude < 5 and hit == TargetSelectedPart then
			Tool.Name = "UC ("..TotalUses-Uses..") - Inject: "..Target[1].Name
		else
			Target = nil
			Tool.Name = "Univseral Cure ("..TotalUses-Uses..")"
		end
	end
end)


Tool.Equipped:Connect(function()
	if LocalPlayer == nil then
		LocalPlayer = game.Players:GetPlayerFromCharacter(Tool.Parent)
	end
end)


Tool.Activated:Connect(function()
	if Target ~= nil and not InUse and Uses < TotalUses then
		InUse = true
		local Result = CureFunction:Invoke(Target[1], "-all")
		if Result ~= true then
			InUse = false
			return
		end
		Uses += 1
		Tool.Name = "Univseral Cure ("..TotalUses-Uses..")"
		if Uses >= TotalUses then
			Tank.Color = Color3.fromRGB(255,255,255)
		end
		wait(Cooldown)
		InUse = false
	elseif Target == nil and not InUse and Uses < TotalUses then
		InUse = true
		local Result = CureFunction:Invoke(LocalPlayer, "-all")
		if Result ~= true then
			InUse = false
			return
		end
		Uses += 1
		Tool.Name = "Univseral Cure ("..TotalUses-Uses..")"
		if Uses >= TotalUses then
			Tank.Color = Color3.fromRGB(255,255,255)
		end
		wait(Cooldown)
		InUse = false
	end
end)

The actual reason why is it says that character isn’t a valid member of meshpart is because you’re actually saying:

MeshPart.Character.Humanoid.WalkSpeed = plr.Character.Humanoid.WalkSpeed - .4

For other problems you have i’ll discuss it in my head later so i can have some plausible solutions.

Edit: The other errors earlier that also said that character is not a valid member of part is also the issue with getting the part, not the parent.

Part.Character.Humanoid.WalkSpeed = plr.Character.Humanoid.WalkSpeed - .4

After a bit of messing around, I got the script to work.
I thank everyone for helping out, I have no doubt the other ones work as well.

Have a great day!

1 Like