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

roblox lua error Workspace.Door.doorscript:2: attempt to index nil with 'Name'?

Asked by 3 years ago

hello i cant figure out this error it is confusing cause i did so many tests and checks here is the error i'm getting Workspace.Door.doorscript:2: attempt to index nil with 'Name'

local door = script.Parent
local Name = game.Players.LocalPlayer.Name
local tweenservice = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(
    0.5,
    Enum.EasingStyle.Linear,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)
local openpos  =  {CFrame = CFrame.new(-166, 7, 195.5)}
local closepos = {CFrame = CFrame.new(-197, 7, 195.5)}
local open = tweenservice:Create(script.Parent,tweeninfo,openpos)
local close = tweenservice:Create(script.Parent,tweeninfo,closepos)

local function touch(hit)
    -- open
    if Name == "AxisAgent" or Name == "babychickless" or Name == "Diamondboy431" or Name == "CoolBeckett102" then
    open:Play()
    else
    close:Play()    
    end
    wait(0.1)
    -- close
end

local function close(hit)
    close:Play()
end

script.Parent.Touched:Connect(touch)
script.Parent.Hitbox2.Touched:Connect(close)

2 answers

Log in to vote
0
Answered by 3 years ago

Hello, is this a LocalScript or Script and where? If it's a Script then you can't do game.Players.LocalPlayer.Name, and if it's a LocalScript, your errors is basically saying LocalPlayer is nil because it says Attempt to index nil with "Name" so the index is LocalPlayer. I recommend getting the player and then the name in different variables like this:

local player = game.Players.LocalPlayer
local name = player.Name

ONLY IF IT'S A LOCALSCRIPT! and if it's a Script, you can get the player by doing:

game.Players.PlayerAdded:Connect(function(player) --Gets the local player by an event.

That will run and get the local player every time someone joins. Only do that line if it's a Script. Hope this answer helps you and good luck! :D

0
Thank You For The Help CallMe_Axis 63 — 3y
0
Np, can u probably give me reputation as i helped you pls? LetalNightmare 99 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You're trying to get the LocalPlayer on the server, which won't work because the server isn't associated with any of clients so it will just return nil instead. You can only get the LocalPlayer on the client.

Instead, you can get the player by using the GetPlayerFromCharacter function inside the touched event.


local door = script.Parent local tweenservice = game:GetService("TweenService") local Players = game:GetService("Players") local tweeninfo = TweenInfo.new( 0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local openpos = {CFrame = CFrame.new(-166, 7, 195.5)} local closepos = {CFrame = CFrame.new(-197, 7, 195.5)} local open = tweenservice:Create(script.Parent,tweeninfo,openpos) local close = tweenservice:Create(script.Parent,tweeninfo,closepos) local function touch(hit) if Players:GetPlayerFromCharacter(hit.Parent) then -- Checking if hit.Parent has a player local Player = Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player from hit.Parent local Name = Player.Name -- open if Name == "AxisAgent" or Name == "babychickless" or Name == "Diamondboy431" or Name == "CoolBeckett102" then open:Play() else close:Play() end wait(0.1) -- close end end local function close(hit) close:Play() end script.Parent.Touched:Connect(touch) script.Parent.Hitbox2.Touched:Connect(close)
0
Pretty Much Works There Is One Small Problem With The last Function. its saying it cant index with the play animation CallMe_Axis 63 — 3y

Answer this question