Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I am trying to make a virus but i get, attempt to index nil with 'FindFirstChild' how to fix?

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)

3 comments

0

did the error occur at line 5 or 6, the output should say

0

You have maybe forget a "end" for your if not, because I don't see it here

0

There is no EOF exception. You may be using a Server-sided script, therefore, the LocalPlayer pointer won't refer to a Client affiliated Player Object.

2 answers

✓ Accepted answer

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

1 comment

0

the "end" is there, plus the error is referring to 'findfirstchild' not expected eof

This discussion is preserved as it was. New answers, comments, and votes are closed.