How do I make a connection with a set timer?

I’m trying to make it so the RunService.HeartBeat has an interval?
I’m new to connections / disconnecting and I have no idea what I could use.

local RunService = game:GetService("RunService")
local Minigun : Tool = script.Parent
local Transfer = script.Parent:WaitForChild("TransferData")

Minigun.Equipped:Connect(function()
	Connection = RunService.Heartbeat:Connect(function()
		local Mouse = game.Players.LocalPlayer:GetMouse().Hit.Position
		Transfer:FireServer(Mouse)
	end)	
end)

Minigun.Unequipped:Connect(function()
	warn("Disconnected minigun")
	Connection:Disconnect()
end)

use task.wait() then whatever you want

1 Like

To expand on how you could use task.wait(), you can use the :Once connection to interrupt the otherwise infinite loop. Similar to disconnecting via .Unequipped:Connect

local RunService = game:GetService("RunService")
local Minigun : Tool = script.Parent
local Transfer = script.Parent:WaitForChild("TransferData")

Minigun.Equipped:Connect(function()
	local firing = true
	
	Minigun.Unequipped:Once(function()
		firing = false
	end)
	
	while firing and task.wait(1) do
		local Mouse = game.Players.LocalPlayer:GetMouse().Hit.Position
		Transfer:FireServer(Mouse)
	end
end)
1 Like

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