So im making a gun to my game, and im using ray's to set the position/orientacion/where to stop the bullet and a guy (at another question) told me to use ray's and he gave me this ray:
local ray = Ray.new(hole.CFrame.Position, (mouse.Hit.Position - hole.CFrame.Position).Unit*range)
But now idk how to use them after creating them, i know that i need to use "findpartinray" but idk where to use it , or how to use it. Anyways here's the script:
local pistola = script.Parent local hole = game.StarterPack.Pistola.Hole local mouse = game.Players.LocalPlayer:GetMouse() -- Lista de Valores -- local ammo = 12 local maxAmmo = 64 local damage = 20 local headshotDamage = 50 local range = 100 -- impedir spamming -- local spam = false -- Disparo -- pistola.Activated:Connect(function() if spam == false then spam = true pistola.Handle.Disparo:Play() local bullet = Instance.new("Part") local ray = Ray.new(hole.CFrame.Position, (mouse.Hit.Position - hole.CFrame.Position).Unit*range) bullet.Size = Vector3.new(range,0.25,0.25) bullet.Position = hole.Position bullet.Transparency = 0.5 bullet.CanCollide = false bullet.Anchored = true bullet.Parent = game.Workspace wait(0.5) bullet:Destroy() spam = false end end)
Could you tell me where to use it, if i need to add something else and also a fix pls :D Thx! (also pls send an answer, dont add a commentary pls)
First of all, the variable called "hole" is set to the starterpack's hole, not the local pistola's hole.
local hole = pistola.Hole
once you get the ray do
local hit, position = workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character) local distance = (pistola.Handle.CFrame.p - position).magnitude -- distance between the part (hit) and your pistola -- Set your bullet's CFrame bullet.CFrame = CFrame.new(pistola.Handle.CFrame.p, position) * CFrame.new(0,0, distance/2)
Also, instead of doing wait(0.5) you can do
game:GetService("Debris"):AddItem(bullet, 0.5) -- (part to destroy, wait time)
If you don't know how to use them check the wiki article, there's even a raycasting article on it.