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

Attempt to index nil with name??

Asked by 1 year ago

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

Log in to vote
1
Answered by
pwx 1581 Moderation Voter
1 year ago

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)
Ad
Log in to vote
-1
Answered by 1 year ago

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!!

Answer this question