Hi everyone, today I wanted to make a door with IK animation of the player. Everything works, but I want when we leave the Part the hand smoothly returns to the original position, not as in the video, the animation is abruptly interrupted and the hand returns to the original position.
At the end of the video you can see that when I leave Part the IK animation is abruptly cut off, and I want the arm to smoothly return to its original position.
-- Make sure ReplicatedStorage and workspace are accessible
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local workspace = game:GetService("Workspace")
-- Make sure the Zone module is accessible
local Zone = require(ReplicatedStorage:WaitForChild("Zone"))
local container = script.Parent
local zone = Zone.new(container)
zone:setAccuracy(Zone.enum.Accuracy.High) -- corrected usage of setAccuracy
-- Function to handle player entering the zone
local function onPlayerEntered(player)
-- Check if the player has a character
local character = player.Character
if not character then
return
end
-- Check if the character has a humanoid
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
return
end
-- Check if IK is already in progress
if humanoid:GetAttribute("IKInProgress") then
return
end
-- Set IKInProgress attribute to prevent overlapping interactions
humanoid:SetAttribute("IKInProgress", true)
-- Check if IKControl exists, create if not
local ikControl = humanoid:FindFirstChildOfClass("IKControl") or Instance.new("IKControl")
if not ikControl:IsA("IKControl") then
-- If IKControl couldn't be created, exit function
humanoid:SetAttribute("IKInProgress", false)
return
end
-- Attach IKControl to humanoid if not already attached
if not ikControl.Parent then
ikControl.Parent = humanoid
ikControl.Type = Enum.IKControlType.Position
-- Find LeftHand and LeftUpperArm
local leftHand = character:FindFirstChild("LeftHand")
local leftUpperArm = character:FindFirstChild("LeftUpperArm")
if not (leftHand and leftUpperArm) then
-- If LeftHand or LeftUpperArm not found, exit function
humanoid:SetAttribute("IKInProgress", false)
ikControl:Destroy() -- Clean up, since IKControl is not used
return
end
ikControl.EndEffector = leftHand
ikControl.ChainRoot = leftUpperArm
end
-- Set the IKControl target
local doorBaseAttachment = workspace.Door and workspace.Door:FindFirstChild("Base") and workspace.Door.Base:FindFirstChild("Attachment")
if not doorBaseAttachment then
-- If Door or Base or Attachment not found, exit function
humanoid:SetAttribute("IKInProgress", false)
return
end
-- Enable IKControl and set target
ikControl.Enabled = true
ikControl.Target = doorBaseAttachment
end
local function onPlayerExited(player)
-- Check if the player has a character
local character = player.Character
if not character then
return
end
-- Check if the character has a humanoid
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
return
end
-- Check if IKControl exists, create if not
local ikControl = humanoid:FindFirstChildOfClass("IKControl") or Instance.new("IKControl")
if not ikControl:IsA("IKControl") then
-- If IKControl couldn't be created, exit function
humanoid:SetAttribute("IKInProgress", false)
return
end
-- Clear the IKControl target
ikControl.Enabled = false
ikControl.Target = nil
humanoid:SetAttribute("IKInProgress", false)
end
-- Connect the onPlayerEntered function to the playerEntered event of the zone
zone.playerExited:Connect(onPlayerExited)
zone.playerEntered:Connect(onPlayerEntered)