Sprint and Swimming

I have a sprint and a swim script and whenever you are in the water you can’t activate the script, which is what i want, however if you activate the sprint before you enter the water and then go in, you will be sprinting in the water. Just wondering if someone could make some additions to make script that stops the sprint script once they enter the water.

Thanks!

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)
        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)

Sprint Script:

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)

Ask me questions if you have them and again, thanks.

1 Like

Maybe replace

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

In the sprint script with this

if hum.FloorMaterial == Enum.Material.Air then
		isInAir = true

		track:Stop()
elseif hum.FloorMaterial == Enum.Material.Water then
  isInAir = false
  track:Stop()
	else
		isInAir = false

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

The formatting of the scripts are off cuz I’m doing this on mobile.

2 Likes

This didn’t work, I’m really struggling at fixing this, do you have any other ideas?

1 Like

Add this and it should work:

hum.StateChanged:Connect(function(old, new)
  if new == Enum.HumanoidStateType.Swimming then
    track:Stop()
end)

This checks whenever the character starts swimming.

1 Like

Want me to add it to the sprint script?

I don’t know if it didn’t work or if I did it wrong but I just added it to the end (which I think is what you meant) of the sprint script. Here’s what it looks like rn:

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()
	elseif hum.FloorMaterial == Enum.Material.Water then
		isInAir = false
		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)

hum.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.Swimming then
		track:Stop()
	end
end)
1 Like

Sorry, but I’m not sure what else to put. I don’t wanna mess up your script even more.

2 Likes

It’s alright, this is a really tough bug, I’ll try to figure something out. Thanks for helping me!

1 Like

No problem

This text will be blurred

2 Likes

Good news! I managed to fix it! Here is the improved swim script if you’re curious!

--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
            -- Stop sprinting if currently active
            if player.Character.Humanoid.WalkSpeed > 16 then
                player.Character.Humanoid.WalkSpeed = 16
            end
            
            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)
1 Like

Good job!

type or paste code here
1 Like

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