Custom Swimming System

I need somebody to make a custom swimming system for me because I’ve genuinely tried as hard as I could and nothing worked. I kind of got some of it but it’s just too difficult for me.

Water Script:

local waterPart = workspace.Water
local RegionCheckInterval = 0.5 
local waterSurfaceHeight = waterPart.Position.Y + waterPart.Size.Y/2

-- Function to repeatedly check characters within the water region   
local function checkSwimmingCharacters()
	local partsInWater = workspace:GetPartsInPart(waterPart)

	for _, part in pairs(partsInWater) do
		local humanoid = part.Parent:FindFirstChild("Humanoid")
		if humanoid then
			local rootPart = humanoid.RootPart

			-- Ensure BodyPosition exists, create if needed
			local bodyPosition = rootPart:FindFirstChild("BodyPosition")
			if not bodyPosition then
				bodyPosition = Instance.new("BodyPosition")
				bodyPosition.Parent = rootPart
			end

			-- Set BodyPosition properties
			bodyPosition.MaxForce = Vector3.new(0, math.huge, 0) -- Strong upward force
			bodyPosition.D = 500  -- Adjust the dampening as needed
			bodyPosition.P = 2000 -- Adjust the positioning force as needed
			bodyPosition.Position = Vector3.new(
				rootPart.Position.X, 
				waterSurfaceHeight, 
				rootPart.Position.Z
			)

			-- (No need for State Change handling when using BodyPosition)
		end
	end
end

-- Start repeated region checks
while true do
	checkSwimmingCharacters()
	wait(RegionCheckInterval)
end

Animation:

local yourWaterPart = workspace.Water 
local swimAnimationId = "rbxassetid://16361131542" 
local swimAnimationTrack

-- Function to start swimming animation (similar to before)
local function startSwimmingAnimation()
	if swimAnimationTrack and swimAnimationTrack.IsPlaying then 
		return 
	end 
	swimAnimationTrack = humanoid:LoadAnimation(swimAnimationId)
	swimAnimationTrack.Priority = Enum.AnimationPriority.Action 
	swimAnimationTrack:Play()
end

-- Detect when the player enters the water using Touched
yourWaterPart.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		if humanoid:GetState() ~= Enum.HumanoidStateType.Swimming then
			humanoid:ChangeState(Enum.HumanoidStateType.Swimming)
			startSwimmingAnimation() -- Start the animation here
		end
	end
end)

-- (You'll likely need a way to detect exiting the water as well)
yourWaterPart.TouchEnded:Connect(function(hit)
	-- Add logic to stop animation using stopSwimmingAnimation() if needed
end) 

image

image

1 Like
1 Like