How do I make it so when a player touches a certain part they play the swimming animation?
Place this in a LocalScript in StarterPlayerScripts (located in StarterPlayer)
local part = game.Workspace.Part -- replace with your part location
local debounce = false
part.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) and debounce == false then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
debounce = true
humanoid:SetStateEnabled("GettingUp", false)
humanoid:ChangeState(Enum.HumanoidStateType.Swimming)
end
end
end)
part.TouchEnded:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
debounce = false
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
end)
local Enumeration = Enum
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local RunService = Game:GetService("RunService")
local Part = Workspace.Part
local function OnRenderStep()
local Parts = Workspace:GetPartsInPart(Part)
for _, Player in ipairs(Players:GetPlayers()) do
local Character = Player.Character
if not Character then continue end
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if not Humanoid then continue end
local State
for _, Descendant in ipairs(Character:GetDescendants()) do
if not (Descendant:IsA("BasePart")) then continue end
if table.find(Parts, Descendant) then State = true break end
end
Humanoid:SetStateEnabled(Enumeration.HumanoidStateType.GettingUp, not State)
if State then Humanoid:ChangeState(Enumeration.HumanoidStateType.Swimming) end
end
end
RunService.RenderStepped:Connect(OnRenderStep)
Local script.