I am trying to do a raycast from a wand to target mouse position but it somehow returns nil instead of raycast result. I tried fixing origin and direction -which had no problems whatsoever- and it didnt work. I don’t even use raycastParameters. Help please
could you include the code and where you are clicking/what you are clicking on?
StarterCharacterScripts | LocalScript
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Mouse = Player:GetMouse()
local function contains(table, val)
for i=1,#table do
if table[i] == val then
return true
end
end
return false
end
local AvailableSpells = {"test"}
Player.Chatted:Connect(function(message, recipient)
if not Character:FindFirstChild("Wand") then
return
end
message = message:lower()
if contains(AvailableSpells, message) then
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event:RemoteEvent = ReplicatedStorage:WaitForChild("SpellEvent")
local Wand:Tool = Character.Wand
local Handle:Part = Wand:FindFirstChild("Handle")
Event:FireServer(message, Handle.Position, Handle, Mouse.Hit.Position)
end
end)
ServerScriptService | Script
local SpellEvent = game.ReplicatedStorage.SpellEvent
local Module = require(game.ReplicatedStorage.SpellModule)
local function spellCheck(player, spell, wandPos, handle, endPos)
if spell == "test" then
print(tostring(wandPos) .. "\n" .. tostring(endPos))
Module.SendBullet(wandPos, endPos, handle, function() end)
end
end
SpellEvent.OnServerEvent:Connect(spellCheck)
ReplicatedStorage | ModuleScript | SpellModule
local ray = {}
function ray.SendBullet(startPos, endPos, handle, onImpact)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {handle}
params.FilterType = Enum.RaycastFilterType.Exclude
params.IgnoreWater = true
local raycastResult = workspace:Raycast(startPos, endPos - startPos)
local part = Instance.new("Part", workspace)
local instance:BasePart = raycastResult.Instance
part.Position = instance.Position
part.Size = Vector3.new(.5,.5,.5)
part.Anchored = true
part.Material = Enum.Material.Neon
part.Color = Color3.new(1,0,0)
end
return ray
You type the message and it raycasts.
RaycastResults sadly don’t get returned if your raycast doesn’t hit anything (ex: you aimed in the air).
I aim it at a BasePart though and every time it returns nil
You should (at least first) perform a raycast on the client.
Also, you’re not using the raycastparams that you defined earlier.
You should check to make sure the raycast result is valid before you use it and you should probably get the unit vector and define a range because the ray being cast only goes up to the object, and doesn’t intersect it.
Another thing to look for is to see if the parts you’re raycasting to have CanQuery enabled. Raycasts ignore parts that have this property disabled.
I have it enabled on all parts in workspace
Same result as others. Just nil
I defined a range and tried unit but still didnt work
Send the updated code please…(char limit)
is CanQuery of the part you are hitting true?
Yeah I double checked it for sure.
Localscript
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Mouse = Player:GetMouse()
local function contains(table, val)
for i=1,#table do
if table[i] == val then
return true
end
end
return false
end
local AvailableSpells = {"test"}
Player.Chatted:Connect(function(message, recipient)
if not Character:FindFirstChild("Wand") then
return
end
message = message:lower()
if contains(AvailableSpells, message) then
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event:RemoteEvent = ReplicatedStorage:WaitForChild("SpellEvent")
local Wand:Tool = Character.Wand
local Handle:Part = Wand:FindFirstChild("Handle")
local params = RaycastParams.new()
params.FilterDescendantsInstances = {Handle}
params.FilterType = Enum.RaycastFilterType.Exclude
params.IgnoreWater = true
local raycastResult = workspace:Raycast(Handle.Position, (Mouse.Hit.Position - Handle.Position).Unit * 1000, params)
Event:FireServer(message, raycastResult, Handle.Position)
end
end)
Serverscript
local SpellEvent = game.ReplicatedStorage.SpellEvent
local Module = require(game.ReplicatedStorage.SpellModule)
local function spellCheck(player, spell, raycastResult:RaycastResult, startPos:Vector3)
if spell == "test" then
Module.SendBullet(startPos, raycastResult.Position, 20)
end
end
SpellEvent.OnServerEvent:Connect(spellCheck)
ModuleScript
local ray = {}
function ray.SendBullet(startPos, endPos, steps)
local instance = Instance.new("Part", workspace)
instance.Anchored = true
instance.CanCollide = false
instance.Size = Vector3.new(.5,.5,.5)
instance.Color = Color3.new(0,1,0)
for i=0,steps,1 do
instance.Position = startPos:Lerp(endPos, i/steps)
wait(0.3)
end
end
return ray
try printing the result in the local script
It prints it normally in the localscript. Now its getting weirder
Well because of that I modified the function so it replaces raycastResult with the hit position and it works so thx.