Is this a good way of rebinding connections on player respawn?

Not entirely sure if this is the best way to rebind connections on player respawn, but heres what i got

function VM:StartViewModel()
	if self["IsActive"] == true then return end
	local ViewModel = self["ViewModel"]  
	if not ViewModel then return end
	
	self["IsActive"] = true
	self["Things"] = {}
	self["Things"]["ToolAddedCheck"] = self["Player"].Character.ChildAdded:Connect(ToolEquipped)
	self["Things"]["ToolRemovedCheck"] = self["Player"].Character.ChildRemoved:Connect(ToolEquipped)

	local ToolCheck = coroutine.create(function()
		 while task.wait() do
			if not self["Player"].Character or self["Player"].Character.Humanoid.Health <= 0 or  self["IsActive"] == false then
				self["Things"]["ToolAddedCheck"]:Disconnect()
				self["Things"]["ToolRemovedCheck"]:Disconnect()
				
				self["Things"]["ToolAddedCheck"] = nil
				self["Things"]["ToolRemovedCheck"] = nil
				
				self["IsDead"] = true
				break
			else
				self["IsDead"] = false
			end
		end
	end)
	
	coroutine.resume(ToolCheck)
	
	local ViewModel = coroutine.create(function()
		self["Things"]["RunServiceViewModel"] = RunService.Heartbeat:Connect(function()
			if self["IsDead"] == true or self["IsActive"] == false then
				self["Things"]["RunServiceViewModel"]:Disconnect()
			else
				self["ViewModel"]:PivotTo(game.Workspace.CurrentCamera.CFrame)
			end
		end)
	end)
	
	coroutine.resume(ViewModel)
end

it all stops as intended, but just wondering if theres a better way then this or is this good enough

1 Like