How to pass custom argument into fast cast

I have the following module, it is for AI to be able to shoot the player, I want to be able to have different damage values for each gun the AI might have so to do that I have to somehow retrieve what gun fired the bullet in the Hit function for fast cast, I have no clue how to do that

local module = {}

local BulletsFolder = game.Workspace:WaitForChild("Bullets")
local FastCast = require(game.ReplicatedStorage.FastCastRedux)
local castBehavior = FastCast.newBehavior()

local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Exclude
castParams.IgnoreWater = true

local animationsFolder = game.ReplicatedStorage.Animations

local bulletTemplate = Instance.new("Part")
bulletTemplate.Anchored = true
bulletTemplate.CanCollide = false
bulletTemplate.Size = Vector3.new(0.1, 0.1, 4)
bulletTemplate.Material = Enum.Material.Neon
bulletTemplate.BrickColor = BrickColor.new("Yellow flip/flop")

castBehavior.RaycastParams = castParams
castBehavior.CosmeticBulletContainer = BulletsFolder
castBehavior.CosmeticBulletTemplate = bulletTemplate

local function Hit(cast, result, velocity, bullet)
    local hit = result.Instance
    local character = hit:FindFirstAncestorWhichIsA("Model")
	--local gun = cast.UserData
	
	print(cast)
	
    game.Debris:AddItem(bullet, 0.25)

    if character:FindFirstChild("ISAI") then return end

    if character and character:FindFirstChild("Humanoid") then
        local damage = gun:GetAttribute("Damage") or 20
        character.Humanoid:TakeDamage(damage)
    end

    game.Workspace.Bullets:ClearAllChildren()
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

local caster = FastCast.new()

caster.LengthChanged:Connect(onLengthChanged)
caster.RayHit:Connect(Hit)

function module.Shoot(target, Gun, AI, reloadTime, stationary)
    castParams.FilterDescendantsInstances = {Gun, BulletsFolder, AI, game.Workspace.AIS}

    if not AI then return end

    local audioPlayer = AI:FindFirstChild("HumanoidRootPart"):FindFirstChild("AudioPlayer")
    if not audioPlayer then return end

    local bulletsLeft = Gun:GetAttribute("BulletsLeft")
    local inMag = Gun:GetAttribute("InMag")

    if Gun:GetAttribute("InMag") > 0 then
        Gun:SetAttribute("InMag", Gun:GetAttribute("InMag") - 1)
        local firePoint = Gun.Barrel.FirePoint
        local origin = firePoint.WorldPosition
        local direction = target.Character.HumanoidRootPart.Position - origin

        audioPlayer.AssetId = Gun.ShootSound.SoundId

        if stationary == nil then
            audioPlayer:Play()
        end

		caster:Fire(origin, direction.Unit * 100, 100, castBehavior, Gun)
    else
        local animation = animationsFolder:FindFirstChild(Gun.Name .. "Reload")

        audioPlayer.AssetId = game.ReplicatedStorage.Sounds.GunEmpty.SoundId
        audioPlayer:Play()
        Gun:SetAttribute("Reloading", true)

        AI.Head.HeadUI.TextLabel.Visible = true
        AI.Head.HeadUI.TextLabel.Text = "Reloading..."

        if animation then
            local animationTrack = AI.Humanoid.Animator:LoadAnimation(animation)
            animationTrack:Play()
        end

        audioPlayer.AssetId = game.ReplicatedStorage.Sounds.AIReloading.Reload.SoundId
        audioPlayer:Play()

        task.wait(reloadTime or 2)

        local maxMagSize = Gun:GetAttribute("MagSize")
        local ammoToFill = maxMagSize - inMag

        if bulletsLeft > 0 then
            Gun:SetAttribute("Reloading", false)

            if bulletsLeft >= ammoToFill then
                Gun:SetAttribute("InMag", maxMagSize)
                Gun:SetAttribute("BulletsLeft", bulletsLeft - ammoToFill)
            else
                Gun:SetAttribute("InMag", bulletsLeft)
                Gun:SetAttribute("BulletsLeft", 0)
            end

            AI.Head.HeadUI.TextLabel.Visible = false
            AI.Head.HeadUI.TextLabel.Text = "Reloading..."
            module.Shoot(target, Gun, AI)
        end
    end
end

return module

I should also add the damage value is an attribute under the gun tool

Figured it out, I set an attribute into the bullet that is the “CosmeticBulletTemplate” In fast cast with a value of the guns damage before firing the caster. So later when the bullet is returned in the hit function i can just get that attribute add do that amount of damage

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