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

How do i use rays after creating them?

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)

3 comments

0

This is the second time i post this question because the last time nobody answered me :P

0

you shouldn't using instance.new in a local script if you want other players to see a projectile

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

2 answers

✓ Accepted answer

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)

5 comments

0

im new to answering things btw, also im not that good at explaining, but i did the best i could

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

0

And also Pistola = Gun

0

Well, so i tested the script and it didn't work either :/

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

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

7 comments

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)

0

LoL, now i bet that nobody will answer this again because they will see that is has one answer alredy XD

0

The answer is correct though...even contains guides on specifically what to do. What else could you possibly expect?

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.

0

Idk, i just didn't understood :p

0

Not understanding doesn't mean the answer is invalid. If you want to understand then tell us what you don't understand

0

1st i didn't said the answer isn't valid 2nd idk why i didn't understood, i guess im just rly noob ;-;

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