Hello there. I have made a swimming script that functions for pc and mobile, but for some reason, swimming up and down on mobile is possible, but for pc its not.
Some solutions I’ve tried so far:
Rewriting the entire script
Print debugging
Here’s my entire script:
-- please dont copy this
local contextActionService = game:GetService("ContextActionService")
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local humanoid = character:WaitForChild("Humanoid")
local rootpart = humanoid.RootPart
local waterName = "_Water"
local isSwimming = false
local swimmingUP = false
local swimmingDOWN = false
local jumpButton
local swimUpKey = Enum.KeyCode.Space
local swimDownKey = Enum.KeyCode.LeftShift
local splashIn = script:WaitForChild("SplashIn")
local splashOut = script:WaitForChild("SplashOut")
local swimmables = {
"BasePart",
"Part",
"UnionOperation",
"TrussPart",
"WedgePart",
"CornerWedgePart"
}
function startSwim()
if isSwimming == false then
local rootpartVelocity = rootpart.Velocity
local swimVelocity = Instance.new("BodyVelocity")
swimVelocity.Name = "SwimVelocity"
swimVelocity.P = 8000
swimVelocity.MaxForce = Vector3.new(rootpartVelocity.X, 8000, rootpartVelocity.Z)
swimVelocity.Velocity = Vector3.new(0, 0, 0)
swimVelocity.Parent = rootpart
local swimDiveVelocity = Instance.new("BodyVelocity")
swimDiveVelocity.Name = "SwimDiveVelocity"
swimDiveVelocity.MaxForce = Vector3.new(0, 8000, 0)
swimDiveVelocity.Velocity = Vector3.new(rootpartVelocity.X, rootpartVelocity.Y, rootpartVelocity.Z)
swimDiveVelocity.Parent = head
humanoid.HipHeight = -3
humanoid:ChangeState("Freefall")
local diveTween = tweenService:Create(
swimDiveVelocity,
TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0),
{Velocity = Vector3.new(rootpartVelocity.X, 0, rootpartVelocity.Z)})
diveTween:Play()
game.Debris:AddItem(swimDiveVelocity, 0.2)
isSwimming = true
end
end
function stopSwim()
if isSwimming == true then
local rootpartVelocity = rootpart.Velocity
isSwimming = false
swimmingDOWN = false
swimmingUP = false
for _,p in next, rootpart:GetChildren() do
if string.find(p.Name, "Swim") then
p:Destroy()
end
end
humanoid.HipHeight = 0
rootpart.Velocity = Vector3.new(rootpartVelocity.X, humanoid.JumpPower * 1.2, rootpartVelocity)
end
end
function handleAction(action, state, input)
if action == "Swim_Up" then
if state == Enum.UserInputState.Begin then
swimmingUP = true
elseif state == Enum.UserInputState.End then
swimmingUP = false
end
elseif action == "Swim_Down" then
if state == Enum.UserInputState.Begin then
swimmingDOWN = true
elseif state == Enum.UserInputState.End then
swimmingDOWN = false
end
end
return (isSwimming and state == Enum.UserInputState.Begin) and Enum.ContextActionResult.Sink or Enum.ContextActionResult.Pass
end
head.Touched:connect(function(hit)
if table.find(swimmables, hit.ClassName) and string.find(hit.Name, waterName) then
startSwim()
end
end)
head.TouchEnded:connect(function(last)
if table.find(swimmables, last.ClassName) and string.find(last.Name, waterName) then
stopSwim()
end
end)
humanoid:GetPropertyChangedSignal("Jump"):connect(function()
if humanoid.Jump and isSwimming then
swimmingUP = true
swimmingDOWN = false
elseif not humanoid.Jump and isSwimming then
swimmingUP = false
elseif humanoid.Jump and not isSwimming then
swimmingUP = false
elseif not humanoid.Jump and not isSwimming then
swimmingUP = false
end
end)
humanoid.Died:connect(function()
if isSwimming == true then
isSwimming = false
swimmingUP = false
swimmingDOWN = false
for _,p in next, rootpart:GetChildren() do
if string.find(p.Name, "Swim") then
p:Destroy()
end
end
for _,p in next, character:GetChildren() do
if p:IsA("BasePart") and not p:FindFirstChild("Force") then
task.spawn(function()
local force = Instance.new("BodyVelocity")
force.Name = "Force"
force.MaxForce = Vector3.new(100, 550 / p:GetMass(), 100)
force.Velocity = Vector3.new(0, 0, 0)
force.P = 500
force.Parent = p
game.Debris:AddItem(force, game.Players.RespawnTime - 0.1)
end)
end
end
end
end)
for _,p in next, player.PlayerGui:GetDescendants() do
if p.Name == "JumpButton" and p:IsA("GuiButton") then
jumpButton = p
break
end
end
contextActionService:BindActionAtPriority("Swim_Down", handleAction, true, 3000, swimDownKey)
contextActionService:SetTitle("Swim_Down", "Dive")
local swimDownButton = contextActionService:GetButton("Swim_Down")
swimDownButton.Name = "DiveButton"
runService.Heartbeat:connect(function(alpha)
if isSwimming == true then
local swimmingVelocity = 0
if swimmingUP == true then
swimmingVelocity = humanoid.WalkSpeed
elseif swimmingDOWN == true then
swimmingVelocity = -humanoid.WalkSpeed
elseif swimmingDOWN == false or swimmingUP == false then
swimmingVelocity = 0
end
local swimVelocity = rootpart:WaitForChild("SwimVelocity")
swimVelocity.Velocity = Vector3.new(0, swimmingVelocity, 0)
swimDownButton.Visible = true
elseif not isSwimming then
swimDownButton.Visible = true
end
end)
if jumpButton and swimDownButton then
swimDownButton.Position = jumpButton.Position - UDim2.new(UDim.new(0, 0), jumpButton.Size.Y + UDim.new(0, 10))
swimDownButton.Size = jumpButton.Size
local slideButtonText = swimDownButton:FindFirstChildWhichIsA("TextLabel")
slideButtonText.TextScaled = true
slideButtonText.Position = UDim2.new(0.1, 0, 0.1, 0)
slideButtonText.Size = UDim2.new(0.8, 0, 0.8, 0)
end
Here are 2 clips that show the swimming behavior for mobile and pc:
Mobile:
https://gyazo.com/f00858cb10ec3a3e2e08d8ed4a6ba063
PC (Can’t even swim up or down)
https://gyazo.com/339c012e689ace0df8b9f39ed64d927f
Any help would be greatly appreciated!