Need help with RunService.HeartBeat and FastCast

Sup dudes, hopefully you can understand me here, so I’m trying to make a Sentry Gun using FastCast module, where for each heartbeat, it will look around and if it finds a target, it will look at the target and shoot (using FastCast), and look around again if target is not in range. However, it does get the job done with finding the target, but when shooting at the target, the bullets don’t even travel, it just stays at the FirePoint attachment. In order for the bullets to travel, RunService.HeartBeat is required, which I am using, but it’s not travelling, what am I doing wrong? I looked around the internet but no solution so I’m just posting it here.

function FireSentry(FireAt)
	if not CanFire then
		return
	end
	CanFire = false
	local dir = (FireAt - FirePointObject.WorldPosition).Unit
	for i = 1, BULLETS_PER_SHOT do
		Fire(dir)
	end
	if FIRE_DELAY > 0.03 then wait(FIRE_DELAY) end
	CanFire = true
end

local RunService = game:GetService("RunService")
local ref = Sentry.Ref 
local yawHinge = Sentry.Platform.Base.YawHinge
local pitchHinge = Sentry.Housing.Base.PitchHinge
local rngAi = Random.new()
local tip = Sentry.Housing.Tip
local beamTemplate = Sentry.Beam

local RADIUS = Sentry:GetAttribute("DetectionRadius")
local DEPRESSION = Sentry:GetAttribute("Depression")
local DAMAGE = Sentry:GetAttribute("Damage")
--local COOLDOWN = Sentry:GetAttribute("Cooldown")

local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Exclude
overlapParams.FilterDescendantsInstances = {Sentry}

local castParamss = RaycastParams.new()
castParamss.FilterType = Enum.RaycastFilterType.Exclude
castParamss.FilterDescendantsInstances = {Sentry}

local function IsReachable(subject)
	local directionAt = subject:GetPivot().Position - ref.Position 
	if math.deg(ref.CFrame.UpVector:Angle(directionAt)) > DEPRESSION then
		return false 
	end
	
	local castOut = workspace:Raycast(ref.Position, directionAt.Unit * RADIUS, castParamss)
	return castOut and castOut.Instance:IsDescendantOf(subject)
end

local directionAxis = Vector3.xAxis
local target = nil 
local shotTime = 0

local SightDis = 40

local relative = nil


--main problem down here
RunService.Heartbeat:Connect(function(ed, dt)
	local relative = nil
	if not target then
		local parts = workspace:GetPartBoundsInRadius(ref.Position, RADIUS, overlapParams)
		for _, part in parts do
			print("checking the area...")
			local character = part:FindFirstAncestorWhichIsA("Model")
			local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
			if humanoid and IsReachable(character) then
				if character == target then return end
				print("target found")
				target = character
				break
			end
		end
	end
	if rngAi:NextNumber() < 0.005 then 
		print("rngAI")
		directionAxis = rngAi:NextUnitVector()
	end
	if target and IsReachable(target) then
		print("found target: " ..target.Name)
		directionAxis = ref.CFrame:PointToObjectSpace(target:GetPivot().Position)
		relative = (target.HumanoidRootPart.Position-PrimaryPart.Position)
		local forward = tip.CFrame.LookVector
		local side = relative.Unit 
		local theta = math.deg(math.acos(forward:Dot(side)))
		if theta <= SightDis then
			FireSentry(target.HumanoidRootPart.Position)
		end
	else
		target = nil 
	end
	print("looking around")
	local flatDirection = directionAxis * Vector3.new(1, 0, 1)
	yawHinge.TargetAngle = math.deg(Vector3.zAxis:Angle(-flatDirection, Vector3.yAxis))

	local axis = Vector3.yAxis:Cross(flatDirection)
	pitchHinge.TargetAngle = math.deg(flatDirection:Angle(directionAxis, axis))
--main problem up here
end)

I just need help on how I can use RunService.HeartBeat to both find a target, and shoot at them. (edit: pretty much multi-tasking) If you need more info or a model, I can provide one. Thanks

1 Like

I’m assuimg that you’re using FastCast Redux but i don’t see any lines mentions about FastCast?

3 Likes

the function FireSentry is using the FastCast module, it’s just that the main code is too big so I shortened it in this post.