Is it possible to detect if function got the result then do something

my goal was to make dash script, and i wanted to make raycast detect if player hitted something on the way then cancel the dash and bounce back

here is the code

`local UIS = game:GetService("UserInputService")
local char = game.Players.LocalPlayer.Character

local HumRootPart = char:FindFirstChild("HumanoidRootPart")
local rootAttach = HumRootPart:WaitForChild("RootAttachment")
local DirLookVec = HumRootPart.CFrame.LookVector

local function RayCast()
	local RayOrigin = HumRootPart.Position
	
	local rayResult = workspace:Raycast(RayOrigin, DirLookVec * 1.5)
	
	if rayResult and not rayResult.Instance:IsDescendantOf(char) then
		local rayNormal = rayResult.Normal
		local XZnormal = Vector3.new(rayNormal.X, 0, rayNormal.Z)
		print(rayResult.Instance)
	end
end

function module.DivingAndCrouch()
	UIS.InputBegan:Connect(function(input, Typing)
		if Typing then return end
		
		if input.KeyCode == Enum.KeyCode.C then
			print("yea")
			local DiveForce = Instance.new("LinearVelocity")
			
			DiveForce.Parent = HumRootPart
			DiveForce.Attachment0 = rootAttach
			
			RayCast()
		end
	end)
end`

note: just follow what title said, i want to make everything said in my goal by myself.

1 Like

set up a if statement checking if the raycast is valid or not and return a value from the function by using

return true

or 

return false

So somthing like this:

local function RayCast()
	local RayOrigin = HumRootPart.Position
	
	local rayResult = workspace:Raycast(RayOrigin, DirLookVec * 1.5)
	
	if rayResult and not rayResult.Instance:IsDescendantOf(char) then
		local rayNormal = rayResult.Normal
		local XZnormal = Vector3.new(rayNormal.X, 0, rayNormal.Z)
		print(rayResult.Instance)
		return true
	else 
		return false
	end
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.