Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to detect when a number variable is subtracted?

I am trying to detect when a tree or a rock in the workspace disappears, but I can't figure it out. My only guess is to detect if a a tree/rock was destroyed and then fire it to all clients. However, what if I lose two at a time? Then it won't fire. Can someone help me? This is my attempt.

local rEvent = workspace.ResourceEvent

while wait(1) do
	oldnumber = 0
	for _,v in pairs(workspace:GetChildren()) do
		if v:IsA("Model") and v.Name == "Tree" or v:IsA("Model") and v.Name == "Rock" then
			oldnumber = oldnumber + 1
		end
	end
	
	newnumber = 0
	for _,v in pairs(workspace:GetChildren()) do
		if v:IsA("Model") and v.Name == "Tree" or v:IsA("Model") and v.Name == "Rock" then
			newnumber = newnumber + 1
		end
	end

	if newnumber > oldnumber then
		workspace.ResourecEvent:FireServer() -- Another problem, how do I detect if I lost a rock/tree? It could be both.
	end
end

3 comments

0

Change it to wait(0). I'm sure that will fix your problem.

0

it only made it run quicker

0

Could you then please explain your problem better? From what you've said, I only understand that you want it to Fire everytime a Rock/Tree dissapears.

1 answer

✓ Accepted answer

Well, from what you've said it seems you need to detect if a Rock/Tree dissapears, and Fire every time one dissapears.

For this, you can try giving the Events an attempt. For this situation, your best event would be Workspace.ChildRemoving. Workspace.ChildRemoving runs every time an instance is removed from workspace.

For your issue I would try this:

local rEvent = workspace.ResourceEvent

workspace.ChildRemoving:connect(function(v)
        if v:IsA("Model") and v.Name == "Tree" or v:IsA("Model") and v.Name == "Rock" then
            workspace.ResourecEvent:FireServer()
        end
end)

Yup. It takes all of those lines away. As I can't fully understand your issue, I hope this helps you and patches your issue. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.