Issue with task.wait()

I’m trying to make a simple script that changes a billboardgui’s imagelabel constantly. When i made the script, i first used wait(), and it worked perfectly fine, now that i see the new feature of task.wait(), i replaced the waits with it, and i keep getting this error in either lines 5, 7 or 9 randomly: attempt to index nil with 'ImageLabel'. The script is inside the billboardgui.

while true do
	wait(0.3)
	if script.Parent.ImageLabel ~= nil then
		if script.Parent.ImageLabel.Image ~= nil then
			script.Parent.ImageLabel.Image = game.ServerStorage.Extra.DeleteDecal["1"].Texture
			task.wait(0.3)
			script.Parent.ImageLabel.Image = game.ServerStorage.Extra.DeleteDecal["2"].Texture
			task.wait(0.3)
			script.Parent.ImageLabel.Image = game.ServerStorage.Extra.DeleteDecal["3"].Texture
		end
	end
end

The 2 if checks weren’t originally there, i added them to check if that fixed the problem, but no, they didn’t.

The billboardgui is inside a folder in serverstorage, and the script is disabled, i have another script as well that clones it and enables the script. After some seconds, the error appears, but somehow, the image keeps changing normally.

Can someone help me? This feature is new and i don’t really know what to do.

Maybe add a WaitForChild when mentioning the ImageLabel. Although im not actually sure if this would make a difference.
Try this code:

local ServerStorage = game:GetService("ServerStorage")
local Extra = ServerStorage:WaitForChild("Extra")
local DeletedDecal = Extra.DeletedDecal
local ImageLabel = script.Parent:WaitForChild("ImageLabel")

while task.wait(0.3) do
	if ImageLabel ~= nil and ImageLabel.Image ~= nil then
		ImageLabel.Image = DeleteDecal["1"].Texture
		task.wait(0.3)
		ImageLabel.Image = DeleteDecal["2"].Texture
		task.wait(0.3)
		ImageLabel.Image = DeleteDecal["3"].Texture
	end
end
3 Likes

It works! thank you, i’ve had this issue for a long time.

1 Like