Attempt to index nil with FindFirstChild - lua

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.

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

Roblox Studio error: Workspace.Driver.OnSeated!:13: attempt to index nil with ''Name''

I am having an error with one of my scripts in Roblox Studio. The line that shows that is a problem is Line 13. The script is for basically to weld when somebody sits on the seat, and to start up the TankGUI (Another script that is fine).
seat = script.Parent
function onChildAdded(part)
if (part.className == "Weld") then
while true do
local welde = seat:FindFirstChild("SeatWeld")
if (welde ~= nil) then
local sitted = welde.Part1.Parent
end
if (sitted.Name ~= script.Parent.Owner.Value) then
if (script.Parent.Owner.Value == "NoOne") then
script.Parent.Owner.Value = sitted.Name
print ("sitted steal")
else
print ("stolen!!")
local shipstealer = game.Workspace:FindFirstChild(sitted.Name)
if (shipstealer ~= nil) then
shipstealer.Humanoid.Jump=true
end
end
end
wait(0.2)
if (welde == nil) then
print("BreakLoop")
script.Parent.Owner.Value = "NoOne"
break end
end
end
end
--while true do
-- wait(0.5)
-- script.Parent.Parent.Name=script.Parent.Owner.Value .. "'s Ship"
--end
seat.ChildAdded:connect(onChildAdded)
Please excuse any poor co-operation or language barriers I'm having. I'm still a bit new here.
if (welde ~= nil) then
local sitted = welde.Part1.Parent
end
if (sitted.Name ~= script.Parent.Owner.Value) then
Since you're declaring sitted as local, it goes out of scope at end, so it's gone when you try to use it in that if, so you're actually using a nil global variable there. Do this instead:
local sitted
if (welde ~= nil) then
sitted = welde.Part1.Parent
end
if (sitted.Name ~= script.Parent.Owner.Value) then
That way it'll stay in scope until you're actually done with it.

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

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.

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

.lua error 'then' expected near 'else'

I am having this error, can't find the error.
local choice_revive = {function(player,choice)
local user_id = vRP.getUserId(player)
if user_id ~= nil then
vRPclient.getNearestPlayer(player,{10},function(nplayer)
local nuser_id = vRP.getUserId(nplayer)
if nuser_id ~= nil then
vRPclient.isInComa(nplayer,{}, function(in_coma)
if in_coma then
if vRP.tryGetInventoryItem(user_id,"medkit",1,true) else
vRP.tryGetInventoryItem(user_id,"smartwatch",1,true)
io.write("Smartwatch: Tilkalder Ambulance")
then
vRPclient.playAnim(player,{false,revive_seq,false}) -- anim
SetTimeout(15000, function()
vRPclient.varyHealth(nplayer,{50}) -- heal 50
end)
end
else
vRPclient.notify(player,{lang.emergency.menu.revive.not_in_coma()})
end
end)
else
vRPclient.notify(player,{lang.common.no_player_near()})
end
end)
end
end,lang.emergency.menu.revive.description()}
I have tried to make, and look for error, but without luck.
- If anyone can fix it, tell me please.
if statement, functions and for statement must be ended with end.
this code must be like that
--Settings--
local Tunnel = module("vrp", "lib/Tunnel")
local Proxy = module("vrp", "lib/Proxy")
vRP = Proxy.getInterface("vRP")
vRPclient = Tunnel.getInterface("vRP","vRP_smartwatch")
if in_coma then
vRP.tryGetInventoryItem(user_id,"smartwatch",1,true)
io.write("Smartwatch: Tilkalder Ambulance")
end
function vRP.sendServiceAlert(sender, emergency,x,y,z, msg)
local service = services[service_name]
local answered = false
if service then
local players = {}
for k,v in pairs(vRP.rusers) do
local player = vRP.getUserSource(tonumber(k))
-- check user
if vRP.hasPermission(k,service.alert_permission) and player ~= nil then
table.insert(players,player)
end
end
end
end

Resources