#how do i prevent "Attempt to index nil" error

1 messages · Page 1 of 1 (latest)

fallow pier
#
if player.Character:FindFirstChild(itemName) then
  player.Character:FindFirstChild(itemName):Destroy()
end

--OR

if player.Character:FindFirstChild(itemName) ~= nil then
  player.Character:FindFirstChild(itemName):Destroy()
end

If the thing is 'nil' ignore the code but instead it errors and the whole script breaks. both variants of this script breaks. how do i have the server only run the code if it isn't nil. And without using 'else' because else what? i dont want to put

else warn("no item")
``` because the code is within a for loop checking for 100 different items and that's 99 warns i dont want filling up the output
hearty harbor
fallow pier
#

test with 2 players

hearty harbor
fallow pier
#

so a task.wait(1) might fix it?

hearty harbor
#

do if player.Character and player.Character:FindFirstChild(itemName) then instead

fallow pier
#

ah okay

#

let me test that

#

yoo cheers btw i ended up using

local character = player.CharacterAdded:Wait()
#

i added SOLVED tag

soft breach
fallow pier
#

i assume because every time i call character its running the whole wait thing agian