What does the shoot
function look like? As 7fis mentioned earlier you’re trying to use a RaycastResult
instead of a Ray
. Did you for example put the RaycastResult
returned by workspace:Raycast()
into workspace:FindPartOnRay()
?
I am having a hard time understanding what you are saying. Here is the primary part of my shoot function:
print("MOBILE")
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {player.Character}
rayParams.FilterType = Enum.RaycastFilterType.Exclude
local rayDirection
local rayLength = 100
--local rayResult = nil
local rayOrigin = tool.Handle.CFrame.p
local screenCenter = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)
local rayFromScreen = camera:ViewportPointToRay(screenCenter.X, screenCenter.Y)
rayResult = workspace:Raycast(camera.CFrame.Position, camera.CFrame.LookVector * rayLength, rayParams)
if rayResult then
rayDirection = rayResult.Position - rayOrigin
else
rayDirection = (rayFromScreen.Direction).unit * 300
end
local ray = workspace:Raycast(rayOrigin, rayDirection)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
print(rayResult)
You used the local variable ray
which is a RaycastResult
in workspace:FindPartOnRay()
as the first parameter.
The method workspace:FindPartOnRay()
expects a Ray
instead of RaycastResult
as its first parameter.
You can see what each Roblox method returns by looking at the rightmost value in the autocompletion window and preferably by reading through its documentation both in the same window and on Roblox’s documentation page:
You can also see what parameters the method expects through the same window and also on Roblox’s documentation pages:
Here’s the full code which now uses a second ray cast to get the part that was hit by the gun:
print("MOBILE")
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {player.Character}
rayParams.FilterType = Enum.RaycastFilterType.Exclude
local rayDirection
local rayLength = 100
local rayOrigin = tool.Handle.CFrame.p
local screenCenter = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)
local rayFromScreen = camera:ViewportPointToRay(screenCenter.X, screenCenter.Y)
local rayResult = workspace:Raycast(camera.CFrame.Position, camera.CFrame.LookVector * rayLength, rayParams)
if rayResult then
rayDirection = rayResult.Position - rayOrigin
else
rayDirection = (rayFromScreen.Direction).unit * 300
end
-- Cast ray from gun handle, returns nil if no part was hit
local gunRayResult = workspace:Raycast(rayOrigin, rayDirection, rayParams)
local part, position = nil, nil
if gunRayResult then
part = gunRayResult.Instance
position = gunRayResult.Position
end
print(rayResult)
The following rows:
local ray = workspace:Raycast(rayOrigin, rayDirection)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
changed to:
-- Cast ray from gun handle, returns nil if no part was hit
local gunRayResult = workspace:Raycast(rayOrigin, rayDirection, rayParams)
local part, position = nil, nil
if gunRayResult then
part = gunRayResult.Instance
position = gunRayResult.Position
end
Also rayResult
wasn’t initialized as a local variable since local rayResult = nil
was commented out
u a w man
tysm for taking the time to help me!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.