Help with raycasting

Im trying to set up raycasting for my weapons in my game but im having an issue with the ray shooting out of the sword. I tried messing with the code and changing things around but have no luck. Here is a link to the place:

test.rbxl (72.9 KB)

Why don’t you format your code </>

Start point:

local tool = script.Parent
local user


 

tool.Equipped:connect(function(mouse)
    user = tool.Parent 
 
    mouse.Button1Down:connect(function() 
        local ray = Ray.new(tool.Handle.Core2.CFrame.p, (mouse.Hit.p - tool.Handle.Core2.CFrame.p).unit*300)
        local hit, position = game.Workspace:FindPartOnRay(ray, user)
 
        local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid:TakeDamage(25)
        end
		
 
        local distance = (position - tool.Handle.Core2.CFrame.p).magnitude
        local rayPart = Instance.new("Part", user)
        rayPart.Name          = "RayPart"
		rayPart.Material	  = 'Neon'
        rayPart.BrickColor    = BrickColor.new("Hot pink")
        rayPart.Transparency  = 0
        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.5, 0.5, distance)
        rayPart.CFrame        = CFrame.new(position, tool.Handle.Core2.CFrame.p) * CFrame.new(0, 0, -distance/2)
 		

        wait(0.01)
		rayPart.Transparency = 0.1
        wait(0.01)
		rayPart.Transparency = 0.2
		rayPart.Size = rayPart.Size -Vector3.new(0.2,0.2,0)
        wait(0.01)
		rayPart.Transparency = 0.3
		wait(0.01)
		rayPart.Transparency = 0.4
		rayPart.Size = rayPart.Size -Vector3.new(0.2,0.2,0)
		wait(0.01)
		rayPart.Transparency = 0.5
		wait(0.01)
		rayPart.Transparency = 0.6
		wait(0.01)
		rayPart.Transparency = 0.7
		wait(0.01)
		rayPart.Transparency = 0.8
		wait(0.01)
		rayPart.Transparency = 0.9
		wait(0.01)
		rayPart.Transparency = 1
		wait(0.1)
		rayPart:Destroy()
    end)
end)

End point:


local tool = script.Parent
local user

 

tool.Equipped:connect(function(mouse)
    user = tool.Parent 
 
    mouse.Button1Down:connect(function() 
        local ray = Ray.new(tool.Handle.Core2.CFrame.p, (mouse.Hit.p - tool.Handle.Core2.CFrame.p).unit*300)
        local hit, position = game.Workspace:FindPartOnRay(ray, user)
 
        local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid:TakeDamage(0)
        end
		
 
        local distance = 0.5
        local rayPart = Instance.new("Part", user)
        rayPart.Name          = "RayPart"
		rayPart.Material	  = 'Neon'
        rayPart.BrickColor    = BrickColor.new("Hot pink")
        rayPart.Transparency  = 0
        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(2, 2, 2)
        rayPart.CFrame        = CFrame.new(position, tool.Handle.Core2.CFrame.p) * CFrame.new(0, 0, -distance/2)
 		

        wait(0.01)
		rayPart.Transparency = 0.1
        wait(0.01)
		rayPart.Transparency = 0.2
		rayPart.Size = rayPart.Size -Vector3.new(1,1,0)
        wait(0.01)
		rayPart.Transparency = 0.3
		wait(0.01)
		rayPart.Transparency = 0.4
		rayPart.Size = rayPart.Size -Vector3.new(1,1,0)
		wait(0.01)
		rayPart.Transparency = 0.5
		wait(0.01)
		rayPart.Transparency = 0.6
		wait(0.01)
		rayPart.Transparency = 0.7
		wait(0.01)
		rayPart.Transparency = 0.8
		wait(0.01)
		rayPart.Transparency = 0.9
		wait(0.01)
		rayPart.Transparency = 1
		wait(0.1)
		rayPart:Destroy()
    end)
end)

I think Ray.new() is the old method of raycasting
the newer one is workspace:RayCast(origin,direction,rayparams) (correct me if im wrong)
so I don’t really understand this code too much
also you should use for i = 0,1,0.1 do end for the fading of the beam and maybe the debris service to destroy the beam.

1 Like

use tween service for that part bro…