The following script is a global script that fires when I say something specific in a local script in that case for testing purposes I chose "IDK", and when I click after saying "IDK" with the activated event on the tool it will fire the remote event, for some reason the ray cast is casting sometimes at the direction of the mouse but not always. Sometimes it just casts randomly to the sky or to the sides of the tool, any suggestions why? The problem is at line 29
script.RemoteEvent.OnServerEvent:connect(function(Player,Mousehitp,Mousehit)
repeat wait() until Player.Character
Character = Player.Character
tool = script.Parent.Parent
local laser = Instance.new("Part")
laser.Name = "Laser"
laser.FormFactor = Enum.FormFactor.Custom
laser.TopSurface, laser.BottomSurface = 0, 0
laser.Size = Vector3.new(0.2, 0.2, 10)
laser.BrickColor = BrickColor.Blue()
laser.Material = "Neon"
laser.Anchored = true
laser.CanCollide = false
laser.Locked = true
laser.CFrame = tool.Tip.CFrame
laser.Parent = game.Workspace
local maxDistance = 400
local curDistance = 0
local stepDistance = 2.5
local stepWait = 0
local StartPoint = tool.Tip.Position
local function Step(overrideDistance)
-- Cast ray:
local ray = Ray.new(StartPoint, Mousehit.p.Unit * (overrideDistance or stepDistance))
local hit, pos, norm = game.Workspace:FindPartOnRayWithIgnoreList(ray, {tool.Tip,Character})
local ignore = {Character,tool.Tip}
local hit, position, normal = game.Workspace:FindPartOnRay(ray, ignore)
-- Update laser position:
laser.CFrame = CFrame.new(StartPoint:lerp(pos, 0.5), pos)
local oldPos = StartPoint
StartPoint = pos
if (hit) then
end
curDistance = (curDistance + (pos - oldPos).magnitude)
-- Apply fade effect to laser as it approaches max distance from < 75 studs:
if (curDistance > (maxDistance - 75)) then
local d = (curDistance - (maxDistance - 75)) / 75
laser.Transparency = d
end
-- Recurse if max distance not reached:
if (curDistance < maxDistance) then
wait(stepWait)
Step()
end
end
Step()
-- Done! Destroy laser:
laser:Destroy()
end)
