I have a script and i just want to know how i could make a part shoot in the direction the mouse is facing.
local FireThrown = game.ReplicatedStorage.RemoteEvents.FireThrown
FireThrown.OnServerEvent:Connect(function (plr, spell, spellPosition)
local newSpell = spell:Clone()
newSpell.Parent = game.Workspace
local Fire = newSpell:FindFirstChild("Fire")
local Sphere = newSpell:FindFirstChild("Sphere.001")
if Fire and Sphere then
Sphere.HandleWeld:Destroy()
Fire.Position = spellPosition
Sphere.Position = Fire.Position
end
end)
If your using a part, the best way would probably be a linear velocity, since it shoots forward, but an even better approach would be using ray casting and make it shoot like that.
In my opinion, raycasting is more reliable and better, and can get more information than a part.Touched can. Or whatever your using.
Here’s a simple gun script that I used to learn raycasting,
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Tool.Activated:Connect(function()
local Origin = Tool.Handle.Position
local Direction = (Mouse.Hit.Position - Origin).Unit
local function CleanUp()
for i, v in pairs(workspace:GetChildren()) do
if v.Name == "RayPart" or v.Name == "OriginPart" then
v:Destroy()
end
end
end
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Tool.Handle, Player.Character:GetChildren()}
Params.IgnoreWater = true
local ray = workspace:Raycast(Origin, Direction * 1000, Params)
local RayLine = Instance.new("Part", workspace)
local distance = (ray.Position - Origin).Magnitude
RayLine.Size = Vector3.new(0.2, 0.2, distance)
RayLine.CFrame = CFrame.new(Origin, ray.Position) * CFrame.new(0, 0, -distance/2)
RayLine.Anchored = true
RayLine.Parent = workspace
task.wait(0.5)
RayLine:Destroy()
CleanUp()
end)
If you scroll down to the RayLine Variable, you can see how I did it.
I just used a part to visualise the ray, but depending on what you’re trying to do, you would probably have to tween it or apply a Linear Velocity. What ever is best for your purpose. Maybe beams can also work.