Its a simple swim script that’s just supposed to make the player float at the top and play an animation when in ‘Ocean’. It doesn’t work at all and was wondering if anyone can help.
local swimVelocity = nil
local swimming = false
local swimAnimation = script:WaitForChild("SwimAnimation")
local swimTrack = animator:LoadAnimation(swimAnimation)
uis.JumpRequest:Connect(function()
if swimming and swimVelocity ~= nil then
swimming = false
swimVelocity.Position = Vector3.new(0, 20, 0)
swimVelocity.MaxForce = Vector3.new(0, math.huge, 0)
swimTrack:Stop()
swimTrack.TimePosition = 0
wait(0.7)
swimVelocity:Destroy()
swimVelocity = nil
if not humanoid then return end
humanoid.WalkSpeed = 24
end
end)
local function swim(target)
if swimming then return end
-- Check if swimVelocity doesn't exist (is nil)
if swimVelocity == nil then
swimming = true
swimVelocity = Instance.new("BodyPosition")
swimVelocity.Position = Vector3.new(0, 0, 0)
swimVelocity.MaxForce = Vector3.new(0, math.huge, 0)
swimVelocity.Parent = target
if not humanoid then return end
humanoid.WalkSpeed = 50
swimTrack:Play()
end
end
while true do
runService.RenderStepped:Wait()
local Torso = character:FindFirstChild("HumanoidRootPart")
if Torso ~= nil then
if (Torso.Position.Y < -10) and (swimming == false) then
swim(Torso)
else
if (Torso.Position.Y > 0) and (swimming == true) then
swimVelocity:Destroy()
swimVelocity = nil
swimming = false
swimTrack:Stop()
swimTrack.TimePosition = 0
end
end
end
end
Thank You!