Projectile help

I’m trying to make my fighter jet shoot bullets that will show to every player but when I send a remote event to the server, it doesn’t work.

Shoot Client:

local RunService = game:GetService("RunService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bullet = ReplicatedStorage:WaitForChild("Bullet")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local model = script.Parent
local VehicleSeat = model:WaitForChild("VehicleSeat")
local GunPart = model:WaitForChild("GunPart")

local cooldown = 0.1
local currentTime = cooldown

local connection

local function StopFiring()
	if connection then
		connection:Disconnect()
		connection = nil
	end
end

local function FireGun()
	StopFiring()
	
	connection = RunService.Heartbeat:Connect(function()
		if VehicleSeat.Occupant then
			local result = game.Players:GetPlayerFromCharacter(VehicleSeat.Occupant.Parent)

			if result and tick() - currentTime >= cooldown then
				currentTime = tick()
				script.ShootGun:FireServer()

			elseif not result then
				StopFiring()
			end
		else
			StopFiring()
		end
	end)
end

mouse.Button1Down:Connect(function()
	if VehicleSeat.Occupant then
		local result = game.Players:GetPlayerFromCharacter(VehicleSeat.Occupant.Parent)

		if result then
			FireGun()
		end
	end
end)

mouse.Button1Up:Connect(function()
	if VehicleSeat.Occupant then
		local result = game.Players:GetPlayerFromCharacter(VehicleSeat.Occupant.Parent)

		if result then
			StopFiring()
		end
	end
end)

--------------------------------------------------------------------------------------------------------------------------------------

local launchPower = 8000

local function ShootGun()
	local newBullet = Bullet:Clone()
	newBullet.CFrame = GunPart.CFrame
	newBullet.Parent = workspace
	
	newBullet:ApplyImpulse(newBullet.CFrame.LookVector * launchPower)
	game:GetService("Debris"):AddItem(newBullet, 8)
end

script:WaitForChild("ShootGun").OnClientEvent:Connect(function()
	ShootGun()
end)

image

Server:

script.Parent.ShootGun.OnServerEvent:Connect(function(player)
	script.Parent.ShootGun:FireAllClients()
end)

Bullet Client:

local part = script.Parent

part.Touched:Connect(function(hit)
	local speed = (part.AssemblyLinearVelocity - hit.AssemblyLinearVelocity).Magnitude
	
	if speed >= 100 then
		script.BreakPart:FireServer(hit) -- doesn't fire
	end
end)

Break Part Server:

local function BreakOthers(part)
	for i,v in ipairs(part:GetChildren()) do
		if v:IsA("Attachment") then
			v:Destroy()
		end
	end
end

script.Parent.BreakPart.OnServerEvent:Connect(function(player, part)
        print("test") ---- nothing

	if part then
		part:BreakJoints()
		BreakOthers(part)
	end
end)

Put a print() before the event gets fired.

Also, 100 AssemblyLinearVelocity is extremely fast, and you might not be reaching that speed.

I did and it printed. It only fires if I create the bullet in a server script but I want it to be smooth for all players.

So does it work but it’s not smooth, or what? I’m so confused.

This isn’t really a solution, but using fast cast will make this 10x easier. You don’t need to fire an event to the server and fire it back.

The remote event called “BreakPart” won’t fire even though speed is above 100 and it also prints on the client.

Yes but FastCast isn’t very good for projectile bouncing and I only need to shoot a sphere.

local speed = (part.AssemblyLinearVelocity - hit.AssemblyLinearVelocity).Magnitude

That won’t work, I just noticed.

You can just get one part’s velocity and that would be the speed, why are you getting the magnitude of two parts?

I want to break the parts if the bullet hits at a certain speed so if the hit part is moving and the part is moving, I have to check both.

local part = script.Parent

part.Touched:Connect(function(hit)
	local speed = (part.AssemblyLinearVelocity - hit.AssemblyLinearVelocity).Magnitude
	
	if speed >= 100 then
		script.BreakPart:FireServer(hit) <---
	end
end)

I fixed it by moving the BreakPart server script into server script service.

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