Attempt to index nil with 'Text'

Hello, I made a flashlight that had a battery system. Everything seems to worked pretty fine but when I die and I respawn again, I encountered an error. I thought that the error came from defining variables so I tried to used :WaitForChild and :FindFirstChild but after all, the flashlight didn’t work and it’s just no error in the output

This is the local script before my editing inside the flashlight:


--// Please like and favourite the model if it helped you! Made by ZorkyVFX [Halfwire Productions] \\--

local uis = game:GetService("UserInputService")
local rep = game:GetService("ReplicatedStorage")
local mouse = game.Players.LocalPlayer:GetMouse()
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local s = require(game.ReplicatedStorage.Settings)

local tool = script.Parent
local gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("FlashLight")
local messages = gui:WaitForChild("Messages")	
local LightUI = gui.LightUI
local BatteryF = gui.BatteryF

local batAdd = rep:WaitForChild("BatteryAdd")
local batRemove = rep:WaitForChild("BatteryRemove")
local flashlight = rep:WaitForChild("Flashlight")

local equipped = false
local msgCooldown = true
local state = false
local power = s.PowerFromStart

if power then
	for i,v in ipairs(BatteryF:GetChildren()) do
		v.ImageTransparency = 0
	end
end

tool:WaitForChild("Handle").Transparency = 1

local function msg(text, Wait)
	
	if msgCooldown then
		msgCooldown = false
		messages:FindFirstChild("Message").Text = text
		messages.Message.TextTransparency = 0
		wait(Wait)
		for i = 0,1,.05 do
			messages.Message.TextTransparency = i
			wait(.01)
		end
		messages.Message.TextTransparency = 1
	end
	msgCooldown = true
end

local function activate()
	if equipped then
		tool.Handle.Sound:Play()
		if power then
			if state ~= true then
				flashlight:FireServer(true, tool)
				state = true
				if s.FlashlightUI then
					LightUI.Visible = true
				end
			else
				flashlight:FireServer(false, tool)
				state = false
				LightUI.Visible = false
			end
		else
			msg("You need to recharge the flashlight",.5)
		end
	else
		flashlight:FireServer(false, tool)
	end
end

uis.InputBegan:Connect(function(input)
	if equipped == true then
		if input.KeyCode == Enum.KeyCode.R or input.KeyCode == Enum.KeyCode.ButtonX then
			local result = batRemove:InvokeServer(power)
			if result == "recharge" then
				for i,v in ipairs(BatteryF:GetChildren()) do
					v.ImageTransparency = 0
				end
				power = true
				msg("Flashlight recharged",.5)
			elseif result == "stillPower" then
				msg("You still have some power left in your battery",.5)
			elseif result == "noBat" then
				msg("You don't have any batteries",.5)
				power = false
			end
		elseif input.KeyCode == Enum.KeyCode.ButtonR1 then
			activate()
		end
	end
end)

mouse.Button1Down:Connect(function()
	if equipped == true then
		activate()
	end
end)

rep.PickUp.OnClientEvent:Connect(function(flashlight, destroy)
	if destroy then
		flashlight:Destroy()
	end
	if s.PickUpMsg then
		if s.PickUpText ~= "" then
			msg(s.PickUpText, s.PickUpTextTime)
		end
	end
end)

if character:FindFirstChild("HumanoidRootPart"):FindFirstChild("SurfaceLight") then
	local surfaceLight = character.HumanoidRootPart.SurfaceLight
	
	surfaceLight.Changed:Connect(function(property)
		if property == "Enabled" then
			if surfaceLight.Enabled == false then
				if BatteryF.d.ImageTransparency == 1 then
					power = false
					LightUI.Visible = false
				end
			end
		end
	end)
end


tool.Equipped:Connect(function()
	equipped = true
	BatteryF.Visible = true
end)

tool.Unequipped:Connect(function()
	equipped = false
	activate()
	state = false
	BatteryF.Visible = false
	LightUI.Visible = false
end)

this is the full output:

 09:03:24.122  Players.TeemoHoang_2008.Backpack.Flashlight.Flashlight:37: attempt to index nil with 'Text'  -  Client - Flashlight:37
 09:03:24.122  Stack Begin  -  Studio
 09:03:24.122  Script 'Players.TeemoHoang_2008.Backpack.Flashlight.Flashlight', Line 37 - function msg  -  Studio - Flashlight:37
 09:03:24.122  Script 'Players.TeemoHoang_2008.Backpack.Flashlight.Flashlight', Line 81  -  Studio - Flashlight:81
 09:03:24.122  Stack End  -  Studio

this is a picture of the explorer:

Try defining a separate variable with the FindFirstChild function call and then index that variable with text.

I not really understand, can you provide some code?

This is predictable, since when a player respawns it clears the player’s Backpack and PlayerGui .

what’s wrong in my code? can you point it out?

When you respawn, wait for a while then use the tool.

messages:FindFirstChild("Message").Text = text

This is where your issue lies, it should be fairly self explanatory.

1 Like