Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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)

0
did the error occur at line 5 or 6, the output should say Gmorcad12345 434 — 4y
0
You have maybe forget a "end" for your if not, because I don't see it here Geohad0926 5 — 4y
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. Ziffixture 6913 — 4y

2 answers

Log in to vote
1
Answered by
Elyzzia 1294 Moderation Voter
4 years ago

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)

Ad
Log in to vote
0
Answered by 4 years ago

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

0
the "end" is there, plus the error is referring to 'findfirstchild' not expected eof Gmorcad12345 434 — 4y

Answer this question