Roblox Error: attempt to index local 'screengui' (a nil value) - lua

Workspace.Part.Script:16: attempt to index local 'screengui' (a nil value)
wait(2) -- Testing to make sure assets loaded
script.Parent.Touched:connect(function(hit)
if not hit or not hit.Parent then return end
local human = hit.Parent:findFirstChild("Humanoid")
if human and human:IsA("Humanoid") then
local person = game.Players:GetPlayerFromCharacter(human.parent)
if not person then return end
person.Checklist.FirstEggCollected.Value = true
local playgui = person:FindFirstChild('PlayerGui')
print(playgui)
wait(0.2)
local screengui = playgui:FindFirstChild('ScreenGui')
wait(0.2)
print(screengui) -- This prints nil
local collectnotice = screengui:FindFirstChild('CollectionNotice') -- This is line 16
local Toggle = collectnotice.Toggle
local text = Toggle.Text
local value = text.Value
value = "The Easy Egg!"
person:WaitForChild('PlayerGui'):FindFirstChild('ScreenGui'):FindFirstChild('CollectionNotice').Toggle.Color.Value = Color3.fromRGB(0,255,0)
person:WaitForChild('PlayerGui'):FindFirstChild('ScreenGui'):FindFirstChild('CollectionNotice').Toggle.Value = true
script.Parent:Destroy()
wait(5)
game.Workspace.Variables.IfFirstEggInGame.Value = false
end
end)
I've been at this for hours. No idea how to make the error fix. FE is on, Yes its name is "ScreenGui" and it is inside "PlayerGui"
Error: Workspace.Part.Script:16: attempt to index local 'screengui' (a nil value)

From the Roblox manual: http://wiki.roblox.com/index.php?title=API:Class/Instance/FindFirstChild
Description: Returns the first child found with the given name, or nil
if no such child exists.
So there seems to be no child named "ScreenGui".
If a function may return nil you have to handle that properly. Blindly indexing possible nil values is bad practice.

Your issue is at the line local collectnotice = screengui:FindFirstChild('CollectionNotice').
You do not have an instance listed for the screengui variable.

Related

attempt to index nil with 'CFrame'

i was trying to make random spawns but sometimes, it gives me an error attempt to index nil with 'CFrame' when i enter the portal.
Here is the code
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') and not db and not hit.Parent.Parent:IsA("Tool") then
db = true
local spawns = workspace.Spawns
local spawnPoint = math.random(1,13)
local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local ss = game:GetService("ServerStorage")
plr.InLobby.Value = false
if UIS.TouchEnabled == true then
script.Ability:Clone().Parent = plr.PlayerGui
end
if not plr.Backpack:FindFirstChild(plr.leaderstats.Glove.Value) and plr.leaderstats.Glove.Value ~= "edgelord" or plr.leaderstats.Glove.Value ~= "ḇ̵̰̪̙̭̎̓o̴̰͈͊̈̓̓̕b̶̨̀͐͌͑͒" then
if ss:FindFirstChild(plr.leaderstats.Glove.Value) then
ss:FindFirstChild(plr.leaderstats.Glove.Value):Clone().Parent = plr.Backpack
hit.Parent.HumanoidRootPart.CFrame = spawns:FindFirstChild(spawnPoint).CFrame
wait(2)
db = false
end
end
end
end)
The "attempt to index nil" error is always telling you that the object you're trying to get a value out of doesn't exist.
In this case, it's probably spawns:FindFirstChild(spawnPoint) that doesn't exist. The FindFirstChild searches the names of the children, and you are passing in a number between 1 - 13. This makes an assumption about the names of the children that is unnecessary and is likely the source of your error.
A safer way to access these objects is to simply get all of the spawn points as an array, and then access a random index within that array.
local spawns = workspace.Spawns:GetChildren()
local randomIndex = math.random(1, #spawns)
local targetCFrame = spawns[randomIndex].CFrame
hit.Parent.HumanoidRootPart.CFrame = targetCFrame

Attempt to index nil with FindFirstChild

Script:
local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("SavedItems")
local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(player)
local ToolData = SaveData:GetAsync(player.UserId)
local BackPack = player:WaitForChild("Backpack")
local StarterGear = player:WaitForChild("StarterGear")
if ToolData ~= nil then
for i, v in pairs(ToolData) do
if ToolFolder:FindFirstChild(v) and BackPack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
ToolFolder[v]:Clone().Parent = BackPack
ToolFolder[v]:Clone().Parent = StarterGear
end
end
end
player.CharacterRemoving:Connect(function(Character)
Character:WaitForChild("Humanoid"):UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local ToolTable = {}
for i,v in pairs(player.Backpack:GetChildren()) do
table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
SaveData:SetAsync(player.UserId, ToolTable)
end
end)
Issue:
ServerScriptService.SaveTools:12: attempt to index nil with 'FindFirstChild'
Couldn't find a solution. Appreciate any help. :)
Any time you see "attempt to index nil with x" you need to look at where you are asking for "x" and realize that the object holding "x" doesn't exist. The job then becomes figuring out why that object doesn't exist.
In your case, whatever object that you are calling "FindFirstChild" on line 12 doesn't exist. Sadly, line 12 uses this three times :
if ToolFolder:FindFirstChild(v) and
BackPack:FindFirstChild(v) == nil and
StarterGear:FindFirstChild(v) == nil then
So let's look at where ToolFolder, BackPack, and StarterGear were created and see if that gives any clues.
local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("SavedItems")
...
local BackPack = player:WaitForChild("Backpack")
local StarterGear = player:WaitForChild("StarterGear")
Backpack and StarterGear looks correct, they are both children of the Player, and both look to be spelled correctly.
ToolFolder is probably the culprit, you should make sure there is actually an object named SavedItems in ServerStorage. Double check that the spelling and capitalization are correct.

What does "attempt to index nil with 'WaitForChild'" mean?

script.Parent.MouseButton1Click:connect(function()
local RS = game:GetService("ReplicatedStorage")
local item = RS:WaitForChild("Pencil")
local price = 350
local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
if stats.Strength.Value>=price then
stats.Strength.Value = stats.Strength.Value - price
local cloned = item:Clone()
cloned.Parent = player.Backpack
cloned.Parent = player.StarterGear
end
end)
I am trying to make a shop and it comes up with "attempt to index nil with 'WaitForChild'" on line 6:
local stats = player:WaitForChild("leaderstats")
I copied it exactly how the video had it and the video had no problem and apparently player has no value even though we set it up just one line above
It means that you are indexing a nil value, which means that player value is nil, which means that game.Players.LocalPlayer on the previous like returns nil. Why that is you need to figure out, as there is not much to go by.
The example shows that it should be local player = game:GetService("Players").LocalPlayer, so you may want to try that.

Roblox - attempt to index field '?' (a nil value) Issue with running a module function

I'm trying to make a datastore and change the value of player data, but I am having an issue getting the script to run the function. I will paste the code I'm having issues with and mark the lines giving errors. I apologize for any rookie mistakes, this'll be my first time programming a game with Lua and on Roblox. I think it may be an issue with how I call the statName and how a statName of "Wheat" isn't there, but I don't know how to call it otherwise, or why it isn't there.
This is the relevant stuff from the modulescript:
function PlayerStatManager:ChangeStat(player, statName, value)
local playerUserId = "Player_" .. player.UserId
assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match") <--this line
if typeof(sessionData[playerUserId][statName]) == "number" then
sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
else
sessionData[playerUserId][statName] = value
end
end
-- Function to add player to the 'sessionData' table
local function setupPlayerData(player)
local playerUserId = "Player_" .. player.UserId
local data
local success, err = pcall(function()
playerData:UpdateAsync(playerUserId, function(playerData)
data = playerData
end)
end)
if success then
if data then
-- Data exists for this player
sessionData[playerUserId] = data
else
-- Data store is working, but no current data for this player
sessionData[playerUserId] = {Money=0, Wheat=0, Silo=0, Feeders=0, Chickens=0}
end
else
warn("Cannot set up data for player!")
end
end
This is the relevant stuff from the script using the modulescript:
local SrvrStats = require(game.ServerStorage.moduleScript)
SrvrStats:ChangeStat(player, 'Wheat', playerWheat.Value) <-- this line
Try This:
local PlayerStatManager = {}
PlayerStatManager.ChangeStat = function(player, statName, value)
local playerUserId = "Player_" .. player.UserId
assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match") <--this line
if typeof(sessionData[playerUserId][statName]) == "number" then
sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
else
sessionData[playerUserId][statName] = value
end
end
-- Function to add player to the 'sessionData' table
PlayerStatManager.setupPlayerData = PlayerStatManager(player)
local playerUserId = "Player_" .. player.UserId
local data
local success, err = pcall(function()
playerData:UpdateAsync(playerUserId, function(playerData)
data = playerData
end)
end)
if success then
if data then
-- Data exists for this player
sessionData[playerUserId] = data
else
-- Data store is working, but no current data for this player
sessionData[playerUserId] = {Money=0, Wheat=0, Silo=0, Feeders=0, Chickens=0}
end
else
warn("Cannot set up data for player!")
end
end
return PlayerStatManager

attempt to index upvalue 'plr' (a nil value)

i have tried to fix the error but it didnt work plrase try help me fix it
heres the script:
local plr = game.Players.LocalPlayer
local items =game:GetService("ReplicatedStorage").Items
game:GetService("ReplicatedStorage").ClientPlaced.OnServerEvent:connect(function(player, itemName, location)
local itemTemplate = items:FindFirstChild(itemName)
if (itemTemplate) then
local item = itemTemplate:clone()
item.Parent = workspace:FindFirstChild(plr.Name .. "Base").ItemHolder
item:SetPrimaryPartCFrame(location[1])
end
end)
The error message tells you that your local variable plr is nil within the scope of the function you define. So you may not index it inside the function.
local plr = game.Players.LocalPlayer
game.Players.LocalPlayer is obviously nil
plr.Name causes the error.
As the function has a player argument this is most likely the variable you want to index. But I cannot know for sure as you don't provide all information.
Try player.Name instead

Resources