As said above, I just want to make prompts that play an animation but has the same length as the animation before loading it instead of manually setting durations on the prompts.
I’d also like it if you pointed out a lot of things which are unnecessary in my code:
local ProximityPromptService = game:GetService("ProximityPromptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage.Events
local Animations = ReplicatedStorage.Assets.Animations
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rootpart = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChildOfClass("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
local initialWalkSpeed = humanoid.WalkSpeed
local tweenFo = TweenInfo.new(.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.In)
local danceTrack:AnimationTrack
local dance:Animation
local ContentProvider = game:GetService("ContentProvider")
local Cache = {}
for i, v in Animations:GetDescendants() do
if v:IsA("Animation") then
ContentProvider:PreloadAsync({v.AnimationId})
end
end
local function GetAnimationLength(Animation)
local AssetId = Animation.AnimationId
if Cache[AssetId] then
return Cache[AssetId]
end
local Sequence = game.KeyframeSequenceProvider:GetKeyframeSequenceAsync(AssetId)
local Keyframes = Sequence:GetKeyframes()
local Length = 0
for i = 1, #Keyframes do
local Time = Keyframes[i].Time
if Time > Length then
Length = Time
end
end
Sequence:Destroy()
Cache[AssetId] = Length
return Length
end
ProximityPromptService.PromptShown:Connect(function(prompt)
if prompt.Parent.Parent == workspace.TaskPrompts then
local taskPrompt = prompt.Parent
dance = Animations.Tasks[taskPrompt.Name]
danceTrack = animator:LoadAnimation(dance)
prompt.HoldDuration = GetAnimationLength(dance)
end
end)
ProximityPromptService.PromptButtonHoldBegan:Connect(function(prompt)
if prompt.Parent:IsA("BasePart")and prompt.Parent.Parent == workspace.TaskPrompts then
local taskPrompt = prompt.Parent
local tween = game.TweenService:Create(rootpart, tweenFo, {CFrame = taskPrompt.CFrame})
initialWalkSpeed = humanoid.WalkSpeed
dance = Animations.Tasks[taskPrompt.Name]
danceTrack = animator:LoadAnimation(dance)
humanoid.WalkSpeed = 0
tween:Play()
danceTrack:Play()
danceTrack.Ended:Connect(function()
if danceTrack.TimePosition > (danceTrack.Length-.1) then
humanoid.WalkSpeed = initialWalkSpeed
Events.TaskFinished:Fire(player, taskPrompt.Name, prompt.ObjectText)
end
end)
end
end)
ProximityPromptService.PromptButtonHoldEnded:Connect(function(prompt, player)
if prompt.Parent:IsA("BasePart") then
local taskPrompt = prompt.Parent
humanoid.WalkSpeed = initialWalkSpeed
danceTrack:Stop()
end
end)
Thanks in advance