#Delayed sounds on reached mark

1 messages · Page 1 of 1 (latest)

undone jasper
#

I've made my own script responsible for walk animations, and also wanted to implement footstep sounds and some effects but problem is that said sounds are pretty much delayed

--Footsteps

local lastFootstepSound = nil
local firstStep = true


local function PlayFootstepSound(Material, foot)
    local sounds = FootstepSounds[Material]
    if not sounds then return end

    local random = Random.new()
    local id = sounds[random:NextInteger(1, #sounds)]


    if id and id ~= lastFootstepSound then
        lastFootstepSound = id
        local sfx = Instance.new("Sound")

        sfx.SoundId = id
        sfx.RollOffMaxDistance = 100
        sfx.RollOffMinDistance = 10
        sfx.PlaybackSpeed = 1.5
        sfx.Volume = 0.5
        sfx.Parent = foot

        sfx:Play()
        task.spawn(function()
            sfx.Ended:Wait()
            sfx:Destroy()
        end)
    else
        PlayFootstepSound(Material, foot)
    end
end

local function OnFootstep(foot: string, Material: Enum.Material)
    if typeof(foot) == "string" then
        foot = character:FindFirstChild(foot.." Leg")
    end
    if not foot or not Material then return end
    
    
    PlayFootstepSound(Material, foot)
end

local function GetMaterial()
    local RayOrigin = root.Position
    local RayDirection = Vector3.new(0, -8, 0)
    
    local Params = RaycastParams.new()
    Params.FilterType = Enum.RaycastFilterType.Exclude
    Params.FilterDescendantsInstances = {character}
    
    local Results = workspace:Raycast(RayOrigin, RayDirection, Params)
    if Results then
        
        return Results.Instance:GetAttribute("Material")
    end
    
    return
end

animTracks.Run:GetMarkerReachedSignal("Footstep"):Connect(function(Param)
    if firstStep then firstStep = false return end
    
    local foot = Param
    local Material: string = GetMaterial()
    OnFootstep(foot, Material)
end)
animTracks.Run.Stopped:Connect(function()
    firstStep = true
end)

RemoveRunningSound()
root.ChildAdded:Connect(RemoveRunningSound)
undone jasper
#

Nevermind guys, it's a studio problem