RayHitHandler doing more damage than normal

I wan’t to fix this problem.
image
Here is the RayHitHandler code

-- Ray Hit Handler (Damage, Effects, etc.)
local function onRayHit(cast, result, velocity, bullet)
    local hitPart = result.Instance
    if hitPart ~= nil and hitPart.Parent ~= nil then
        local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
        if humanoid then
            humanoid:TakeDamage(50) -- Damage.
            print(humanoid.Parent.Name.." has taken "..humanoid.Health.." Damage") -- line 88
        end
    end
    game:GetService("Debris"):AddItem(bullet, 2)
end

Also heres the entire script.

local FireEvent = game.ReplicatedStorage.GameMechanics.Remotes.Ruger1022Fire
local ReloadEvent = game.ReplicatedStorage.GameMechanics.Remotes.ReloadEvent
local FastCast = require(game.ReplicatedStorage.GameMechanics.Modules.FastCast.FastCastRedux)
local PartCacheModule = require(game.ReplicatedStorage.GameMechanics.Modules.FastCast.PartCache)

-- Constants
local DEBUG = false
local BULLET_SPEED = 1000
local BULLET_MAXDIST = 1000
local BULLET_GRAVITY = Vector3.new(0, -workspace.Gravity, 0)
local MIN_BULLET_SPREAD_ANGLE = 1
local MAX_BULLET_SPREAD_ANGLE = 4
local BULLETS_PER_SHOT = 1
local RELOAD_TIME = 2
local MAX_AMMO = 15
local FIRE_COOLDOWN = 0.2
local PIERCE_DEMO = true

-- Variables
local tool = script.Parent
local handle = tool:FindFirstChild("Handle")
local caster = FastCast.new()
local lastFireTime = 0

-- Bullet Template Setup
local bulletTemplate = Instance.new("Part")
bulletTemplate.Anchored = true
bulletTemplate.CanCollide = false
bulletTemplate.Material = Enum.Material.Neon
bulletTemplate.Size = Vector3.new(0.2, 0.2, 0.2)
bulletTemplate.Color = Color3.fromRGB(158, 104, 11)
bulletTemplate.Shape = "Ball"

-- Cosmetic Bullet Provider
local CosmeticBulletsFolder = workspace.GameMechanics:FindFirstChild("BulletsFolder")
CosmeticBulletsFolder.Name = "BulletsFolder"
local CosmeticPartProvider = PartCacheModule.new(bulletTemplate, 100, CosmeticBulletsFolder)

-- Raycast Params and Behavior
local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Exclude
castParams.IgnoreWater = true

local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.MaxDistance = BULLET_MAXDIST
castBehavior.CosmeticBulletContainer = CosmeticBulletsFolder
castBehavior.CosmeticBulletProvider = CosmeticPartProvider
castBehavior.Acceleration = BULLET_GRAVITY
castBehavior.AutoIgnoreContainer = false

local function Reflect(surfaceNormal, bulletNormal)
	return bulletNormal - (2 * bulletNormal:Dot(surfaceNormal) * surfaceNormal)
end

local function onEquipped()
	castParams.FilterDescendantsInstances = {tool.Parent, CosmeticPartProvider}
end

local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
	if bullet then 
		local bulletLength = bullet.Size.Z/2
		local offset = CFrame.new(0, 0, -(length - bulletLength))
		bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
	end
end

-- Reload Function
local function reload(player, ammoType)
	local ammoFolder = player:FindFirstChild("AmmoStats")
	if not ammoFolder then return end

	local ammoCount = ammoFolder:FindFirstChild(ammoType)
	if not ammoCount or ammoCount.Value == MAX_AMMO then return end

	ammoCount.Value = 0
	task.wait(RELOAD_TIME)
	ammoCount.Value = MAX_AMMO
end

-- Ray Hit Handler (Damage, Effects, etc.)
local function onRayHit(cast, result, velocity, bullet)
	local hitPart = result.Instance
	if hitPart ~= nil and hitPart.Parent ~= nil then
		local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid:TakeDamage(50) -- Damage.
			print(humanoid.Parent.Name.." has taken "..humanoid.Health.." Damage") -- line 88
		end
	end
	game:GetService("Debris"):AddItem(bullet, 2)
end

function OnRayPierced(cast, result, velocity, bullet)
	-- You can do some really unique stuff with pierce behavior - In reality, pierce is just the module's way of asking "Do I keep the bullet going, or do I stop it here?"
	-- You can make use of this unique behavior in a manner like this, for instance, which causes bullets to be bouncy.
	local position = result.Position
	local normal = result.Normal

	local newNormal = Reflect(normal, velocity.Unit)
	cast:SetVelocity(newNormal * velocity.Magnitude)

	-- It's super important that we set the cast's position to the ray hit position. Remember: When a pierce is successful, it increments the ray forward by one increment.
	-- If we don't do this, it'll actually start the bounce effect one segment *after* it continues through the object, which for thin walls, can cause the bullet to almost get stuck in the wall.
	cast:SetPosition(position)

	-- Generally speaking, if you plan to do any velocity modifications to the bullet at all, you should use the line above to reset the position to where it was when the pierce was registered.
end

-- Bullet Firing Function
local function fire(player, mousePosition, tool)
	local currentTime = tick()
	if currentTime - lastFireTime < FIRE_COOLDOWN then
		print("Fire cooldown active!")
		return
	end
	lastFireTime = currentTime

	local ammoTypeInstance = tool:FindFirstChild("AmmoType")
	if not ammoTypeInstance then
		warn("AmmoType not found in tool!")
		return
	end
	local ammoType = ammoTypeInstance.Value

	local ammoFolder = player:FindFirstChild("AmmoStats")
	if not ammoFolder then
		warn("AmmoStats folder missing for player:", player.Name)
		return
	end

	local ammoCount = ammoFolder:FindFirstChild(ammoType)
	if not ammoCount or ammoCount.Value <= 0 then
		warn("No ammo left!")
		return
	end
	local firePoint = tool.Handle:FindFirstChild("FirePoint")
	ammoCount.Value = ammoCount.Value - 1
	print("Ammo after firing:", ammoCount.Value)
	local origin = firePoint.WorldPosition
	print("Fire origin:", origin)
	local direction = (mousePosition - origin).Unit
	print("Fire direction:", direction)
	local adjustedDirection = direction * (1 + math.random(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE) / 100)

	for _ = 1, BULLETS_PER_SHOT do
		caster:Fire(origin, adjustedDirection, BULLET_SPEED, castBehavior)
		tool.Equipped:Connect(onEquipped)
		caster.RayPierced:Connect(OnRayPierced)
		caster.LengthChanged:Connect(onLengthChanged)
		caster.RayHit:Connect(onRayHit)
	end

	if DEBUG then
		print("Firing bullet from:", tostring(origin), "to", tostring(mousePosition))
	end
end


-- Attach Event Listeners
FireEvent.OnServerEvent:Connect(function(player, mousePosition, tool)
	if tool and tool:IsA("Tool") then
		fire(player, mousePosition, tool)
	end
end)

ReloadEvent.OnServerEvent:Connect(reload)

Im trying to fix this but im stumpted on why it keeps doing more damage from a far distance or even being close to the dummy.

I read through the code quickly since i need to go sleep but, i’ve noticed there’s no debounce.
Maybe that could solve it.

added a debounce didnt change anything.

Maybe it hits multiple body part inside a character and thats why it does more damage than usual?

you were correct fixed the problem.

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