ModuleScript only detecting a NumberValue the first time

ModuleScript:

module.DamageCoin = function(c, h, plrs)
	for i,v in pairs(plrs) do
		c:WaitForChild("Health").Value -= h
		if c:WaitForChild("Health").Value == 0 then
			count = count - 1
			c:Destroy()
			CallClientsForDestroy(c)
		else
			CallClientsForUpdate(c, v)
		end
	end
end

This function of a ModuleScript is only detecting the Health NumberValue the first time the function runs. The second time it shows this warning:

imagen

But the coin has the Health NumberValue:
imagen

I have searched in the devforum, but I didn’t find anything.

2 Likes

I assume that the function CallClientsForDestroy(c) is related to the destruction of the parameter, in this case c. Your NumberValue Health is stored within the parameter c, and if it is destroyed or the children of c are, the program will be yielding infinitely.

Infinite yield possible essentially means that there is a possibility that a thread will be yielding (or waiting) infinitely. This is a common issue with :WaitForChild().

By destroying c with the function CallClientsForDestroy(c), the program will be continuously waiting for the NumberValue Health, but since it no longer exists, the program will wait infinitely.

I recommend revisiting the function CallClientsForDestroy(), as that is likely the cause for your issue.

Furthermore, it is highly recommended to limit the use of :WaitForChild() for this exact reason. Alternatively, you can use :FindFirstChild(), which will look for a child, rather than wait for a child. The primary purpose for :WaitForChild() is to wait for instances when a client joins a server (Local Script); instances are already loaded into the server so Server Scripts and Module Scripts do not have to rely on :WaitForChild() as much (but this does not mean that it is forbidden to use).

What im making is a pet simulator with a friend, CallClientsForDestroy makes all of the clients which damaged that coin, delete the bilboard (I made that function). By the way, I found the error and it wasnt in this script. Thank you for your help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.