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

How do i use rays after creating them?

Asked by 6 years ago

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)

0
This is the second time i post this question because the last time nobody answered me :P mewant_taco 17 — 6y
0
you shouldn't using instance.new in a local script if you want other players to see a projectile theking48989987 2147 — 6y
0
I'm not sure how fast this bullet is going to be but personally I would use a Ray for something like an instantaneous speed projectile (i.e. a laser) and Touched for something like a bullet/rocket Vulkarin 581 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
0
im new to answering things btw, also im not that good at explaining, but i did the best i could bhqpping 80 — 6y
0
Ty, it rly helped me :D But i didn't understood one thing, you created a variable called hit, position, but i cant find the place where you used it mewant_taco 17 — 6y
0
And also Pistola = Gun mewant_taco 17 — 6y
0
Well, so i tested the script and it didn't work either :/ mewant_taco 17 — 6y
0
works fine for me, btw if your gonna use this for your game, its only gonna work clientsided if you have just a localscript for the gun bhqpping 80 — 6y
Ad
Log in to vote
1
Answered by
gullet 471 Moderation Voter
6 years ago

If you don't know how to use them check the wiki article, there's even a raycasting article on it.

0
I alredy checked it :P but i didn't understood much, there are no parts where to use the ray X/ (i mean, i dont understood XD) mewant_taco 17 — 6y
0
LoL, now i bet that nobody will answer this again because they will see that is has one answer alredy XD mewant_taco 17 — 6y
0
The answer is correct though...even contains guides on specifically what to do. What else could you possibly expect? Vulkarin 581 — 6y
0
If there's something you don't understand about it, you'll need to be more specific about what that is since everything is documented on the wiki articles. gullet 471 — 6y
View all comments (3 more)
0
Idk, i just didn't understood :p mewant_taco 17 — 6y
0
Not understanding doesn't mean the answer is invalid. If you want to understand then tell us what you don't understand Vulkarin 581 — 6y
0
1st i didn't said the answer isn't valid 2nd idk why i didn't understood, i guess im just rly noob ;-; mewant_taco 17 — 6y

Answer this question