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)
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.
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.
--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:
client (player X) fires a remote event to the server saying they are shooting a gun
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.
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.