Bugged Swim System

I attempted to make a swim script. It’s meant to keep the character floating at the top of the part and then play a swimming animation, the animation doesn’t play and buoyancy script is very messed up. I really tried my best and I hope someone can tell me what is wrong.

image

local waterPart = workspace.Water

-- Function to detect when a player enters the water
local function onPlayerEnteredWater(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local bodyForce = Instance.new("BodyForce")
		bodyForce.Force = Vector3.new(0, workspace.Gravity * humanoid.RootPart.AssemblyMass, 0)
		bodyForce.Parent = humanoid.RootPart
		humanoid.StateChanged:Connect(function(oldState, newState)
			if newState ~= Enum.HumanoidStateType.Swimming then
				bodyForce:Destroy() -- Remove buoyancy when exiting water
			end
		end)
	end
end

-- Function to detect when a player exits the water
local function onPlayerExitedWater(part)
	-- (You can find if the player exited via Touched events.)
end

-- Connect Touched events
waterPart.Touched:Connect(onPlayerEnteredWater)
waterPart.TouchEnded:Connect(onPlayerExitedWater) 

image

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animation = "http://www.roblox.com/asset/?id=16361131542" -- Load your swimming animation here (Insert its ID)
local swimAnimationTrack = humanoid:LoadAnimation(animation)
local waterPart = workspace.Water

-- Check if the character is in the water
-- Example using a simple height check:
workspace:GetPropertyChangedSignal("AbsoluteSimulationRadius"):Connect(function()
	local characterBottom = character.PrimaryPart.Position.Y - (character.PrimaryPart.Size.Y/2)
	if characterBottom < waterPart.Position.Y + (waterPart.Size.Y/2) then
		if not swimAnimationTrack.IsPlaying then
			swimAnimationTrack:Play()
		end
	else
		if swimAnimationTrack.IsPlaying then
			swimAnimationTrack:Stop()
		end
	end
end)

image

Please get back to me and tell me what to put and where because I’m a little dumb when it comes to people telling me how to fix stuff.

touched events dont fire on non collide parts
change it to like a getpartsinpart check

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.