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)

2 comments
I'll give it a shot.
It works! By "direction", I thought it meant the direction FROM the origin position. Thanks!