I made a gun to my game, but i had some problems, so a guy just told me to use a ray. I added a ray to my script but it still doesn't work. There's a video so you can see what is the problem: http://gyazo.com/1cf05bb4a57ad9a37866f40546380fd7

AnD 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
local coolDown = 0.5
 
-- 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")
	   bullet.Size = Vector3.new(range,0.25,0.25)
	   local ray = Ray.new(hole.CFrame.Position, (mouse.Hit.Position - hole.CFrame.Position).Unit*100)
	   local hit, position = workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character)
	   local distance = (pistola.Handle.CFrame.p - position).magnitude
	   bullet.CFrame = CFrame.new(pistola.Handle.CFrame.p, position) * CFrame.new(0,0, distance/2)
	   bullet.Transparency = 0.5
	   bullet.CanCollide = false
	   bullet.Anchored = true
	   bullet.Parent = game.Workspace
	   bullet.Orientation = hole.Orientation
	   bullet.Position = hole.Position
	game:GetService("Debris"):AddItem(bullet, 0.5) 
	    spam = false
	end	
end)

Could you help me?

Also "pistola" = gun in portuguese XD

2 comments

0

It's because hole is referencing the tool in starterpack, you should instead make hole a child of pistola since pistola is script.Parent and references the actual pistol in the backpack itself...for instance, hole = pistola.Hole on line 2

0

Thank you!