Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a bullet go in the directon of your mouse?

Asked by 4 years ago

The main area of focus is round line 24. I've looked at many sources, but I still need help. So, I have A script that works great, but I need help getting the direction right. The fireball seems to go in one direction no matter which way it is facing. the script is below.

local Tool = script.Parent
local Handle = Tool:FindFirstChild("Handle")
local FireBallPart = game.ServerStorage:FindFirstChild("Union")
local enabled = true

local function Shoot(target)

if enabled == true then
    enabled = false
        local Ball = FireBallPart:Clone()
    local startPosition = Handle.Position
    local targetPosition = workspace.Part.position
    Ball.Parent = workspace
    Ball.CanCollide = true
    Ball.Anchored = false
    local BallCframe = CFrame.new(startPosition) + Vector3.new(.1,1,.1)


    Ball.CFrame = BallCframe

        Ball.CFrame = CFrame.new(Ball.Position,targetPosition)

        local BodyVelocity = Instance.new("BodyVelocity")
        BodyVelocity.velocity = BallCframe.lookVector * 10
        BodyVelocity.Parent = Ball
        print (BallCframe.lookVector)
    wait(1)
    enabled = true

    wait(50)
    Ball:Remove()
end

For testing, I just wanted it to go towards on part, but that didn't seem to work either.

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

You'll want to get the mouse by using a RemoteEvent from the client (recommend putting the tools remotes inside of a folder thats in the tool)

LocalScript

local Tool = script.Parent
local Remotes = Tool.Remotes
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

Tool.Activated:Connect(function()
    Remotes.Shoot:FireServer(mouse.Hit)
end)

ServerScript

local Tool = script.Parent
local Handle = Tool:FindFirstChild("Handle")
local FireBallPart = game.ServerStorage:FindFirstChild("Union")
local enabled = true

local function Shoot(player,mouse)

if enabled == true then
    enabled = false
        local Ball = FireBallPart:Clone()
    local startPosition = Handle.Position
    local targetPosition = workspace.Part.position
    Ball.Parent = workspace
    Ball.CanCollide = true
    Ball.Anchored = false
    local BallCframe = CFrame.new(startPosition) + Vector3.new(.1,1,.1)


    Ball.CFrame = BallCframe

        Ball.CFrame = CFrame.new(Ball.Position,targetPosition)

        local BodyVelocity = Instance.new("BodyVelocity")
        BodyVelocity.velocity = mouse.LookVector * 90 --Speed
        BodyVelocity.Parent = Ball
        print (BallCframe.lookVector)
    wait(1)
    enabled = true

    wait(50)
    Ball:Remove()
end

Hope this helps!

0
This helped a lot, and now I understand what I did wrong. But, what are remotes? lormandillis 2 — 4y
0
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events They are instances used to communicate between the client and the server. laurenbtd5 379 — 4y
0
Thank you again, you seriously helped. lormandillis 2 — 4y
0
No problem, make sure to mark as the answer laurenbtd5 379 — 4y
Ad

Answer this question