I am trying to make a code that infects players whenever they have the virus, but whenever i try to spread it , the outputs says: attempt to index nil with 'FindFirstChild'
local players = game:GetService("Players") local player = game.Players.LocalPlayer.Character player:WaitForChild("Humanoid").Touched:Connect(function(hit) if(hit.Parent:FindFirstChild("Humanoid")) then if not players:GetPlayerFromCharacter(hit.Parent):FindFirstChild("IsInfected")and players:GetPlayerFromCharacter(player):FindFirstChild("IsInfected") then local infectedPlr = players:GetPlayerFromCharacter(hit.Parent) local boolValue = Instance.new("BoolValue",infectedPlr) boolValue.Name = "isInfected" boolValue.Value = true game:GetService("ReplicatedStorage").VirusUI:Clone().Parent = infectedPlr.PlayerGui if game:GetService("ReplicatedStorage").VirusUI:Clone().Parent == infectedPlr.PlayerGui then print("Player Has been infected") end end end end)
there are. more than a few things wrong with this
the specific error is caused by you trying to call GetPlayerFromCharacter on the LocalPlayer
also, this should be a server script in StarterCharacterScripts, since changes made on the client don't replicate to the server, meaning that the other player never gets infected
local Players = game:GetService("Players") -- this isn't a super duper necessary thing but making services use PascalCase instead of camelCase makes it clearer what your code does, since it's pretty common practice local ReplicatedStorage = game:GetService("ReplicatedStorage") local char = script.Parent local player = Players:GetPlayerFromCharacter(char) char:WaitForChild("Humanoid").Touched:Connect(function(hit) local touchedPlayer = game.Players:GetPlayerFromCharacter(hit.Parent) if touchedPlayer and hit.Parent:FindFirstChild("Humanoid") and not touchedPlayer:FindFirstChild("IsInfected") and player:FindFirstChild("IsInfected") then -- you only need one if statement for this local boolValue = Instance.new("BoolValue", touchedPlayer) boolValue.Name = "IsInfected" boolValue.Value = true game:GetService("ReplicatedStorage").VirusUI:Clone().Parent = touchedPlayer.PlayerGui print("Player has been infected") -- there's no reason to check if the gui was cloned, since it'll ALWAYS be cloned no matter what end end)
You have maybe forget a "end" for your if not, because I don't see it here