Run Service problem

Using GetChildern it does not execute twice, based on the information given. Something else is going on.

Place1.rbxl (53.6 KB)

1 Like

If you want to add an element to the table every time a new instance is added, you will need to refresh the table obtained by otherFolder just like that:

local run = game:GetService("RunService")
local otherFolder
local t = {}

run.Heartbeat:Connect(function()
	otherFolder = game.Workspace:WaitForChild("otherFolder"):GetChildren()
	
	if #t < #otherFolder then
		print(#t)
		table.insert(t, os.time())
	end

end)

Or you can literally put the variable on the Heartbeat.

But I think use the ChildAdded signal is a better way.

local run = game:GetService("RunService")
local otherFolder = game.Workspace:WaitForChild("otherFolder")
local t = {}

otherFolder.ChildAdded:Connect(function()
	table.insert(t, os.time())
end)
1 Like

I have it like this, but is not working how it should

if #t < #playerFolder:FindFirstChild("Defender"):GetChildren() then			
	table.insert(t, os.time())
end
1 Like

I am updating it in the runservice, but still running the code 2 times even if the folder has one element. I have other functions which are the same as this one and they run perfectly.

1 Like

FWIW, I thought I was talking to the OP at times when I was replying to you… my mistake.

I agree with everything you’ve said :slight_smile:

…and @Alchimo99 if you want to update table when the folder contents change, @NiniScripterXQc posted the best way to do this using ChildAdded.

Best of luck.

2 Likes

I have to check other conditions also not only when something is added to the folder. ChildAdded wont help in my case.

1 Like

Have you tried using repeat or while instead of Heartbeat?

1 Like

i think it would be good to post your actual code since bugs can appear for all sorts of reasons and giving a rewritten version might remove the bug
or i guess just use the rewritten version if it works

2 Likes

I just used while and is printing 2 time as the run service does.

Can you past this code here, because for me it’s working pretty good even in the Heartbeat

I think you probably made an error that I can’t currently see in the condition check because if it does it even in a repeat or a while then it’s a condition check error, I guess. :face_with_monocle:

2 Likes

I just re checked and the double execution goes only when I clone the object in the folder, but after the object respawn is working well. I don’t know why this is happening since the number of elements is one.

2 Likes

I think you said that you remove the element from the table after checking it? Did I understand correctly? Because if this is the case, and you empty your table and it contains 0 elements and the file has two elements, it is normal for this to trigger twice.

1 Like

I insert the time in the table only when I clone some mobs. After I clone the mobs I am checking if the attack speed (which is a value) is equal with the os.time() - the time from the table and then I execute the code and I remove the time from the table and the thing repeats until I get some conditions and after that the mobs de spawn or die. And when they respawn again a new time is chosen and the things repeats. The code double execute when I first time clone the mobs, but to the next cycle I noticed that is working good.

1 Like

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