Swim System Troubles

I have a swim script, its separated into a lot of different stuff so if you need more just tell me but here’s the main swim script. I just need someone to add something to the script where the players walk speed stays at 16 the entire time you’re in the water until you leave. The reason for this is because I have a sprint script and I do not want players to be activate it in water which you currently can. Thank you for helping me!

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)

Remember, if you need anymore information just ask! And again, thank you for your help!

2 Likes

Hello!

If you want the player to be unable to run while they are in water, you can apply a value to the player that states they are in water, and use an if statement to check if they are allowed to sprint or not (whether or not they’re in water).

The way I would do this is by having a table, when a player joins, they are added as an index. This index has a true or false value tied to it, (whether they are swimming or not).

Then you can use this T/F value to block the sprint key on the server side if you want. Also this is just off the top of my head so you might be able to find a better solution fairly quickly.

1 Like

can you give me an example in my script?

1 Like

Unfortunately I cannot because I am very tired right now and I believe I am not supposed to make scripts for people asking for help on the forum.

I will put this in a more organized way though so its easier to interpret and integrate.

Firstly, define the table:

local playerSwimming = {}

then you need to define the player you are talking about when they load in. This means in the player added function, you need to add an index that is the player object.

You are using player character added though, and that works but you could also use player.PlayerAdded:Connect(function() end). I think thats what it is at least.

playerSwimming[player] = false --this means this player is not swimming

Then, if you want to make sure they dont sprint while in water, you need to basically use this boolean to say whether the run input is allowed.

Basically just wrap the run logic in an if statement that checks to make sure swimming is false before you run.

Hopefully this helps!

2 Likes

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