Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make an object fire towards my mouse position?

I'm trying to make a fireball move towards my mouse instead of straight forward from my body. I'm not really sure what commands or CFrames to use to make it do that. Heres the entire script so far.

Player = game.Players.LocalPlayer Mouse = Player:GetMouse()

Mouse.KeyDown:connect(function(key) key=key:lower() if key == "q" then local x=Instance.new("Part", workspace) x.Shape = "Ball" x.BrickColor = BrickColor.new("Really red") x.Transparency = 0.5 x.TopSurface = "Smooth" x.BottomSurface = "Smooth" x.CanCollide = false x.Anchored = false x.CFrame = Player.Character.Head.CFrame * CFrame.new(0,0,-12) local v = Instance.new("BodyVelocity", x) v.Velocity = Player.Character.Head.CFrame.lookVector * 90

	v.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	local f = Instance.new("Fire")
	f.Size = 10
	f.Parent = x

local S = script["Damage"]:clone() S.Parent = x x.Parent= workspace wait(2)

	x:remove()
	
end

end)

1 comment

0

remove() is deprecated, connect is deprecated , keydown is deprecated

1 answer

Mouse.KeyDown is deprecated, do not use it to get user input. Instead use ContextActionService or UserInputService. Both of these are client side only services.RBXScriptSignal:connect() is deprecated, do not use it either. Instead use RBXScriptSignal:Connect()Also, do not use strings to set a property that holds an enumeration as a value. Also, global workspace was recently deprecated, use game.Workspace instead. And if your script[“Damage”] script is not disabled, disable it using its Disabled property.

local ContextActionService = game:GetService([[ContextActionService]])
local plr = game:GetService([[Players]]).LocalPlayer
local startCFrame = game.Workspace[plr.Name].Head.CFrame
local head = plr.Character.Head
local mouse = plr:GetMouse()

function fireball()
    local ball = Instance.new([[Part]], game.Workspace) 
    ball.Shape = Enum.PartType.Ball ——- you see I didn’t say “Ball”
    ball.Color = Color3.fromRGB(255, 0, 0)
    ball.Transparency = .5
    ball.TopSurface = Enum.SurfaceType.Smooth ——- Not “Smooth”
    ball.BottomSurface = Enum.SurfaceType.Smooth
    ball.CanCollide = false
    ball.CFrame = startCFrame * CFrame.new(0, 0, -12)

    local BV = Instance.new([[BodyVelocity]], ball)
    BV.Velocity = Vector3.new(mouse.Hit.lookVector * 90)
    BV.MaxForce = Vector3.new(head.CFrame.lookVector, 5, 0)
    
    local fire = Instance.new([[Fire]], ball)
    fire.Size = 10
    
    local dmgScript = script.Damage:Clone()
    dmgScript.Parent = ball
    dmgScript.Disabled = false
    
    wait(2)
    ball:Destroy()
end

ContextActionService:BindAction([[Fireball]], 
    fireball, 
    false, 
    Enum.KeyCode.Q
)

This discussion is preserved as it was. New answers, comments, and votes are closed.