Code and animation issue

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!

from what I can observe, the keycard is anchored when it is welded to the player and playing the animation, causing that to happen. Unanchor the keycard, wherever it is.

I can also see a few errors popping up because “track” isn’t defined in the “onPromptHoldEnded” function which doesn’t stop the animation. Define “track” outside of the functions so both functions can use it or redefine the animation in “onPromptHoldEnded”. This also applies to “weld”. I suggest using “FindFirstDescendant” to find the weld in the player.

2 Likes

So, i realised i have two welds in the keycard, and im not disabling the one that welds the torso and keycard, so i did it and the keycard moved to the hand correctly, but the glitch where the character moves still occur, any ideas on how to solve it?

1 Like

That is most likely caused by when you set the CFrame of the player’s character or any of their limbs.

1 Like

Alright, thank you for the help! ill contine trying stuff to fix this!

1 Like