How do I add bullets to a gun

So i made a gun and i have the script and everything but how do i add the bullets to the gun when its firing. Here is my script. You can implement it and test it. Any help is appreciated!

Local Script:

local maxAmmo = 40
local Ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local textLabel = playerGui:WaitForChild("Gun"):FindFirstChild("AmmoText")

script.Parent.Equipped:Connect(function(Mouse)
	local function reload()
		reloading = true
		wait(1)
		Ammo = maxAmmo
		reloading = false
	end
	
	script.Parent.Activated:Connect(function()
		if Ammo > 0 and not reloading then
			Ammo = Ammo - 1
			script.Parent.GunShot:Play()
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then
				script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20)
			end
		elseif reloading == false then
			reload()
			script.Parent.GunShot:Stop()
		end
		
		while wait() do
			textLabel.Text = (Ammo).." / "..maxAmmo
		end
	end)
	local input = game:GetService("UserInputService")
	input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
			reload()
		end
	end)
	
end)

Server Script:

script.Parent.DealDamage.OnServerEvent:Connect(function(player, Target, Damage)
	Target.Humanoid:TakeDamage(Damage)
end)
1 Like

By bullets do you mean the visuals?

yes actually bullets that will be shot from the gun

You could choose between two options in general, it could be a projectile as well as a ray-styled beam.

so how do i script that. I have no clue on how to script it

You can also use a sphere with a trail.

You make a tween. From the origin position (from the barrel of the gun) to the destination (the hit position from the ray)

do i need to add a remote event from the local script then create a new server script

Woah woah woah, why would you need that? Are you firing the ray from the server?

Well - the most common way with projectiles is to launch them with specific velocity directly from the root point and add a trail as a VFX.
For ray styled beam, you could create a part with a length of magnitude between the root and landing position, facing the landing from the root.

1 Like

This method is good, but if you tween the part you don’t need to use any physics overhead and you can control the speed of the bullet too.

I recommend you generate any of the part/trail/projectiles in the front end to prevent any performance issues.

no im firing it from the gun to the server

Ok good, then the ray should return a position for it. What you can do is then initiate the tween on the client while firing an event to the server with basic information like the origin position and destination position. From there, you replicate that to all the other clients to do the animation on their own so it looks smooth on everyones pc’s.

As @WilliamAlezandro said, you should NEVER do visuals on the server. Always keep those on the client.

im not understanding can you make it a little simple

You’re right, and to easily handle bullet drops, the method I’ve suggested should be working fine without any issue. Example

just want to have a bullet effect

like a script that make a bullet effect or something

For sure!

--local script
local function shootGun(user) --passing the user who shot the gun
    --put the code for whatever you need here
end

Gun.Activated:Connect(function()
    ShootingEvent() --player who shot the displays for himself local side
    shootGun(player) --if the player themselves shot the gun, this runs it on the client
end)

ShootingEvent.OnClientEvent:Connect(shootGun)
--server script
-- Shooting remote
local function bounceRemote(player, user)
    for i,v in ipairs(game.Players:GetPlayers()) do --getting all the players in the game

        if v ~= player then --all players BUT the one who shot the gun, (they already ran it on the client in the earlier code at shootGun(player) after ShootingEvent()
            user = v
            print(user)
            ShootingEvent:FireClient(v, player)

        end
    end
end

ShootingEvent.OnServerEvent:Connect(bounceRemote)

What the code is doing is passing the name of the client who shot the gun to the server, then back to all the other clients to say “Hey, player X is shooting their gun” and they make it appear on their own screen.

Logic behind it:

  1. client (player X) fires a remote event to the server saying they are shooting a gun
  2. The server is like “Hmmm ok let me see your file” and gets all the information on that specific clients gun like the bullet it has, the origin position of the bullet, destination position from the raycast, etc.
  3. Then the server tells everyone “Yo, player X is shooting a gun with this information” and all the other clients (player Y, Z, etc) spawn a bullet with player X’s information on player X’s position.

Disclaimer: The code above will NOT work, it’s just pseudo code for you to have a jump pad to work off.