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

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

Related

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.

Data Store not saving the Player's Boolean Value

I was making a game that when the Player dies, then the Player will get banned but isn't working (The part of Saving the Bool Value that checks if the player was banned already been banned). How i can fix that? No errors in output.
Code:
local DataStoreService = game:GetService("DataStoreService")
local BANS = DataStoreService:GetDataStore("BANNED")
game.Players.PlayerAdded:Connect(function(Player)
local Char = Player.CharacterAdded:Wait() or Player.Character
local Hum = Char:WaitForChild("Humanoid") or Char.Humanoid
local banornot = Instance.new("Folder")
banornot.Name = "Banned"
banornot.Parent = Player
local banned = Instance.new("BoolValue", banornot)
banned.Name = "Banido"
banned.Value = false
local PlayerId = Player.UserId
local BANIDO = BANS:GetAsync(PlayerId)
Hum.Died:Connect(function()
if not BANIDO then
BANS:SetAsync(PlayerId, banned.Value)
Player:Kick("You can't join the game.")
end
end)
end)
game.Players.PlayerAdded:Connect(function(Player)
local PlayerId = Player.UserId
local banidovalue = Player:WaitForChild("Banned"):WaitForChild("Banido").Value
local BANIDO = BANS:GetAsync(PlayerId, banidovalue)
if BANIDO then
banidovalue = true
if banidovalue == true then
Player:Kick("You can't join the game. Sorry you know the rules.")
else print("erro")
end
end
end)
There are multiple issues with this.
The main issue is that BANIDO will never change. It will always have the value false, no matter what you do. Why? The only use of SetAsync takes the BoolValue's value, which also does not change. So, it's always false. There is a point in which you "set" it to true, except you don't. You take the value, put it into a variable, and set the variable to true. The actual BoolValue doesn't change. Even if you were to fix that, you kick the user right after that, which makes it useless.
Second, you don't need a BoolValue. I don't know why you used it, so I can't explain this part.
Here is a commented version of an improved script, in which I go through all of the issues:
local DataStoreService = game:GetService("DataStoreService")
local BANS = DataStoreService:GetDataStore("BANNED")
game.Players.PlayerAdded:Connect(function(Player)
local Char = Player.CharacterAdded:Wait() or Player.Character
local Hum = Char:WaitForChild("Humanoid") --No "or Char.Humanoid" is needed, WaitForChild waits until it finds the thing which makes that part useless.
--We don't need a BoolValue
local PlayerId = Player.UserId
local BANIDO = BANS:GetAsync(PlayerId)
if BANIDO then --I moved the check from the other connection to this one. Do not connect to an event twice in the same script if the necessities are the same.
Player:Kick("You can't join the game. Sorry you know the rules.")
end
--I did not do a check like this: BANIDO == true
--This is because BANIDO is already a boolean, true or false, and you're essentially saying "if this is true, give me true, if it's false, give me false" which is just the value
Hum.Died:Connect(function()
if not BANIDO then
BANS:SetAsync(PlayerId, true) --true instead of banned.Value. We got rid of the BoolValue, and also, even if we didn't, it wouldn't change.
Player:Kick("You can't join the game.")
end
end)
end)

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.

Getting a nil response while trying to clone a object to backpack

I am trying to clone an object from the replicatedstorage to the players backpack when an object part is touched and the code looks fine for me but it keeps giving a nil response from clone.parent = player.backpack
local replicatedtorage = game:GetService("ReplicatedStorage")
local Sword = replicatedtorage:FindFirstChild("Sword")
local part = game.Workspace.Part
local player = game.Players.LocalPlayer
local clone = Sword:Clone()
part.Touched:Connect(function(hit)
local humanoid = hit.parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
clone.Parent = player.Backpack
end
end)
This looks like a server Script which cannot access Players.LocalPlayer like clients can because there is no local player to the server. A way to get the Player that touched a part is through Players:GetPlayerFromCharacter() which requires one instance to be passed and will either return the Player whose Character is that instance or nil.
part.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
clone.Parent = player.Backpack
end
end)
This should work right away in your script and can replace your existing Touched connection.

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.

Resources