Making my Sentry Shoot the Player

I’ve been working on a sentry system for my game, and I do not know how to proceed any further. I made a system where the sentry’s barrel faces the player, but I don’t really know how to make it shoot without becoming a minigun.

local TS = game:GetService("TweenService")
local RS = game:GetService("RunService")
local PS = game:GetService("Players")

local sentry = workspace.Sentry
local sentryFire = sentry.FiringPart

local burstCount = 5

local function attack(root)
	print(root)
end

local function lookAt(root)
	RS.Heartbeat:Connect(function(deltaTime)
		local distance = (sentryFire.Position - root.HumanoidRootPart.Position).Magnitude
		if distance < 20 then
			sentryFire.CFrame = CFrame.lookAt(sentryFire.Position, root.HumanoidRootPart.Position)
			return root
		end
	end)
end

game.Players.PlayerAdded:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	lookAt(char)
end)

Once it faces the player, use a part and fire it out at a velocity towards player using direction(partA.position - partB.position) and if it makes contact(detect using raycasts) do damage. Modules such as fast cast help optimize and improve projectiles. Also, you can add effects and sounds to the bullet for a better effect. Another alternative is just raycasting in a direction and dealing damage

My problem is how to make the part not fly every heartbeat

Task.wait() or use delta
Delta can really be helpful with heartbeat
Runservice.Heartbeat:Connect(delta(this delta))
use this delta to caculate how much time passes between frames, and once enough time passes, fire sentry.
You can also use spawn(funciton)
Hope you find this helpful

Ill try that. Is there any information on spawn(function)

it runs something that will not slow down other code ex.
while true do
wait(1)
print(“a”)
end
runs every second
while true do
spawn(function()
wait(1)
end)
print(“a”)
end
prints(“a”) every frame

This may not be spefefic, do research in roblox API and documenation

I got something else like this:

local start = os.time()
local frame = start - os.time()
print(frame)

All I need is a way to connect the function every 2 seconds

locaal start = os.time()
local frame = start -os.time()
runservice.Heartbeat:connect(function
if frame > 2 then
start = os.time()
do something
end

end)
something like this

every time I go near it, it just doesn’t update.


image
idk wtf is happening

oh, frame should = os.time() - start
sorry, mistake on my behalf

No I typed it wrong in the first place; you’re good

Now it works, thank you so much!

I was kinda wondering, how would I add burst attacks?

Maybe use a for loop with a shorter interval that shoots, then pausing for a longer interval that doesn’t shoot before initiating the next for loop.

Alright. I have a system where it shoots in a repeat-until loop and pauses at the end, but its slow and inefficient

maybe in the function Lookat() above, you change the line
if frame > 2
to
if frame > var

and change the var when it is bursting.
For example in a burst var = 0.1 and later var = 2

you would wait until 10 or so bullets were shot in a burst, then change wait(var in prev post) to 2, then repeat

like this?

if frame > burst then
start = os.time()
sentry.attack(firingPart, root)
burst += 1
end

no, maybe like a var named bullets shot in burst and adding 1 every time a bullet has been shot and if like 10 bullets have been shot, the increase burst, then at the end of that repeat.

if frame > burst then
	start = os.time()
	sentry.attack(firingPart, root)
	bulletsShot += 1
	if bursting == false then
		bursting = false
		burst=0.1
	end
	if bulletsShot == 10 then
		bulletShots =0
		burst = 2
		bursting = false
		--or just wait 2 and don't use the var bursting and just set burst to a fixed value
	end
end

like this
sorry if this is unclear/ variables are confusing

2 Likes