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

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

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

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

2 comments

0

Thank You For The Help

0

Np, can u probably give me reputation as i helped you pls?

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)

1 comment

0

Pretty Much Works There Is One Small Problem With The last Function. its saying it cant index with the play animation

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