Hello developers, I am currently working on a slot purchase system. I have created a system, when a slot gets touched, 8 raycasts fire near it to get the nearby slots. Consequently, we can work with a table of slots near the target slot to unlock new slots.
Unfortuately, the system doesn’t see any nearby slots, even tought there are. Here is my script and the slot systems image in order for you to have a clearer image of the situation.
local module = {}
local raycastLength = 12
-- PRINT THE RESULT
local function printRaycastRes(raycastResult)
if raycastResult then
print("Instance:" .. raycastResult.Instance)
print("Distance:" .. raycastResult.Distance)
else
print("no res")
end
end
-- FIRE THE RAY
local function fireRay(rayOrigin, rayDirection)
local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
printRaycastRes(raycastResult)
end
-- CREATING THE PARAMETERS FOR RAYCAST (MAIN ISSUE)
function module.PurchaseSlot(slot)
for i = 1, 8 do
-- create a ray origin
-- use if statements to create the ray direction parameters
-- combine the origin with direction and fire 8 raycasts (this version has only 1 for testing)
local rayOrigin = slot.Position + Vector3.new(0, 8, 0)
local rayDirection
-- Assigning the positions (X and Z primarily to get all the slots nearby, there will be 8 if & elseif states)
if i == 1 then
rayDirection = slot.Position + Vector3.new(0, 0, raycastLength)
fireRay(rayOrigin, rayDirection)
end
end
end
return module
print out items like your slot.Position so you can see where the actual items are.
Does the fact that your rayOrigin is 8 studs up from your slot.Position keep the ray from hitting anything?
You can try making a Part as a ray indicator that shows where it’s actually firing from.
Does the part the ray is supposed to contact have its CanQuery Property off?
I have analysed the ray origin and the ray directions position carefully. Here they are:
The ray directly passes through the target slot, however, I get no raycast result. Also, the target slot has its CanQuery property on.
Also, as you can see there is a 3D model in between two positions. The models CanQuery property is off and I have also tried to completely delete it. Nothing seems to work.
and if it dont work try to switch the rayOrigin and slot.Position (slot.Position - rayOrigin) can’t remember which way around it is right now
edit: also try -raycastlength, trial and error is my favourite with raycasts to be honest
This actually helped me get a raycast result, however the result is only the part itself. Here is the actual problem, the ray doesn’t get a direction on the X or Z axis and only on Y. The issues image and the current script for you to get a better understanding is below.
Yeah, I now managed to successfully raycast. However, here is an other issue. Now, the raycast hits the character if its in the way and doesn’t continue to raycast until it touches the slot. Basically, it returns the characters part instead continuing its path and touching the targeted slot.
I tried turning characters parts CanQuery to false when it gets added to workspace, however it also doesn’t seem to work. Is there an other solution for this?
In the Raycasting documentation check out the Filtering section. You can choose which Parts are able to be detected by the ray. It’ll ignore all other items.