Raycast doesnt work?

  1. What do you want to achieve? Keep it simple and clear!

so i make it so that when you click it raycast but the raycast didnt
work and idk why and there are no errors too.

heres the code:

LocalScript

local player = game.Players.LocalPlayer

local char = player.Character or player.CharacterAdded:Wait()
local direction = HumanoidRootPart.CFrame.LookVector * 6

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		local rayParam = RaycastParams.new()
		rayParam.FilterType = Enum.RaycastFilterType.Exclude
		rayParam.FilterDescendantsInstances = {char}

		local rayResult = workspace:Raycast(HumanoidRootPart.Position, direction, rayParam)
		
		if rayResult == nil then return end
		local otherchar = rayResult.Instance.Parent
		local player = game.Players:GetPlayerFromCharacter(otherchar)
		if player == nil then return end
		
		attackEvent:FireServer(otherchar)
	end
end)

ServerScript

local RS = game:GetService("ReplicatedStorage")

local attackEvent = RS:WaitForChild("AttackEvent")

attackEvent.OnServerEvent:Connect(function(plr, char)
	char.Humanoid:TakeDamage(100)
end)

Your direction is outside the input began function and its pretty short 6 studs.

Make sure you access this property inside the input began function to update the direction on click.

I also recommend using a part to visualize the raycast.

changed it to 20 studs yet i found out theres no raycast result

i already put the direction in the input began function but it still wont work

i ended up switching to spatial queries lol thanks for the direction explaination it worked

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