Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Raycast not detecting parts?

I'm really struggling to get this raycast to work. I can't seem to find a consistent setup for it to work. I'm attempting to raycast from a player's HRP to a part I have scripted to be set to 8 units in front of them, but every time I try to do the raycast, it seems to only sometimes work.

Local script:

game.Loaded:Wait() -- To prevent loading issues

local run = game:GetService('RunService')
local player = game.Players.LocalPlayer
player.CharacterAdded:Wait()

local char = player.Character
local debounce = false -- To prevent excessive transfurming
local rep = game.ReplicatedStorage -- For convenience
local mouse = player:GetMouse()
local rayPart = rep.RayPart:Clone() -- Helps raycast
local anim = script.Collapse -- Animation
local collapseTrack = char:WaitForChild('Humanoid').Animator:LoadAnimation(anim)
collapseTrack.Priority = Enum.AnimationPriority.Action -- Plays animation regardless of other animations

rayPart.Parent = workspace.RayPartFolder -- For raycast blacklist

local HRP = player.Character:WaitForChild('HumanoidRootPart')

rep.EverythingClientSideOfTransfurm.OnClientEvent:Connect(function()
	run.Heartbeat:Connect(function()
		if player.Transfurmed.Value then
			rayPart.CFrame = HRP.CFrame:ToWorldSpace(CFrame.new(0, 0, -8)) -- Helps raycast
		end
	end)
	
	collapseTrack:Play()
	
	mouse.Button1Down:Connect(function()
		if player.Transfurmed.Value then -- If player is transfurmed, because event is still connected after death/untransfurmation
			if not debounce then -- Prevents spam of transfurmation
				debounce = true
				print('Mouse clicked')
				local rayParams = RaycastParams.new()
			
				rayParams.FilterDescendantsInstances = {player.Character, workspace.RayPartFolder}
				rayParams.FilterType = Enum.RaycastFilterType.Blacklist
				
				local rayResult = workspace:Raycast(HRP.Position, rayPart.Position, rayParams) -- Just a raycast
				
				if rayResult ~= nil then
					print(rayResult.Instance)
					print((HRP.Position - rayResult.Instance.Position).magnitude) -- Just some test prints
				end
				if rayResult ~= nil and game.Players:GetPlayerFromCharacter(rayResult.Instance.Parent) and (HRP.Position - rayResult.Instance.Position).magnitude < 8 then
					rep.TransfurmOtherPlayer:FireServer(game.Players:GetPlayerFromCharacter(rayResult.Instance.Parent))
				end
				
				wait(5)
				
				debounce = false
			end
		end
	end)
	
	collapseTrack.Stopped:Wait()
	
	char.LeftLeg:Destroy()
	char.RightLeg:Destroy()
	char.LeftArm:Destroy()
	char.RightArm:Destroy()
	
	local TBP
	if player.Color.Value == 'White' then
		TBP = rep.TransfurmedBodyParts.White
	end
	local limbs = {
		TBP.LeftArm:Clone(),
		TBP.RightArm:Clone(),
		TBP.LeftLeg:Clone(),
		TBP.RightLeg:Clone()
	}
	for i, v in pairs(limbs) do
		v.Parent = char
	end
	
	run.Heartbeat:Connect(function()
		limbs[1].CFrame = char['Left Leg'].CFrame
		limbs[2].CFrame = char['Right Leg'].CFrame
		limbs[3].CFrame = char['Left Arm'].CFrame
		limbs[4].CFrame = char['Right Arm'].CFrame -- To put transfurmed limbs on players character correctly on the client's screen
	end)
end)

1 answer

✓ Accepted answer

I think it is because you are using Raycast wrong on 39, the second argument of it is direction while you are putting your Part's position. Try doing this:

local rayResult = workspace:Raycast(
	HRP.Position,

	--/ When getting direction, always use 'that object minus this object'
	--/ Also you see i am using [Unit] and multiplication with your
	--\ max distance, that's so your ray has your desired length
	--\ so :ToWorldSpace() is not needed in this case
	(rayPart.Position - HRP.Position).Unit * YOUR_DISTANCE,
	rayParams
)

Now i did not test it but you get the point, you need to get the direction as an argument 2, not the position.

2 comments

0

I'll give it a shot.

0

It works! By "direction", I thought it meant the direction FROM the origin position. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.