I'm trying to make a raycasting gun, following the How to Make a Raycasting Lasergun wiki tutorial, Instead of using **Equipped ** and then MouseButton1Down, I did the **Activated ** event instead, but for some reason it isn't casting rays.
There are no errors.
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Tool.Activated:connect(function()
print("Tool has activated")
local Ray = Ray.new(Tool.Handle.CFrame.p, (Mouse.Hit.p - Tool.Handle.CFrame.p).unit * 300)
print("ray")
local Hit, Position = game.Workspace:FindPartOnRay(Ray, Player)
print("Hit, position")
local Humanoid = Hit and Hit.Parent and Hit.Parent:findFirstChild("Humanoid")
print("Found hum")
if Humanoid then
Humanoid:TakeDamage(30)
end
local Distance = (Position - Tool.Handle.CFrame.p).magnitude
print("Distance")
local RayPart = Instance.new("Part", Player)
print("RayPart")
RayPart.Name = "RayPart"
RayPart.BrickColor = BrickColor.new("Royal purple")
print("Royal purple")
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.Handle.CFrame.p) * CFrame.new(0, 0, -Distance /2)
print("CFrame")
game.Debris:AddItem(RayPart, 0.1)
print("Added to Debris")
end)
Output:
Tool has activated ray Hit, position Found hum Distance RayPart Royal purple CFrame Added to Debris

3 comments
Does a
printin the function confirm the Activated event is actually firing?Indeed. I will use some prints around it and check for the results.
I have edited the code to the print version, all the prints are happening.