Hello! I’m currently experiencing an issue, where my raycast script (local) only sometimes works, and does not react to the range parameter at all! This is probably an oversight i’ve made, but i’ve become very annoyed by it. I’d appreciate any help!
Local Script:
--> Services
local Replicated_Storage_Service = game:GetService("ReplicatedStorage")
local Context_Action_Service = game:GetService("ContextActionService")
local Player_Service = game:GetService("Players")
local User_Input_Service = game:GetService("UserInputService")
--> References
local Modules_Folder = Replicated_Storage_Service:WaitForChild("Modules")
--> Player
local Player = Player_Service.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
--> Modules
local CF = require(Modules_Folder.Custom_Functions)
--> Values
local Action_Name_1 = "Attack"
local Range = 1
local Debounce = false
--> Main Functions
local function Attack()
if Debounce == false then
Debounce = true
print("Fired")
local Hit_Part, Position = CF.Raycast(Character.HumanoidRootPart.Position, Mouse.Hit.p, Range, Character, true)
print(Hit_Part)
task.wait(0.4)
Debounce = false
end
end
Context_Action_Service:BindAction(Action_Name_1, Attack, true, Enum.UserInputType.MouseButton1)
It isn’t complete, as i was just trying to get the raycast working! Here’s the module it calls upon:
function CF.Raycast(Start, Direction, Range, Ignore, Debug)
local Raycast_Parameters = RaycastParams.new(); Raycast_Parameters.FilterDescendantsInstances = {Ignore}; Raycast_Parameters.FilterType = Enum.RaycastFilterType.Blacklist
local Raycast_Result = workspace:Raycast(Start, (Direction - Start) * Range, Raycast_Parameters)
if Raycast_Result then
if Debug == true then
local distance = (Start - Raycast_Result.Position).Magnitude
local p = Instance.new("Part")
p.Anchored = true
p.CanCollide = false
p.Size = Vector3.new(0.1, 0.1, distance)
p.Color = Color3.new(1, 0, 0)
p.Transparency = 0.7
p.CFrame = CFrame.lookAt(Start, Raycast_Result.Position)*CFrame.new(0, 0, -distance/2)
p.Parent = game.Workspace
end
print("Returned")
return Raycast_Result.Instance, Raycast_Result.Position, Raycast_Result.Normal
else
print("No Result!")
end
end
I’ve found a raycast visualiser, which i used to try to help me diagnose it! Now here’s a video of the issue: Raycasting Issues - Only works sometimes!
As you can see from the video above (i’ve uploaded it to streamable.com as it was too large), you can see the majority of the time, it returns nil as shown in the output! Does anyone have any help on this? I’d appreciate it a ton!