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

Attempt to index nil with name??

This is a teleport script, but for some reason I am getting a "Attempt to index nil with name Line 5" and I don't know why. Any help is really appreciated!

local Pad = game.Workspace.SecuritySystem.Pad2

script.Parent.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Name = Player.Name
	if Player then
		local CurrentlyTeleporting = Player.Character:FindFirstChild("CurrentlyTeleporting")
		if not CurrentlyTeleporting then return end
		
		if not CurrentlyTeleporting.Value then
			CurrentlyTeleporting.Value = true
			Player.Character.HumanoidRootPart.CFrame = Pad.CFrame + Vector3.new(0,5,0)
			print(Name,"tried to rob the bank while cooldown was active.")
			wait(3)
			CurrentlyTeleporting.Value = false
		end
	end
end)

2 answers

You should get the Name after checking if it's a player.

local Pad = game.Workspace.SecuritySystem.Pad2

script.Parent.Touched:Connect(function(Hit)
	local Player = game:GetService('Players'):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		-- get the name AFTER you check if it's a player
		local Name = Player.Name
		-- rest of your code
	end
end)

This maybe the problem Player.Name is usually not caps like this player.Name Secondly I don't recommend having a local variable for player.Name. Also if any of those didn't work try this

function hit(player)

Put this before player.Name Hope that helps!!

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