I have basically taken the roblox on tutorial on how to make a ray gun
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
tool.Equipped:connect(function(mouse)
print("Tool equipped!")
mouse.Button1Down:connect(function()
print("Mouse pressed!")
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Bright red")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.1)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(30)
end
end
end)
end)
and modified it to work on filtering enabled but I'm having issues making the ray a cylinder instead of a block, it seems to fire sideways instead of forwards as I am trying to make it do. Would anyone have any solutions to this? Here's my modified code:
local tool = script.Parent
script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, mousehit)
local ray = Ray.new(tool.Handle.CFrame.p, ( tool.Handle.CFrame.p-mousehit.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Toothpaste")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
beam.Shape = "Cylinder"
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(distance, 5, 5)
beam.CFrame = CFrame.new( tool.Handle.CFrame.p,position) * CFrame.new(-distance / 2, 0, 0)
------ beam.Rotation = Vector3.new(90,0,90) --ive tried these but they dont work
-- beam.Orientation = beam.Orientation + Vector3.new(0,90,0)
game:GetService("Debris"):AddItem(beam, 290)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(30)
end
end
end)

1 comment
Make your own scripts.