I making a game that have melee weapon with raycast hitbox it’s work very well but when I stand and face to other character’s corner my raycast hitbox get larger range that make player can damage to another player or npc with longer range.
Here is my server script:
local Players = game:GetService("Players")
local runservice = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local eventFolder = RS:WaitForChild("event")
local event = eventFolder.activatedEvent
local attachments = script.Parent.Sword:GetDescendants()
local damageDebounce = false
local rayParamsCheckList = workspace:GetDescendants()
local rayParamsList = {}
Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(chr)
for _, v in pairs(rayParamsCheckList) do
if v:IsA("BasePart") and v.CanCollide == true and not v.Parent:FindFirstChildWhichIsA("Humanoid") then
table.insert(rayParamsList, v)
end
end
end)
end)
event.OnServerEvent:Connect(function(plr)
local chr = plr.Character
local hum = chr:FindFirstChildWhichIsA("Humanoid")
local humTouchedList = {}
local rayActivatedDebounce = false
local rayActivatedDebounceUpdateFunc = coroutine.wrap(function()
wait(0.3)
rayActivatedDebounce = true
end)
rayActivatedDebounceUpdateFunc()
for _, v in pairs(attachments) do
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = rayParamsList
local rayPositionUpdateFunc = coroutine.wrap(function()
while rayActivatedDebounce == false do
local rayTouchedFunc = coroutine.wrap(function(ray)
if ray then
local otherHum = ray.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
if not table.find(humTouchedList, otherHum) and otherHum then
if otherHum ~= hum then
table.insert(humTouchedList, otherHum)
otherHum:TakeDamage(10)
end
end
end
end)
local ray = workspace:Raycast(v.WorldPosition, v.WorldPosition, rayParams)
--[[ showing raycast position
local part = Instance.new("Part", workspace)
part.Shape = Enum.PartType.Ball
part.BrickColor = BrickColor.new(1, 0, 0.0156863)
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(0.1, 0.1, 0.1)
part.Position = v.WorldPosition
]]--
rayTouchedFunc(ray)
runservice.Heartbeat:Wait()
end
end)
rayPositionUpdateFunc()
end
end)
And I also have local script that will detect when player activated tool that gonna fire server event.
Sorry for bad english I sometime use google translate
If anyone know how to fix this issue please help me I finding about how to fix this in devforum and youtube but I found nothing that can solve this problem Thank you!