I am looking to make a tree-cutting system by using raycasts which has been done.
I have gotten the script to work 90%, I am relatively new to raycasting but I have gone from HumanoidRootPart to mouse Viewpoint to detect whether a tree has been hit by the raycast. The issue I am facing is that on some angles whilst near the tree, the raycast does not detect the tree EVERY time. I have tried adjusting the rayDirection distance by multiplying it by a larger number, same issue occurs
See videos.
If anyone is wondering why I have repeated raycast on both client and server, I am not an expert at exploit-proofing but my general idea was that it would be a check on the server to prevent exploiters, same thing with the server debounce (works)
--- client
local Axe = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Remote = game.ReplicatedStorage:WaitForChild("TreeCast")
local HRP = character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local mouse = player:GetMouse()
local camera = workspace.Camera
local list = {character:GetDescendants()}
local Debounce = false;
Axe.Activated:Connect(function()
print(Debounce)
if Debounce == false then
local raycastParameters = RaycastParams.new()
raycastParameters.FilterDescendantsInstances = list
raycastParameters.FilterType = Enum.RaycastFilterType.Exclude
local rayOrigin = Vector3.new(HRP.CFrame.LookVector,HRP.Position.Y,HRP.CFrame.LookVector)
local rayDirection = (mouse.Hit.Position - rayOrigin).Unit * 5
local raycastResult = workspace:Raycast(Axe.Handle.Position, rayDirection, raycastParameters)
if raycastResult then
print(raycastResult.Instance)
if raycastResult.Instance.Name == "Tree" or raycastResult.Instance.Name == "TreeModel" then
print("Tree hit.")
Debounce = true;
print('RI43')
Remote:FireServer(Axe.Handle, character, HRP, camera, mouse.Hit.Position)
end
end
else
return
end
end)
Remote.OnClientEvent:Connect(function(tree)
print(tree)
Debounce = false;
print("Client rmeote fired percent gong dwonb")
local GUI = tree:WaitForChild("UI")
local Percentage = GUI:WaitForChild("Percentage")
print(Percentage)
local Percent = Percentage.Size.X.Scale
if Percentage.Size.X.Scale > 0 then
Percentage.Size = UDim2.new(Percentage.Size.X.Scale - 0.35, 0,Percentage.Size.Y.Scale, 0)
end
end)
-- server script (below)
local Remote = game.ReplicatedStorage:FindFirstChild("TreeCast")
local playerDebounce = {}
local TreeModel
Remote.OnServerEvent:Connect(function(player, Axe, character, HRP, camera, mouse)
if not playerDebounce[player.Name] then
playerDebounce[player.Name] = true
local list = {}
for _, v in pairs(game:GetService("Players"):GetPlayers()) do
for _, q in pairs(v.Character:GetDescendants()) do
if q:IsA("BasePart") then
table.insert(list, v)
end
end
end
table.insert(list, Axe)
table.insert(list, character:GetDescendants())
local raycastParameters = RaycastParams.new()
raycastParameters.FilterDescendantsInstances = list
raycastParameters.FilterType = Enum.RaycastFilterType.Exclude
local rayOrigin = Vector3.new(HRP.CFrame.LookVector.X,HRP.Position.Y,HRP.CFrame.LookVector.Z)
local rayDirection = (mouse - rayOrigin).Unit * 5
local raycastResult = workspace:Raycast(Axe.Position, rayDirection, raycastParameters)
if raycastResult ~= nil and raycastResult.Instance.Name == "Tree" then
print("Tree found!!")
TreeModel = raycastResult.Instance
wait(1)
-- TreeModel:Destroy()
playerDebounce[player.Name] = nil
Remote:FireClient(player, TreeModel)
else
wait(1)
playerDebounce[player.Name] = nil
Remote:FireClient(player, TreeModel)
return
end
end
end)
Any help would be appreciated.