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

why is there an attempt to index nil with 'Name' error?

Asked by 3 years ago
t = {}

script.Parent.Parent.Activated:Connect(function(g)
    local t = game.Players:FindFirstChild(g.Name)
    t.leaderstats.Thirst.Value += 4
end)

i searched on the first page of google and i couldnt solve it. some solutions led me to another error "argument 1 missing or nil"

t = {}

script.Parent.Parent.Activated:Connect(function(g)
    local t = game.Players:FindFirstChild(g)
    t.leaderstats.Thirst.Value += 4
end)

Im trying to make a water bottle and when it's activated it increases the thirst by 4 on the leaderboard

local DataStore = game:GetService("DataStoreService")

local ds = DataStore:GetDataStore("PointsSaveSystem")
local db = DataStore:GetDataStore("ThirstSaveSystem")
local df= DataStore:GetDataStore("Food")

game.Players.PlayerAdded:connect(function(player)

    local leader = Instance.new("Folder",player)

    leader.Name = "leaderstats"

    local Points = Instance.new("IntValue",leader)

    Points.Name = "Bucks"

    Points.Value = ds:GetAsync(player.UserId) or 30

    ds:SetAsync(player.UserId, Points.Value)
    -------------------------------------------------------
    local Thirst = Instance.new("IntValue",leader)

    Thirst.Name = "Thirst"

    Thirst.Value = db:GetAsync(player.UserId) or 7

    db:SetAsync(player.UserId, Thirst.Value)
---------------------------------------------
    local Food = Instance.new("IntValue",leader)

    Food.Name = "Food"

    Food.Value = df:GetAsync(player.UserId) or 10

    df:SetAsync(player.UserId, Food.Value)



end)

game.Players.PlayerRemoving:connect(function(player)

    ds:SetAsync(player.UserId, player.leaderstats.Points.Value)
    db:SetAsync(player.UserId, player.leaderstats.Points.Value)
    df:SetAsync(player.UserId, player.leaderstats.Points.Value)

end)



(code of the datastores and leaderboard)

0
do you know which line on which script the game tells you that erorred? proROBLOXkiller5 112 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

The term 'Attempt to index nil' means that your code is attempting to index something that doesn't exist.

For example:

local Variable = game:FindFirstChild("SomethingThatDoesntExist")

Variable.SomeProperty = Something

Would result in the error 'Attempt to index nil with SomeProperty'. However, Roblox doesn't see the error when it first happens because of the WaitForChild() function.

If we just set the variable directly like so:

local Variable = game.SomethingThatDoesntExist

Variable.SomeProperty = Something

We would get an error saying 'SomethingThatDoesntExist is not a valid member of game' The reason we don't get an error right away is because, if WaitForChild() doesn't find the child it returns Nil. This is why you got the error 'Attempt to index nil'.

0
Your function input g doesn't exist. No matter what you do it will error. wwwaylon09 113 — 3y
Ad

Answer this question