In my game there is a swimmable part which simulates water physics. My problem is that players can simply spam jump on the surface of the water and never actually start swimming.
see video below:
here is the main script in which handles the player swimming.
local runService = game:GetService("RunService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local moduleScripts = replicatedStorage:WaitForChild("moduleScripts")
local tweenService = game:GetService("TweenService")
local particles = replicatedStorage:WaitForChild("particles")
local smoke = particles:WaitForChild("smoke")
local players = game:GetService("Players")
local _player = players.LocalPlayer
local equippedTool = _player:WaitForChild("tempValues"):WaitForChild("equippedItem")
local walkSpeed = 20
local sprintSpeed = walkSpeed * 1.5
local fishingWalkSpeed = 5
local _camera = workspace.CurrentCamera
local _userInput = game:GetService("UserInputService")
function _debounce(func)
local isRunning = false
return function()
if not isRunning then
isRunning = true
func()
wait(0.5)
isRunning = false
end
end
end
local Info = TweenInfo.new(
0.3, -- Length(seconds)
Enum.EasingStyle.Sine, -- Easing Style
Enum.EasingDirection.InOut -- Easing Direction
)
local sprintProperties = { FieldOfView = 75 }
local cameraSprintTween = tweenService:Create(game.Workspace.CurrentCamera, Info, sprintProperties)
local walkProperties = { FieldOfView = 70 }
local cameraWalkTween = tweenService:Create(game.Workspace.CurrentCamera, Info, walkProperties)
local character = script.Parent
local newParticle = smoke:Clone()
newParticle.Parent = character:WaitForChild("LeftFoot")
local newParticle2 = smoke:Clone()
newParticle2.Parent = character:WaitForChild("RightFoot")
local humanoid = character:WaitForChild("Humanoid")
humanoid.Running:Connect(function(speed: number?)
local connection: RBXScriptConnection?
if connection then
connection:Disconnect()
end
if speed >= 0 and humanoid.WalkSpeed ~= 5 then
task.wait(2)
connection = runService.RenderStepped:Connect(function()
if not character:FindFirstChild("LeftFoot") then
connection:Disconnect()
return
end
if not string.match(equippedTool.Value,"Fishing Rod") then
if humanoid.MoveDirection.Magnitude > 0 then
local walkspeedTween = tweenService:Create(humanoid,Info,{WalkSpeed = sprintSpeed})
walkspeedTween:Play()
cameraSprintTween:Play()
if humanoid.FloorMaterial == Enum.Material.Air then
character.LeftFoot.smoke.Enabled = false
character.RightFoot.smoke.Enabled = false
else
character.LeftFoot.smoke.Enabled = true
character.RightFoot.smoke.Enabled = true
end
else
cameraWalkTween:Play()
local walkspeedTween = tweenService:Create(humanoid,Info,{WalkSpeed = walkSpeed})
walkspeedTween:Play()
character.LeftFoot.smoke.Enabled = false
character.RightFoot.smoke.Enabled = false
connection:Disconnect()
end
end
end)
end
end)
The way in which going about detecting when the player is jumping on the part is when “isLowerIn” is true. The only thing im struggling with is how do I use this to then stop the player jumping whilst also allowing them to still jump out of the water too.