Simple "Respawn" Function Bugging Out Only On First Run

I’m trying to create a simple dummy system, where a dummy can be attacked for xp and gold. In concept the idea is very simple; however, I’ve run into one issue. The loop in the respawn function doesn’t run the first time its called for any dummy as in even If I’ve already called the function multiple times for one dummy, the loop still wont run on the first function call of any other dummy. In essence, I want to know if I am either overlooking something extremely obvious or if this is a bug?

I can guarantee that the there are children to dummy.Stats.DmgDealt which are Int Values even on the first run through that i’m referring to.

For fixes I’ve tried force running the function for each dummy when the the server loads.
In fact, ive done so in many ways, such as simply calling the function and dropping the dummies health to 0 while adding int value tags to under the dummy.Stats.DmgDealt. In the end even using this roundabout method the first time I manually attacked the dummy and it died, the loop did not run.
Just to humor myself I even tried moving the function to a module script to no avail.

local dummies = game.Workspace:WaitForChild("DummiesFolder")
local DL = game:GetService("ReplicatedStorage"):WaitForChild("BindableEvents"):WaitForChild("DummyLoot")
local DummyModule = require(script.Parent.DummyRespawn)

local function respawn(dummy, waitTime)
	local dmg = {}

	for i,v in pairs(dummy.Stats.DmgDealt:GetChildren()) do
		print("Test")
		if v:IsA("IntValue") then
			local plrTable = {v.Name, v.Value}
			print (plrTable[1] .. ":" .. plrTable[2])
			table.insert(dmg, plrTable)
		end
		v:Destroy()
	end
	
	DL:Fire(dummy.Stats.Level.Value, dmg)
	wait(waitTime)
	dummy.Stats.Health.Value = dummy.Stats.Level.Value * 100
end

for i,v in pairs(dummies:GetChildren()) do
	if v:IsA("Model") then
		local dummy = v
		local stats = dummy.Stats
		local Health = stats.Health
		local lvl = stats.Level
		
		local hpFr = dummy.HPPart.BillboardGui.Frame.Frame
		local hpText = dummy.HPPart.BillboardGui.Frame.TextLabel
		
		Health.Changed:Connect(function()
			hpFr.Size = UDim2.new(Health.Value/(lvl.Value*100),0,1,0)
			hpText.Text = Health.Value .. "/" .. lvl.Value*100
			if Health.Value <= 0 then
				respawn(dummy, 5)
				--DummyModule.Respawn(dummy, 5)
			end
		end)
		
		Health.Value = 0 -- Test -- Loop Does Run; However, for the following actual attack it does not
	end	
end

1 Like

I don’t know if this is something you meant to do, or you just overlooked it but the part where you get the children of your dummies folder isn’t in the respawn function it’s outside of it. Also don’t use V twice use a different letter like x

1 Like

That is intentional
What I am referring to is this loop.

	for i,v in pairs(dummy.Stats.DmgDealt:GetChildren()) do
		print("Test")
		if v:IsA("IntValue") then
			local plrTable = {v.Name, v.Value}
			print (plrTable[1] .. ":" .. plrTable[2])
			table.insert(dmg, plrTable)
		end
		v:Destroy()
	end

Also in this case different letters will not make a difference; however, I have already tried that.

hmm, in that case, you might want to try trial and error, It’s quite tedious but add or replace lines of code, add print values to see how far your code is getting, etc. this is usually my way of finding bugs. If you’ve already tried that maybe look at the module maybe something is wrong there. Wish I could be more help.

I’ve done trial and error for several hours now. Decided to remove all the prints as to not fill up this post and confuse you guys. Also the module just contains a copy of the respawn function for testing purposes which in the above script isn’t being called to anyway. I’ve also had the loop nested within the other loop and have done a ton more debugging

Also all good, this seems to be quite a weird issue.

Sorry but here’s a bump, as I am still clueless after more digging.

You could clone a script and parent it to the dummy each time a child named “dummy” or a child in a certain folder are being added?

Well that isn’t really addressing the issue. Also the dummies are permanent, as in they aren’t actual respawning but instead just regaining health.
In the name of debugging though I will clone a script to each dummy and see what happens.

Looks like a simple wait right before the loop “fixed” the issue. Guess i’ll leave it at that for now.