Swimming Script

I have a swim script, it’s costume water and not actual roblox water so detecting if the player is in water won’t fix my issue. The problem is that you can activate sprint when in the water and I just want to player to stay at a walking speed of 16. There’s a lot more other scripts to the swimming so if you need them just ask. I just really need help fixing this. Thank you!

Here is the swim script:

--Services{
local replicatedStorage = game:GetService("ReplicatedStorage")
local inputService = game:GetService("UserInputService")
local context = game:GetService("ContextActionService")
local playerService = game:GetService("Players")
local runService = game:GetService("RunService")
local lighting = game:GetService("Lighting")
--}

--Vars{
local player = playerService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local diveCorrection = lighting:WaitForChild("DiveCorrection")
local humanoid = character:WaitForChild("Humanoid")
local diveBlur = lighting:WaitForChild("DiveBlur")
local seaValue = script:WaitForChild("SeaValue")
local sea = seaValue.Value
local ocean = sea:WaitForChild("Ocean")
local sand = sea:WaitForChild("Sand")
--}

--LocalVars{
local swimPosition = nil
local surfacing = false
local isDiving = false
local swimming = false
local canJump = false
local isDive = false

local swimTrack = humanoid:LoadAnimation(game.ReplicatedFirst.Animations.Swim)
--}

--Tables{
local oceanSetting = {
	["LastCheck"] = DateTime.now().UnixTimestamp;
	["TimerChange"] = 25;
	["CurrentCount"] = 1;
	
}

local textureList = {
	"http://www.roblox.com/asset/?id=251911331";
	"http://www.roblox.com/asset/?id=251911339";
	"http://www.roblox.com/asset/?id=251911347";
	"http://www.roblox.com/asset/?id=251911363";
	"http://www.roblox.com/asset/?id=251911369";
	"http://www.roblox.com/asset/?id=251911379";
	"http://www.roblox.com/asset/?id=251911392";
	"http://www.roblox.com/asset/?id=251911407";
	"http://www.roblox.com/asset/?id=251911419";
	"http://www.roblox.com/asset/?id=251911429";
	"http://www.roblox.com/asset/?id=251911442";
	"http://www.roblox.com/asset/?id=251911454";
	"http://www.roblox.com/asset/?id=251911472";
	"http://www.roblox.com/asset/?id=251911481";
	"http://www.roblox.com/asset/?id=251911490";
	"http://www.roblox.com/asset/?id=251911502";
	"http://www.roblox.com/asset/?id=251911515";
	"http://www.roblox.com/asset/?id=251911532";
	"http://www.roblox.com/asset/?id=251915177";
	"http://www.roblox.com/asset/?id=251911587";
	"http://www.roblox.com/asset/?id=251911594";
	"http://www.roblox.com/asset/?id=251911605";
	"http://www.roblox.com/asset/?id=251911616";
	"http://www.roblox.com/asset/?id=251911630";
	"http://www.roblox.com/asset/?id=251911639";
}

--}




player.CharacterAdded:Connect(function(char)
	character = char
	humanoidRootPart = char:WaitForChild("HumanoidRootPart")
	humanoid = char:WaitForChild("Humanoid")
	swimPosition = nil
	surfacing = false
	isDiving = false
	swimming = false
	canJump = false
	isDive = false
	swimTrack = humanoid:LoadAnimation(game.ReplicatedFirst.Animations.Swim)
	ocean.Position = Vector3.new(humanoidRootPart.Position.X, ocean.Position.Y, humanoidRootPart.Position.Z)
	sand.Position = Vector3.new(humanoidRootPart.Position.X, sand.Position.Y, humanoidRootPart.Position.Z)
end)

humanoid.Destroying:Connect(function()
	swimPosition = nil
	surfacing = false
	isDiving = false
	swimming = false
	canJump = false
	isDive = false
	swimTrack:Stop()
end)

ocean.Touched:Connect(function(otherPart)
	if swimming == true then return end
	if otherPart.Parent == character and otherPart == character.PrimaryPart then
		swimming = true
		
		game.ReplicatedStorage.Bindables.PlayEffect:Fire("Splash", humanoidRootPart.CFrame, ocean.Position.Y + 7)
		
		swimPosition = Instance.new("BodyPosition")
		swimPosition.MaxForce = Vector3.new(0, 100000, 0)
		swimPosition.Position = Vector3.new(0, ocean.Position.Y + 7.5, 0)
		
		runService.RenderStepped:Wait()
		
		task.wait(.05)
		
		swimTrack:Play()
		swimPosition.Parent = humanoidRootPart
		
		task.wait(.25)
		canJump = true
	end
end)

inputService.JumpRequest:Connect(function()
	if swimPosition ~= nil and swimming and canJump and isDiving == false and isDive == false and surfacing == false then
		canJump = false
		swimPosition.Position = swimPosition.Position + Vector3.new(0, 25, 0)
		swimTrack:Stop()
		task.wait(.5)
		swimPosition:Destroy()
		
		swimming = false
	end
end)


runService.RenderStepped:Connect(function()
	runService.RenderStepped:Wait()
	if character ~= nil then
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if humanoidRootPart ~= nil then
			local distCheck = (character:GetPivot().Position - ocean.Position).Magnitude
			if distCheck >= 750 then
				ocean.Position = Vector3.new(humanoidRootPart.Position.X, ocean.Position.Y, humanoidRootPart.Position.Z)
				sand.Position = Vector3.new(humanoidRootPart.Position.X, sand.Position.Y, humanoidRootPart.Position.Z)
			end
		end
	end
	
	local camera = workspace.CurrentCamera
	if camera.CFrame.Y < ocean.Position.Y + 8 then
		diveBlur.Enabled = true
		diveCorrection.Enabled = true
	else
		diveBlur.Enabled = false
		diveCorrection.Enabled = false
	end
end)

And here is the sprint script incase you need it:

local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")

local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://18567622681"
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Movement

local RUN_KEYCODE = Enum.KeyCode.LeftControl
local RUN_SPEED = 36
local WALK_SPEED = 16

local isSprinting = false
local isMoving = false
local isInAir = false

local function floorMaterialChanged()
	if hum.FloorMaterial == Enum.Material.Air then
		isInAir = true

		track:Stop()
	else
		isInAir = false

		if isSprinting and isMoving and not track.IsPlaying then
			track:Play()
		end
	end
end

local function updateAnimation()
	if isSprinting and isMoving then
		hum.WalkSpeed = RUN_SPEED
		if not track.IsPlaying then
			track:Play()
		end
	else
		hum.WalkSpeed = WALK_SPEED
		track:Stop()
	end
end

local function inputBegan(input, processed)
	if processed then return end

	if input.KeyCode == RUN_KEYCODE then
		isSprinting = not isSprinting
		updateAnimation()
	end
end

local function moveDirectionChanged()
	isMoving = hum.MoveDirection.Magnitude > 0
	if not isInAir then
		updateAnimation()
	end
end

userInputService.InputBegan:Connect(inputBegan)

hum:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)

hum:GetPropertyChangedSignal("FloorMaterial"):Connect(floorMaterialChanged)

A quick way you can achieve this is by using attributes, in this case “Swimming” on the character

--Services{
    local replicatedStorage = game:GetService("ReplicatedStorage")
    local inputService = game:GetService("UserInputService")
    local context = game:GetService("ContextActionService")
    local playerService = game:GetService("Players")
    local runService = game:GetService("RunService")
    local lighting = game:GetService("Lighting")
    --}
    
    --Vars{
    local player = playerService.LocalPlayer
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
    local diveCorrection = lighting:WaitForChild("DiveCorrection")
    local humanoid = character:WaitForChild("Humanoid")
    local diveBlur = lighting:WaitForChild("DiveBlur")
    local seaValue = script:WaitForChild("SeaValue")
    local sea = seaValue.Value
    local ocean = sea:WaitForChild("Ocean")
    local sand = sea:WaitForChild("Sand")
    --}
    
    --LocalVars{
    local swimPosition = nil
    local surfacing = false
    local isDiving = false
    local swimming = false
    local canJump = false
    local isDive = false
    
    local swimTrack = humanoid:LoadAnimation(game.ReplicatedFirst.Animations.Swim)
    --}
    
    --Tables{
    local oceanSetting = {
        ["LastCheck"] = DateTime.now().UnixTimestamp;
        ["TimerChange"] = 25;
        ["CurrentCount"] = 1;
        
    }
    
    local textureList = {
        "http://www.roblox.com/asset/?id=251911331";
        "http://www.roblox.com/asset/?id=251911339";
        "http://www.roblox.com/asset/?id=251911347";
        "http://www.roblox.com/asset/?id=251911363";
        "http://www.roblox.com/asset/?id=251911369";
        "http://www.roblox.com/asset/?id=251911379";
        "http://www.roblox.com/asset/?id=251911392";
        "http://www.roblox.com/asset/?id=251911407";
        "http://www.roblox.com/asset/?id=251911419";
        "http://www.roblox.com/asset/?id=251911429";
        "http://www.roblox.com/asset/?id=251911442";
        "http://www.roblox.com/asset/?id=251911454";
        "http://www.roblox.com/asset/?id=251911472";
        "http://www.roblox.com/asset/?id=251911481";
        "http://www.roblox.com/asset/?id=251911490";
        "http://www.roblox.com/asset/?id=251911502";
        "http://www.roblox.com/asset/?id=251911515";
        "http://www.roblox.com/asset/?id=251911532";
        "http://www.roblox.com/asset/?id=251915177";
        "http://www.roblox.com/asset/?id=251911587";
        "http://www.roblox.com/asset/?id=251911594";
        "http://www.roblox.com/asset/?id=251911605";
        "http://www.roblox.com/asset/?id=251911616";
        "http://www.roblox.com/asset/?id=251911630";
        "http://www.roblox.com/asset/?id=251911639";
    }
    
    --}
    
    
    
    
    player.CharacterAdded:Connect(function(char)
        character = char
        humanoidRootPart = char:WaitForChild("HumanoidRootPart")
        humanoid = char:WaitForChild("Humanoid")
        swimPosition = nil
        surfacing = false
        isDiving = false
        swimming = false
        canJump = false
        isDive = false
        swimTrack = humanoid:LoadAnimation(game.ReplicatedFirst.Animations.Swim)
        ocean.Position = Vector3.new(humanoidRootPart.Position.X, ocean.Position.Y, humanoidRootPart.Position.Z)
        sand.Position = Vector3.new(humanoidRootPart.Position.X, sand.Position.Y, humanoidRootPart.Position.Z)
        char:SetAttribute("Swimming", false)
    end)
    
    humanoid.Destroying:Connect(function()
        swimPosition = nil
        surfacing = false
        isDiving = false
        swimming = false
        canJump = false
        isDive = false
        swimTrack:Stop()
    end)
    
    ocean.Touched:Connect(function(otherPart)
        if swimming == true then return end
        if otherPart.Parent == character and otherPart == character.PrimaryPart then
            character:SetAttribute("Swimming", true)
            swimming = true
            
            game.ReplicatedStorage.Bindables.PlayEffect:Fire("Splash", humanoidRootPart.CFrame, ocean.Position.Y + 7)
            
            swimPosition = Instance.new("BodyPosition")
            swimPosition.MaxForce = Vector3.new(0, 100000, 0)
            swimPosition.Position = Vector3.new(0, ocean.Position.Y + 7.5, 0)
            
            runService.RenderStepped:Wait()
            
            task.wait(.05)
            
            swimTrack:Play()
            swimPosition.Parent = humanoidRootPart
            
            task.wait(.25)
            canJump = true
        end
    end)
    
    inputService.JumpRequest:Connect(function()
        if swimPosition ~= nil and swimming and canJump and isDiving == false and isDive == false and surfacing == false then
            canJump = false
            swimPosition.Position = swimPosition.Position + Vector3.new(0, 25, 0)
            swimTrack:Stop()
            task.wait(.5)
            swimPosition:Destroy()
            
            swimming = false
            character:SetAttribute("Swimming", false)
        end
    end)
    
    
    runService.RenderStepped:Connect(function()
        runService.RenderStepped:Wait()
        if character ~= nil then
            local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
            if humanoidRootPart ~= nil then
                local distCheck = (character:GetPivot().Position - ocean.Position).Magnitude
                if distCheck >= 750 then
                    ocean.Position = Vector3.new(humanoidRootPart.Position.X, ocean.Position.Y, humanoidRootPart.Position.Z)
                    sand.Position = Vector3.new(humanoidRootPart.Position.X, sand.Position.Y, humanoidRootPart.Position.Z)
                end
            end
        end
        
        local camera = workspace.CurrentCamera
        if camera.CFrame.Y < ocean.Position.Y + 8 then
            diveBlur.Enabled = true
            diveCorrection.Enabled = true
        else
            diveBlur.Enabled = false
            diveCorrection.Enabled = false
        end
    end)
local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")

local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://18567622681"
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Movement

local RUN_KEYCODE = Enum.KeyCode.LeftControl
local RUN_SPEED = 36
local WALK_SPEED = 16

local isSprinting = false
local isMoving = false
local isInAir = false

local function floorMaterialChanged()
	if hum.FloorMaterial == Enum.Material.Air then
		isInAir = true

		track:Stop()
	else
		isInAir = false

		if isSprinting and isMoving and not track.IsPlaying then
			track:Play()
		end
	end
end

local function updateAnimation()
	if isSprinting and isMoving then
		hum.WalkSpeed = RUN_SPEED
		if not track.IsPlaying then
			track:Play()
		end
	else
		hum.WalkSpeed = WALK_SPEED
		track:Stop()
	end
end

local function inputBegan(input, processed)
	if processed then return end

	if input.KeyCode == RUN_KEYCODE then
		if not isSprinting and char:GetAttribute("Swimming") then
			return
		end

		isSprinting = not isSprinting
		updateAnimation()
	end
end

local function moveDirectionChanged()
	isMoving = hum.MoveDirection.Magnitude > 0
	if not isInAir then
		updateAnimation()
	end
end

userInputService.InputBegan:Connect(inputBegan)

hum:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)

hum:GetPropertyChangedSignal("FloorMaterial"):Connect(floorMaterialChanged)
1 Like

Works great, couple issues but they are minor and I can hopefully fix

also its better to use humanoid.animator:LoadAnimation() instead of humanoid:LoadAnimation() as loading it on the humanoid is deprecated

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