.Heartbeat problems

Hey so im trying to code an entity for my game and it seems as if i cant get around this .Heartbeat problem. What i want it to do is fire every one second and then disconnect after 10 seconds. The reason why i cant use a while true do loop is because i want it to disconnect after 10 seconds and i dont know how to do that. Any help is appreciated.

	spawn(function()
			print("Spawn function")
			uhdfasndhi = game:GetService("RunService").Heartbeat:Connect(function()
				wait(1)
				print("Damaged")
			
				hum:TakeDamage(5)
			end)
			task.wait(10)
			print("Disconnecting")
			uhdfasndhi:Disconnect()
			script.Parent:Destroy()
		end)
1 Like

I think you could use a for loop instead of using the heartbeat connection.

Example:

spawn(function()
	print("Spawn function")
    for i = 1, 10 do
        task.wait(1)
        print("Damaged")
        hum:TakeDamage(5)
    end
	script.Parent:Destroy()
end)

Loops 10 times waiting 1 second inside each loop.

3 Likes

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