Im trying to recreate swimming mechanics from fe2 and im having a few issues

Sometimes when the player exits the water they are still able to swim up and down i cant come up with a solution

same thing with animations playing after player exits water

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local SwimEvent = ReplicatedStorage.Swim

local Splash = script.Splash

local Debounce = false
local ExitDebounce = false

local Animator = Humanoid:WaitForChild("Animator")
local AnimationsFolder = script.Animations

local FloatAnimation = AnimationsFolder.Float
local FloatTrack = Animator:LoadAnimation(FloatAnimation)

local SwimAnimation = AnimationsFolder.Swim
local SwimTrack = Animator:LoadAnimation(SwimAnimation)

local SwimUpAnimation = AnimationsFolder.SwimUp
local SwimUpTrack = Animator:LoadAnimation(SwimUpAnimation)

local SwimDownAnimation = AnimationsFolder.SwimDown
local SwimDownTrack = Animator:LoadAnimation(SwimDownAnimation)



SwimEvent.OnClientEvent:Connect(function(Water)
	if Debounce == true then return end
	Debounce = true
	Splash:Play()
	FloatTrack:Play()
	
	
	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Parent = HumanoidRootPart
	BodyVelocity.MaxForce = Vector3.new(0,5000,0)
	BodyVelocity.Velocity = Vector3.new(0,0,0)
	----Dives
	UserInputService.InputBegan:Connect(function(Input,GameProcessedEvent)
		if Input.KeyCode == Enum.KeyCode.Space and GameProcessedEvent ~= true then
			BodyVelocity.Velocity = Vector3.new(0,20,0)
			SwimUpTrack:Play()
			SwimDownTrack:Stop()
			FloatTrack:Stop()
			SwimTrack:Stop()
		elseif Input.KeyCode == Enum.KeyCode.E and GameProcessedEvent ~= true then
			BodyVelocity.Velocity = Vector3.new(0,-20,0)
			SwimDownTrack:Play()
			SwimUpTrack:Stop()
			FloatTrack:Stop()
			SwimTrack:Stop()
		end
	end)

	UserInputService.InputEnded:Connect(function(Input,GameProcessedEvent)
		if Input.KeyCode == Enum.KeyCode.Space and GameProcessedEvent ~= true then
			BodyVelocity.Velocity = Vector3.new(0,0,0)
			SwimUpTrack:Stop()
			FloatTrack:Play()
		elseif Input.KeyCode == Enum.KeyCode.E and GameProcessedEvent ~= true then
			BodyVelocity.Velocity = Vector3.new(0,0,0)
			SwimDownTrack:Stop()
			FloatTrack:Play()
		end
	end)
----Swims

	UserInputService.InputBegan:Connect(function(Input,GameProcessedEvent)
		if Input.KeyCode == Enum.KeyCode.W and GameProcessedEvent ~= true then
			SwimTrack:Play()
		elseif Input.KeyCode == Enum.KeyCode.A and GameProcessedEvent ~= true then
			SwimTrack:Play()
		elseif Input.KeyCode == Enum.KeyCode.D and GameProcessedEvent ~= true then
			SwimTrack:Play()	
		elseif Input.KeyCode == Enum.KeyCode.S and GameProcessedEvent ~= true then
			SwimTrack:Play()
		end
	end)
	local function AnimationStopper()
		if Humanoid.MoveDirection.Magnitude == 0 then
			SwimTrack:Stop()
		end
	end
	
	Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(AnimationStopper)
	
	Water.TouchEnded:Connect(function(Hit)
		if Hit.Parent == Character and ExitDebounce == false then
			ExitDebounce = true
			BodyVelocity.Velocity = Vector3.new(0,50,0)
			wait(0.1)
			BodyVelocity:Destroy()
			FloatTrack:Stop()
			SwimTrack:Stop()
			Debounce = false
			ExitDebounce = false
		end
		
	end)
	
	
end)

2 Likes

Instead of using .Touched and .TouchEnded, instead you should use :GetPartsInPart().

1 Like

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