I have this water system in my game for players to swim in parts with a water tag, but if you swim while holding spacebar, you fling up. And I have gotten the code from somewhere else, so I don’t know what or where the issue is even after reading.
--LocalScript
spawn(function()
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local userInputService = game:GetService("UserInputService")
local isTouchScreen = userInputService.TouchEnabled
local swimModule = require(script:WaitForChild("Libraries"):WaitForChild("SwimModule"))
local zonePlus = require(script:WaitForChild("Libraries"):WaitForChild("Zone"))
local lightingModule = require(script:WaitForChild("Libraries"):WaitForChild("UnderwaterLighting"))
repeat task.wait() until char:FindFirstChildOfClass("Humanoid")
local hum = char:FindFirstChildOfClass("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
local tag = script.CollectionServiceTag.Value
local folder = script.PartFolder.Value
local container = folder or game:GetService("CollectionService"):GetTagged(tag)
if typeof(container) == "table" then if #container < 1 then container = nil end end
if not container then error('Please set a folder that contains the parts for the player to swim in OR tag your parts using collection service') end
local zone = zonePlus.new(container)
local gotOut = false
local defaultForce = hrp.AssemblyMass * workspace.Gravity
local tapCon
local debounce = false
local jumpdb =false
local function folderChanged()
zone = zonePlus.new(container)
end
if script.PartFolder.Value then
container.ChildAdded:Connect(folderChanged)
container.ChildRemoved:Connect(folderChanged)
else
game:GetService("CollectionService"):GetInstanceAddedSignal(script.CollectionServiceTag.Value):Connect(folderChanged)
game:GetService("CollectionService"):GetInstanceRemovedSignal(script.CollectionServiceTag.Value):Connect(folderChanged)
end
local function getKeycodesPressed()
local data = {}
local a = userInputService:GetKeysPressed()
for _, obj in a do
table.insert(data, obj.KeyCode)
end
return data
end
local con = game:GetService("RunService").Heartbeat:Connect(function()
hrp.AssemblyLinearVelocity = hrp.AssemblyLinearVelocity.Unit * math.min(hrp.AssemblyLinearVelocity.Magnitude,100)
if debounce then return end
if tapCon then
tapCon:Disconnect()
tapCon = nil
end
local UpperDetectorPos = hrp.CFrame:ToWorldSpace(CFrame.new(0, 1, -0.75))
local LowerDetectorPos = hrp.CFrame:ToWorldSpace(CFrame.new(0, -2.572, -0.75))
local HeadDetectorPos = hrp.CFrame:ToWorldSpace(CFrame.new(0, 1.322, -0.75))
local isUpperIn = zone:findPoint(UpperDetectorPos)
local isLowerIn = zone:findPoint(LowerDetectorPos)
local isHeadIn = zone:findPoint(HeadDetectorPos)
local isCameraIn = zone:findPoint(workspace.CurrentCamera.CFrame.Position)
local function gOut()
if not isTouchScreen then
swimModule:GetOut()
swimModule:Stop()
else
for i = 1, 20 do
swimModule:GetOut()
swimModule:Stop()
end
debounce = true
task.wait(0.25)
debounce = false
end
end
if isUpperIn and isLowerIn then
swimModule:Start()
if gotOut then
swimModule:CreateAntiGrav()
gotOut = false
end
elseif not isUpperIn and not isLowerIn then
swimModule:Stop()
elseif not isUpperIn and isLowerIn then
swimModule:ClearAntiGrav()
gotOut = true
end
if table.find(getKeycodesPressed(), Enum.KeyCode.Space) or table.find(getKeycodesPressed(), Enum.KeyCode.ButtonA) then
local force = hrp:FindFirstChildOfClass("VectorForce")
if force then
force.Force = Vector3.new(0, defaultForce * script:WaitForChild("Configuration"):GetAttribute("CharDensityUp") , 0)
end
else
local force = hrp:FindFirstChildOfClass("VectorForce")
if force then
if hum.MoveDirection.Magnitude == 0 then
if isHeadIn then
force.Force = Vector3.new(0, defaultForce * script:WaitForChild("Configuration"):GetAttribute("CharDensity"), 0)
end
else
force.Force = Vector3.new(0, defaultForce -15, 0)
end
end
end
tapCon = game:GetService("UserInputService").TouchTapInWorld:Connect(function()
if not swimModule.Enabled or isHeadIn or not isLowerIn then return end
gOut()
end)
local function isInsideBrick(position, brick)
local v3 = brick.CFrame:PointToObjectSpace(position)
local size = brick.Size / 2
return (math.abs(v3.X) <= size.X)
and (math.abs(v3.Y) <= size.Y)
and (math.abs(v3.Z) <= size.Z)
end
if isCameraIn then
for i,v in pairs(zone.zoneParts) do
if isInsideBrick(workspace.CurrentCamera.CFrame.Position,v) then
lightingModule:Add(v)
end
end
else
lightingModule:Remove()
end
end)
local function died()
lightingModule:Remove()
con:Disconnect()
end
local humCon = hum.Died:Connect(died)
end)
--SwimModule
local runService = game:GetService("RunService")
local swimModule = {}; swimModule.__index = swimModule
if runService:IsServer() then return end
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
repeat task.wait() until char:FindFirstChildOfClass("Humanoid")
local hum = char:FindFirstChildOfClass("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
local con
local function humStates(activate, toSet)
hum:SetStateEnabled(Enum.HumanoidStateType.Running, activate)
hum:SetStateEnabled(Enum.HumanoidStateType.RunningNoPhysics, activate)
hum:SetStateEnabled(Enum.HumanoidStateType.GettingUp, activate)
hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, activate)
hum:SetStateEnabled(Enum.HumanoidStateType.Freefall, activate)
hum:SetStateEnabled(Enum.HumanoidStateType.FallingDown, activate)
hum:ChangeState(toSet)
end
local atts = {}; local forces = {}
local function antiGrav(on)
if on then
if #atts > 0 or #forces > 0 then return end
local mass = hrp.AssemblyMass
local att = Instance.new("Attachment")
att.WorldPosition = hrp.Position
att.Parent = hrp
table.insert(atts, att)
local force = Instance.new("VectorForce")
force.RelativeTo = Enum.ActuatorRelativeTo.World
force.Force = Vector3.new(0, 0 , 0)
force.Attachment0 = att
force.ApplyAtCenterOfMass = true
force.Parent = hrp
table.insert(forces, force)
game:GetService("Debris"):AddItem(force,1)
return att, force
else
for _, v in atts do
v:Destroy()
end
for _, v in forces do
v:Destroy()
end
table.clear(atts)
table.clear(forces)
end
end
function swimModule:Start()
if runService:IsServer() then return end
if self.Enabled then return end
humStates(false, Enum.HumanoidStateType.Swimming)
local attachment, force = antiGrav(true)
self.Enabled = true
self.RSConnection = runService.Heartbeat:Connect(function()
if hum.MoveDirection.Magnitude > 0 then return end
hrp.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
end)
end
function swimModule:Stop()
if runService:IsServer() then return end
if not self.Enabled then return end
self.Enabled = false
humStates(true, Enum.HumanoidStateType.Freefall)
antiGrav(false)
if self.RSConnection then
self.RSConnection:Disconnect()
end
end
function swimModule:ClearAntiGrav()
antiGrav(false)
end
function swimModule:CreateAntiGrav()
task.delay(0.05, function()
hrp.AssemblyLinearVelocity = Vector3.new()
end)
antiGrav(true)
end
function swimModule:GetOut()
humStates(true, Enum.HumanoidStateType.Jumping)
end
function swimModule:ActivateStates()
humStates(false, Enum.HumanoidStateType.Swimming)
end
return setmetatable({Enabled = false}, swimModule)
--UnderwaterLighting
local module = {}
local lighting = game:GetService("Lighting")
local atmos = script:WaitForChild("Atmosphere")
local sky = script:WaitForChild("Sky")
local previousObjects = {}
local enbled = false
function module:Add(part)
--print(part,part.ClassName)
if enabled then return end
enabled = true
for _, v in lighting:GetChildren() do
v.Parent = script
table.insert(previousObjects, v)
end
local color1 = part.Color
local color2 = Color3.fromRGB(159, 159, 159)
local color3 = Color3.new(color1.R+color2.R, color1.G+color2.G, color1.B+color2.B) --print(color3)
atmos.Color=color3
atmos.Parent = lighting
sky.Parent = lighting
end
function module:Remove()
if not enabled then return end
enabled = false
for _, v in script:GetChildren() do
v.Parent = lighting
end
atmos.Parent = script
sky.Parent = script
end
return module