Attempt to index nil with 'Name'

Greetings,

I am getting this error on my code and I really don’t know why. Please help, thanks!

The error:
image

The code:

task.wait(3)
--Services
local players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

--Events
local SendGUI = RS:WaitForChild("Racing"):WaitForChild("SendGUI")
local Bindable = RS:WaitForChild("Racing"):WaitForChild("Bindable")

--Items
local Racetrack1 = script.Parent
local PlayersFolder = Racetrack1.Players

local FinishLine = Racetrack1.FinishLine
local Participants = Racetrack1.Participants
local Ongoing = Racetrack1.Ongoing

local debounce = false
local place = 0

FinishLine.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		
		warn("I have received the touch")
		
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if Ongoing.Value == true then
			warn("I've seen that the value is true")
			if players:FindFirstChild(player.Name) and PlayersFolder:FindFirstChild(player.Name) then --This is where the error is located
				warn("I have found player, so both of the things are found.")
				place = Participants.Value - (Participants.Value - 1)

				PlayersFolder:WaitForChild(player.Name).Value = place
				
				local otherserver = 2 
				Bindable:Fire(otherserver, player)
				warn("I've also fired the bindable event to the script in sss, my job is done here :D")
			end
		end
		
		wait(0.1)
		debounce = false
	end
end)

This returns nil, if it did not hit a player.

2 Likes

It’s because any part that touches the part is trying to index player. Add an if statement to make sure that player isn’t nil.

if player then
—do stuff
end
1 Like

Okay, the problem is you are not checking if the player does exist (before trying to index .Name in your checks). If the :GetPlayerFromCharacter() function cannot get a player (e.g. the hit.Parent is not a player model) it will return nil and therefore you will get the error. The corrected version of your script that checks if there is a player that has been returned is:

task.wait(3)
--Services
local players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

--Events
local SendGUI = RS:WaitForChild("Racing"):WaitForChild("SendGUI")
local Bindable = RS:WaitForChild("Racing"):WaitForChild("Bindable")

--Items
local Racetrack1 = script.Parent
local PlayersFolder = Racetrack1.Players

local FinishLine = Racetrack1.FinishLine
local Participants = Racetrack1.Participants
local Ongoing = Racetrack1.Ongoing

local debounce = false
local place = 0

FinishLine.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		
		warn("I have received the touch")
		
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if player and Ongoing.Value == true then --Check if the player exists on this line.
			warn("I've seen that the value is true")
			if players:FindFirstChild(player.Name) and PlayersFolder:FindFirstChild(player.Name) then --This is where the error is located
				warn("I have found player, so both of the things are found.")
				place = Participants.Value - (Participants.Value - 1)

				PlayersFolder:WaitForChild(player.Name).Value = place
				
				local otherserver = 2 
				Bindable:Fire(otherserver, player)
				warn("I've also fired the bindable event to the script in sss, my job is done here :D")
			end
		end
		
		wait(0.1)
		debounce = false
	end
end)
1 Like

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