I have a LocalScript inside a TextLabel which is a descendant of PlayerGui that constantly checks if a player has a certain tool in their inventory. The script itself works fine, however, when the player resets or dies, the script breaks. The output says: ":7: attempt to index a nil value.
The output is basically saying after the player respawns, the LocalPlayer is nil. But, there's a line of code that's supposed to prevent that.
repeat wait() until game.Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer.Character
while wait() do
if game.Players.LocalPlayer then
local get = game.Players.LocalPlayer
local backpackgun = game.Workspace:findFirstChild(get.Name):findFirstChild("Long Bow")
local gun = get.Backpack:findFirstChild("Long Bow")
if not gun and not backpackgun and script.Parent.Text == "WEAPON OWNED" then
script.Parent.Text = "BUY?"
end
end
end
Can anyone help me out?
EDIT: I changed the script to this:
repeat wait() until game.Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer.Character
while wait() do
if game.Players.LocalPlayer and game.Players.LocalPlayer.Character then
local get = game.Players.LocalPlayer
local backpackgun = game.Workspace:findFirstChild(get.Name):findFirstChild("Long Bow")
local gun = get.Backpack:findFirstChild("Long Bow")
if not gun and not backpackgun and script.Parent.Text == "WEAPON OWNED" then
script.Parent.Text = "BUY?"
end
end
end
but the output stays the same and the script breaks.

3 comments
But what about line 2?
There is no need to wait for the client's character in my script. It checks to see if they have a character.
Oh, I see what you meant. I made it keep reference to the script's former parent. Thank you for mentioning it.