What i am trying to do
I am making a keycard door system, the issue is that the code is not working. Im not really sure what is wrong, if it is the code or the animation. All help is apprichated!
Video
Code:
local Prompt = script.Parent -- The proximity prompt
local AnimId = "rbxassetid://---------" -- The animation ID
-- Create and configure an Animation instance
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimId
local activeTracks = {} -- Table to track active animations per player
-- Helper function to get or create an Animator
local function getOrCreateAnimator(humanoid)
local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
animator = Instance.new("Animator")
animator.Parent = humanoid
end
return animator
end
-- Helper function to handle keycard positioning
local function positionKeycard(character, keycard)
local rightArm = character:FindFirstChild("Right Arm")
if rightArm and keycard then
keycard.ArmWeld.Part1 = rightArm
keycard.CFrame = rightArm.CFrame * CFrame.new(0, 1.1, 0) * CFrame.Angles(math.rad(-90), 0, 0)
print("Grabbed keycard")
else
warn("Keycard or Right Arm not found for character:", character.Name)
end
end
-- Handles when a player begins holding the prompt
local function onPromptHoldBegan(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
local keycard = character:FindFirstChild("KeycardHolder") and character.KeycardHolder:FindFirstChild("CharacterKeycard")
if not humanoid then
warn("No Humanoid found for player:", player.Name)
return
end
local animator = getOrCreateAnimator(humanoid)
-- Freeze player movement
humanoid.WalkSpeed = 0
character.HumanoidRootPart.CFrame = Prompt.Parent.CharacterPosition.CFrame
-- Weld character to the prompt position
local weld = Instance.new("WeldConstraint")
weld.Part0 = character.HumanoidRootPart
weld.Part1 = Prompt.Parent.CharacterPosition
weld.Parent = character.HumanoidRootPart
-- Play animation
local track = animator:LoadAnimation(Animation)
track:Play()
activeTracks[player] = track
-- Animation markers
track:GetMarkerReachedSignal("Grab"):Connect(function()
positionKeycard(character, keycard)
end)
track:GetMarkerReachedSignal("Release"):Connect(function()
print("Released keycard")
-- Optional: Add release logic here
end)
end
-- Handles when a player stops holding the prompt
local function onPromptHoldEnded(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass("Humanoid")
-- Stop the animation
local track = activeTracks[player]
if track then
track:Stop()
activeTracks[player] = nil
end
-- Restore player movement
if humanoid then
humanoid.WalkSpeed = 16
end
-- Remove weld
local weld = character.HumanoidRootPart:FindFirstChildOfClass("WeldConstraint")
if weld then
weld:Destroy()
end
print("Animation stopped for player:", player.Name)
end
-- Connect proximity prompt events
Prompt.PromptButtonHoldBegan:Connect(onPromptHoldBegan)
Prompt.PromptButtonHoldEnded:Connect(onPromptHoldEnded)
Thank you!