Fastcast .rayhit event triggers multiple times based on how many previous shots were landed

Whenever .rayhit triggers, it adds by 1 from the previous shot and triggers based on it. The logs below demonstrates it. I’ve looked through these forums:

But none worked.
Also please excuse if I mess up something I’m new to the devforums.

local dbg = 0
 
local function onHit(cast, result : RaycastResult, velocity, tracer)
    dbg += 1
    print(dbg)
    
    local part = result.Instance
    local char = part:FindFirstAncestorWhichIsA("Model")
    local dontPenetrate = {
        Enum.Material.Concrete;
        Enum.Material.Cobblestone;
        Enum.Material.Metal;
        Enum.Material.Brick;
        Enum.Material.Basalt;
        Enum.Material.Pavement;
        Enum.Material.Rock;
        Enum.Material.CrackedLava;
        Enum.Material.DiamondPlate;
    }
 
    if result.Instance then
        local cosmetic
        if table.find(dontPenetrate, result.Material) then
            cosmetic = game.ReplicatedStorage.cosmetic.bullethole_small:Clone()
        else
            cosmetic = game.ReplicatedStorage.cosmetic.bullethole_big:Clone()
        end
        cosmetic.Parent = workspace.Debris
        cosmetic.CFrame = CFrame.new(result.Position, result.Position + result.Normal)
        local random = math.random(1, 2)
        if random == 1 then
            playSound(4427234167, cosmetic, .5)
        elseif random == 2 then
            playSound(4427232788, cosmetic, .5)
        end
        game.Debris:AddItem(cosmetic, 10)
    end
 
    if result.Material == Enum.Material.Glass then
        require(modules.GlassShattering):shatter(part, result.Position, result.Normal)
        tracer:Destroy()
    elseif table.find(dontPenetrate, result.Material) then
        tracer.CanCollide = true
        tracer.Trail.Enabled = false
        tracer.Anchored = false
        game.Debris:AddItem(tracer, 5)
    else
        tracer:Destroy()
    end
 
    if char and char:FindFirstChildWhichIsA("Humanoid") then
        if part.Name == "Head" then
            char.Humanoid:TakeDamage(require(plr.Character:FindFirstChildWhichIsA("Tool").gun).lethaldamage)
        else
            char.Humanoid:TakeDamage(require(plr.Character:FindFirstChildWhichIsA("Tool").gun).damage)
        end
    end
end
 
plr.CharacterAdded:connect(function()

plr.Character.ChildAdded:Connect(function(child)
		if bullet ~= nil then return end
		bullet = fastcast.new()
	end)
	
plr.Character.gunclient.events.setpose.OnServerInvoke = (function(eplr, gun, pose, arg, check)
        if pose == "fire" then
            if gun.gun.ammo.Value > 0 then
                local bulletbehavior = bullet.newBehavior()
                local params = RaycastParams.new()
                params.IgnoreWater = true
                params.FilterType = Enum.RaycastFilterType.Exclude
                params.FilterDescendantsInstances = {gun.Parent, workspace.Debris}
                bulletbehavior.RaycastParams = params
                bulletbehavior.MaxDistance = require(gun.gun).range
                bulletbehavior.Acceleration = Vector3.new(0, -workspace.Gravity/2, 0)
                bulletbehavior.CosmeticBulletTemplate = game.ReplicatedStorage.cosmetic.tracer
                bulletbehavior.CosmeticBulletContainer = workspace.Debris
 
                bullet:Fire(gun.Handle.muzzle.WorldPosition, (arg - gun.Handle.muzzle.WorldPosition).Unit, require(gun.gun).speed, bulletbehavior)
                gun.gun.ammo.Value -= 1
                
                --⸻⸻⸻⸻⸻⸻⸻⸻
            
                bullet.RayHit:Connect(onHit)
                bullet.LengthChanged:Connect(lengthChanged)
 
                playSound(require(gun.gun).firesound, gun.Handle, 1)
 
                gun.Handle.muzzle.fire:Emit()
                gun.Handle.muzzle.smoke:Emit(10)
            
                local shell = game.ReplicatedStorage.cosmetic.shell:Clone()
                shell.CanCollide = true
                shell.CollisionGroup = "CharactersCollide"
                shell.Parent = gun.Handle.eject
                
                shell.CFrame = gun.Handle.eject.WorldCFrame
                game.Debris:AddItem(shell, 3)
            end
        end
    end)
end)
end)```

13:43:08.562 FIRED - Server - Script:159
13:43:08.564 1 - Server - Script:29
13:43:09.816 FIRED - Server - Script:159
13:43:09.817 2 - Server - Script:29
13:43:09.818 3 - Server - Script:29
13:43:12.040 FIRED - Server - Script:159
13:43:12.042 4 - Server - Script:29
13:43:12.042 5 - Server - Script:29
13:43:12.042 6 - Server - Script:29

You are supposed to disconnect the event that registers hit (I believe it’s bullet.RayHit:Connect(onHit)) once you’re done with it.

It disconnects itself. That is what the docs say at least.
https://etithespir.it/FastCastAPIDocs/fastcast-objects/caster/#rbxscriptsignal-rayhitactivecast-raycastresult-vector3-instance

Not it does not. The docs say to connect to the events only once and not everytime it fires.

That doesn’t make sense. How would I detecte if the cast that fires at

 bullet:Fire(gun.Handle.muzzle.WorldPosition, (arg - gun.Handle.muzzle.WorldPosition).Unit, require(gun.gun).speed, bulletbehavior)

would hit something?

You create the event once per object just like doing part.touched

plr.Character.ChildAdded:Connect(function(child)
		if bullet ~= nil then return end
		bullet = fastcast.new()

--put the rayhit connection here for this fastcast.
	end)

Thank you so much. Gosh I regret using fastcast sometimes.

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