Also, how could I go about making it so instead of the bullets going were the mouse is it goes straight forward depending on where the barrel is facing
This guide explains very nicely how one can add recoil, bobble and sway to their guns.
To make the gun fire in the direction of the barrel, you can create an attachment in front of GunFirePoint
and use this attachment’s position as the direction, instead of using (mousepos-origin).Unit
.
alright thinks, ill mess around with it tommorow morning. How can I make it so its hold down to shoot instead of individual clicks?
For that, the client could fire the server when the player’s click input begins and run a loop on the server which checks if the player can actually fire or not, and fires if possible.
When the user input ends, the client can fire the server again informing it to break the loop.
Eg:
--On the client:
local UserInputService = game:GetService("UserInputService")
local Remote = PATH.TO.REMOTE
UserInputService.InputBegan:Connect(function(input, isBusy)
if (not isBusy) and (input.UserInputType == Enum.UserInputType.MouseButton1) then
Remote:FireServer(0)
end
end)
UserInputService.InputEnded:Connect(function(input, isBusy)
if (not isBusy) and (input.UserInputType == Enum.UserInputType.MouseButton1) then
Remote:FireServer(1)
end
end)
--On the server:
local Remote = PATH.TO.REMOTE
local shouldBreak = false
Remote.OnServerEvent:Connect(function(state : number)
if state == 0 then
shouldBreak = false
while not shouldBreak do
--Fire the gun!
end
elseif state == 1 then
shouldBreak = true
end
end)
For the Server part, how do I implement that into what I already have?
How do I go about doing this too btw? scripting wise
Currently, what you are defining as direction is: (mousepos-origin).Unit
.
This is basically the unit vector between the GunFirePoint.WorldPosition
and the mouse click position.
This vector defines the direction. However, since you need the direction to be along the barrel, you will need two points. These two points can be GunFirePoint.WorldPosition
and another attachment’s WorldPosition (which you will need to add).
It would look like:
local direction = (Attachment.WorldPosition-origin).Unit
I just noticed a problem with the code. You should not create a caster every time you want to fire a bullet. A caster is a ‘gun’ in itself. For further reference: FastCast
Also, you can simply move your code under the OnServerEvent to under the loop (where I have added a comment)
Sorry, could I get the solutions to this in smaller words lol