I am trying to make a system where when a player is touching a part, and when they press “V”, an animation plays. (I plan on editing more).
Here’s what I have so far:
-- Replace this with the actual part you want to detect touches on
local part = game.Workspace.WaterDrinkFolder.WaterDetection
-- Replace this with the actual animation ID you want to play
local animationId = "rbxassetid://17075750640"
-- Function to handle input
local function onInput(input, gameProcessedEvent)
if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.V then
local player = game.Players:GetPlayerFromCharacter(input.UserInputType == Enum.UserInputType.Keyboard and game.Players.LocalPlayer.Character or nil)
if player then
local character = player.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid and part:IsPointInRegion(humanoid.RootPart.Position) then
local anim = Instance.new("Animation")
anim.AnimationId = animationId
local playAnim = humanoid:LoadAnimation(anim)
playAnim:Play()
end
end
end
end
end
-- Connect the input event
game:GetService("UserInputService").InputBegan:Connect(onInput)
I think you should use part:GetTouchingParts() and loop through the parts and check if they’re descendant of character, in which case proceed to playing animation
I think it is because it transforms the deprecated usage into the new one (I believe it creates an animator when you try loading an animation onto humanoid), but long term it’s better to use an animator
Uh, does input service even work in server scripts? I don’t think so, you should rewrite the script to a LocalScript and please use the animator documentation.