Problems with my guns on my star ships

Before problems

So I want to make guns for my star wars ships but I don’t know how to start.

I thought of making a part keep going in a direction until it hits another ship.

I also need to have it so only Star Destroyers (this is star wars, ofc) can hit CR90 Corvettes (aka the Tantive IV aka a rebel ship), and vice versa. They aren’t gonna be manned, also.

How should I do this? What is the easiest way to do this?

After problems: Problems with my guns on my star ships - #3 by IAmPinleon

There are a bunch of great topics on the Forum to help you get started, and many to help you become more advanced with creating animated weaponry.

Here are my two personal favorites:
Animating Tools
FastCast Module (for Rays)

To keep it simple, you’d have to first use RaycastingService to create a ray for the gun’s aim. Then, you’d just have to write a script for actually firing the gun and rendering the bullet on everyone’s client to prevent server issues.

And if you want to make it so only certain teams can hit each other, then you can simply use a script to detect if the first part the ray touches is a parent of a specific object. If it’s the right team, then continue with the script.

I have not made a gun before so I cannot answer your complex questions. However I hope the topics above get you on the right track. (If I get anything wrong, please notify me and I will change this post.)

2 Likes

Ok I didn’t use those tutorials but used TheDevKing’s tutorial on raycasting and made some changes.

local bullet = game.ReplicatedStorage.Laser

local turret = script.Parent

local fireRate = 1
local Damage = 10
local speed = 1000
local aggroDist = 5000

while wait(fireRate) do
	
	local target = nil
	for i, v in pairs(workspace:GetChildren()) do
		local side = v:FindFirstChild("Side")
		local health = v:FindFirstChild("Health")
		local part = v:FindFirstChild("Primary")
		if (health and health.Value > 0) and (side and side.Value == "Empire") and part then
			if (part.Position - turret.Position).magnitude < aggroDist then
				local bulletRay = Ray.new(turret.Position, (part.Position - turret.Position).Unit * 250000)
				local hit, position = workspace:FindPartOnRayWithIgnoreList(bulletRay, {turret})
				if hit == part then
					target = part
					print("Object not in the way :)")
				else
					print("Object in the way :(")
					print(tostring(hit), tostring(hit.Parent), tostring(hit.Parent.Parent))
				end
			end
		end
	end
	
	if target then
		local part = target
		turret.CFrame = CFrame.new(turret.Position, part.Position)
		
		local newBullet = bullet:Clone()
		newBullet.Position = turret.Position
		newBullet.Parent = workspace
		
		newBullet.Velocity = turret.CFrame.LookVector * speed
		
		newBullet.Touched:Connect(function(hit)
			if hit.Name ~= "Primary" then
				local health = hit.Parent:FindFirstChild("Health")
				if health then
					if health.Value > 0 then
						health.Value = health.Value - Damage
						newBullet:Destroy()
						if health.Value == 0 then
							for _, weld in pairs(hit.Parent:GetDescendants()) do
								if weld:IsA("WeldConstraint") then
									weld:Destroy()
									print("DESTROYED")
								end
							end
						end
					end
				end
			end
		end)
	end
end

Two problems:

  1. The star ship moves when shooting at the enemy star ship, and anchoring it leads to problem 2.
  2. The guns move even when anchored, but the star ship doesn’t move.