Hi. I have been messing around with the default raycast gun script and I want the laser to get smaller and move forward. Here is my current code:
local tool = script.Parent
--when the tool is equipped
--store the character of the person using the tool
user = tool.Parent
mouse = game.Players.LocalPlayer:GetMouse()
--when the left mouse button is clicked
mouse.Button1Down:connect(function()
--make and do a hit test along the ray
if script.Parent.AbleToFire.Value == true then
print('RAYSENT')
local ray = Ray.new(tool.FireFrom.CFrame.p, (mouse.Hit.p - tool.FireFrom.CFrame.p).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
--do damage to any humanoids hit
local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(30)
end
--draw the ray
local distance = (position - tool.Handle.CFrame.p).magnitude
local rayPart = Instance.new("Part", workspace)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Bright red")
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame = CFrame.new(position, tool.FireFrom.CFrame.p) * CFrame.new(0, 0, -distance/2)
--add it to debris so it disappears after 0.1 seconds
wait(0.1)
rayPart.Size = rayPart.Size - Vector3.new(0.2,0.2,distance/3)
rayPart.CFrame = rayPart.CFrame * rayPart.CFrame:toObjectSpace(CFrame.new(0, distance/3, 0))
wait(0.1)
rayPart.Size = rayPart.Size - Vector3.new(0.2,0.2,distance/3)
rayPart.CFrame = rayPart.CFrame * rayPart.CFrame:toObjectSpace(CFrame.new(0, distance/3, 0))
wait(0.1)
rayPart.Size = rayPart.Size - Vector3.new(0.2,0.2,distance/3)
rayPart.CFrame = rayPart.CFrame * rayPart.CFrame:toObjectSpace(CFrame.new(0, distance/3, 0))
wait(0.1)
game.Debris:AddItem(rayPart, 0.1)
end
end)
function Equipped()
script.Parent.AbleToFire.Value = true
LeftArmFireCF = CFrame.new(.84, .4, 1.1) * CFrame.fromEulerAnglesXYZ(math.rad(320), -0.2, math.rad(-90))
RightArmFireCF = CFrame.new(-.75, -0.4, -0.95) * CFrame.fromEulerAnglesXYZ(math.rad(-89), math.rad(-14), 0)
LeftArmWeld = Instance.new("Weld", script)
RightArmWeld = Instance.new("Weld", script)
local Char = game.Players.LocalPlayer.Character
if Char:findFirstChild("Left Arm") ~= nil then
LeftArmWeld.Parent = script
LeftArmWeld.Part0 = Char["Head"]
LeftArmWeld.Part1 = Char["Left Arm"]
LeftArmWeld.C1 = LeftArmFireCF
Char["Torso"]["Left Shoulder"].Part1 = nil
end
if Char:findFirstChild("Right Arm") ~= nil then
RightArmWeld.Parent = script
RightArmWeld.Part0 = Char["Head"]
RightArmWeld.Part1 = Char["Right Arm"]
RightArmWeld.C1 = RightArmFireCF
Char["Torso"]["Right Shoulder"].Part1 = nil
end
end
function UnEquipped()
script.Parent.AbleToFire.Value = false
coroutine.resume(coroutine.create(function()
local Char = game.Players.LocalPlayer.Character
if Char:findFirstChild("Left Arm") then
LeftArmWeld.Part0 = nil
LeftArmWeld.Part1 = nil
Char["Torso"]["Left Shoulder"].Part1 = Char["Left Arm"]
end
if Char:findFirstChild("Right Arm") then
RightArmWeld.Part0 = nil
RightArmWeld.Part1 = nil
Char["Torso"]["Right Shoulder"].Part1 = Char["Right Arm"]
end
end))
end
script.Parent.Equipped:connect(Equipped)
script.Parent.Unequipped:connect(UnEquipped)
Right now, I fire the ray and the trace, the trace works for 0.1 ticks, then it goes and has a fit in the other direction.
How could I get it to move forward through the objects perspective. The code that I try to do this with is here:
wait(0.1)
rayPart.Size = rayPart.Size - Vector3.new(0.2,0.2,distance/3)
rayPart.CFrame = rayPart.CFrame * rayPart.CFrame:toObjectSpace(CFrame.new(0, distance/3, 0))
wait(0.1)
rayPart.Size = rayPart.Size - Vector3.new(0.2,0.2,distance/3)
rayPart.CFrame = rayPart.CFrame * rayPart.CFrame:toObjectSpace(CFrame.new(0, distance/3, 0))
wait(0.1)
rayPart.Size = rayPart.Size - Vector3.new(0.2,0.2,distance/3)
rayPart.CFrame = rayPart.CFrame * rayPart.CFrame:toObjectSpace(CFrame.new(0, distance/3, 0))
wait(0.1)
game.Debris:AddItem(rayPart, 0.1)
Any help would be greatly appreciated!
Thanks!
P.S. No errors before you ask.
