How do you make a projectile?

I am trying to make a projectile when you press a button it sends a fireball.
The only thing I need help with is actually sending the fireball. Like CFrames and so on.
I did watch YouTube videos, but it wasn’t that useful for me.

Thank you for reading and have a great day.

2 Likes

Y e s

Projectiles are probably 1 of my most personal favorite things to make, especially considering how you want to handle this

For your instance, you want to send a Fireball upon clicking a Button :thinking: The thing here, is that it depends what “Button” specifically we’re looking at here (ClickDetector, GuiButton, etc)

Once we know what “Button” we’re looking for, then I’ll give an explanation on what to do :wink:

2 Likes

For a physics projectile I like just applying a moment of impulse and letting the physics engine handle a nice gravity curve for me.

Force = mass * acceleration

Fireball:GetMass() to find the mass, then acceleration is just your desired speed in studs per second.

You just take the unit direction vector of where you want the fireball to go and multiply it by force then pass that right into ApplyImpulse.

2 Likes

The UserInputService is when you press “F” it will activate. I know I can’t control your time but can you help me now?

1 Like

Maybe

Well you’d need to use RemoteEvents, which would be capable of handling client/server transportation

We’d want to send a Fireball to the server-side, and the User Detection from the client

You could potentially do something like this?

--Client Side
local Event = game.ReplicatedStorage:WatiForChild("RemoteEvent")
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input, Chatted)
    if Chatted then
        return
    end

    if Input.KeyCode == Enum.KeyCode.F then
        Event:FireServer() --We're sending this for the server to receive
    end
end)
--Server Script inside ServerScriptService
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Fireball = game.ServerStorage:WaitForChild("Fireball")

Event.OnServerEvent:Connect(function(Player)
    local Clone = Fireball:Clone()
    Clone.Parent = workspace
    --You can set where you want the fireball to go from, it depends though
end)
1 Like

I’m sorry I might have given you the wrong idea but I’m trying to make it so it moves I did everything like that already just not the moving part like how far it goes and everything like that.

Oh, whoops my bad then LOL

You still haven’t answered where you want the fireball to fire from though :thinking: Like do you want it to target towards where the mouse is? Or where the Character is looking?

1 Like

I’d like to fire where the character is looking/facing. So if the character is facing for example north it will go north, and I like the new avatar lol.

1 Like

cant you just use a body velocity body mover then have its velocity be the vector formed from the player and the mouse hit. if you want it to go where the player is facing just set the velocity to the look vector ig

2 Likes

I don’t how to do any of that, that is why I’m asking for help.

velocity is just speed with direction (vector) and to form a vector from 2 points, say A to B its just B-A. Normalize that vector (.Unit) then multiply it by some scalar

bodyvelocity.Velocity = (mouse.Hit.Position - character.Head.Position).Unit*5

if u want it to be based on where they are facing its just

bodyvelocity.Velocity = character.Head.CFrame.LookVector * 5
1 Like

I swear I feel like a British type-guy with this cap, thanks lol

Gotcha, you can get where the Character’s looking by referencing its LookVector property, which is probably the easiest way to handle it

And since the Player parameter is already passed when OnServerEvent is connected, we can just simply get the Character using Player.Character:

--Server Script inside ServerScriptService
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Fireball = game.ServerStorage:WaitForChild("Fireball")
local Speed = 80

Event.OnServerEvent:Connect(function(Player)
    local Character = Player.Character

    local Clone = Fireball:Clone()
    Clone.Parent = workspace
    Clone.Position = Character.HumanoidRootPart.Position

    local BodyVel = Instance.new("BodyVelocity")
    BodyVel.Parent = Clone
    BodyVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

    BodyVel.Velocity = Character.HumanoidRootPart.CFrame.LookVector * 10
end)
1 Like

Wut

Must be an infinite yield warning, are you sure that you inserted a RemoteEvent object inside the ReplicatedStorage Service?

WAIT it works! But how do you do damage and make is disappear?

The easy approach is by creating a Touched event inside that OnServerEvent you just created (EVENT-CEPTION >:O)

After creating all of your stuff, just add a Touched event that will damage any Humanoid that touches it, you could also create a debounce if the want the Part to only damage you once (Otherwise it’d just either result in instakilling you or majorly damaging you

local FireballDamage = 25
local DB = false

Clone.Touched:Connect(function(Hit)
    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)

    if not DB and Player then
        local Character = Hit.Parent
        DB = true
        Character.Humanoid:TakeDamage(FireballDamage)
        wait(1)
        DB = false
    end
end)
2 Likes

Thank you so much this really helped with my game!

Also how can I make it spawn more away from me cause it will damage me too.

1 Like

Well are you wanting to make it spawn away more? Or do you just not want it to damage you when it’s first cast?

Wait I found the solution but the speed still doesn’t work.
local Speed = 80

1 Like

I forgot to set the Speed WHOOPS

I gotcha though, try this:

--Server Script inside ServerScriptService
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Fireball = game.ServerStorage:WaitForChild("Fireball")
local Speed = 80
local Offset = 10

Event.OnServerEvent:Connect(function(Player)
    local Character = Player.Character

    local Clone = Fireball:Clone()
    Clone.Parent = workspace
    Clone.Position = Character.HumanoidRootPart.Position + Character.HumanoidRootPart.CFrame.LookVector * Offset

    local BodyVel = Instance.new("BodyVelocity")
    BodyVel.Parent = Clone
    BodyVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

    BodyVel.Velocity = Character.HumanoidRootPart.CFrame.LookVector * Speed

    local FireballDamage = 25
    local DB = false

    Clone.Touched:Connect(function(Hit)
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)

        if not DB and Player then
            local Character = Hit.Parent
            DB = true
            Character.Humanoid:TakeDamage(FireballDamage)
            wait(1)
            DB = false
        end
    end)
end)