Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local WaterDict = game.Workspace.WaterFolder:GetChildren()
- local Torso = script.Parent:WaitForChild("Torso")
- local ShouldUpdateMass = true
- local Mass = 0
- local WorldGravity = 196.2
- local WaterGravity = WorldGravity / 4
- local GravityForce = Instance.new("BodyForce", Torso)
- local UnderWaterEffect = game:GetService("Lighting"):WaitForChild("UnderWaterEffect")
- local UnderWaterSwampEffect = game:GetService("Lighting"):WaitForChild("UnderWaterSwampEffect")
- local UnderLavaEffect = game:GetService("Lighting"):WaitForChild("UnderLavaEffect")
- local UnderWaterSoundEffects = {}
- for _, Effect in pairs(game:GetService("SoundService"):WaitForChild("Music"):GetChildren()) do
- if Effect.Name == "UnderWaterEffect" then
- table.insert(UnderWaterSoundEffects, Effect)
- end
- end
- local Camera = game:GetService("Workspace").CurrentCamera
- function GetCharacterMass()
- local CharacterMass = 0
- for _, Inst in pairs(script.Parent:GetDescendants()) do
- if Inst:IsA("BasePart") then
- CharacterMass = CharacterMass + Inst:GetMass()
- end
- end
- return CharacterMass
- end
- local thresholds = {}
- for _, i in pairs(WaterDict) do
- local pos = i.Position
- local size = i.Size
- table.insert(thresholds, {[i] = {
- pos.X-(size.X/2), pos.X+(size.X/2),
- pos.Y-(size.Y/2), pos.Y+(size.Y/2),
- pos.Z-(size.Z/2), pos.Z+(size.Z/2),
- i.Biome.Value
- }})
- -- store these precalculated borders for every part
- -- [part] = {BOTX, TOPX, BOTY, TOPY, BOTZ, TOPZ, BIOME}
- -- this is identical to the old script except these aren't recalculated every frame lol
- end
- function IsUnderWater(X, Y, Z)
- if Y <= 17.5 and Y >= -43 then return true, "Normal" end
- for n, data in pairs(thresholds) do -- n = NUM, data = TABLE
- for p, i in pairs(data) do -- p = PART, i = TABLE (BOTX, TOPX etc)
- if X>i[1] and X<i[2] and Y>i[3] and Y<i[4] and Z>i[5] and Z<i[6] then
- return true, i[7]
- end
- end
- end
- return false, "None"
- end
- game:GetService("RunService").Heartbeat:Connect(function()
- if ShouldUpdateMass then Mass = GetCharacterMass() end
- local UnderWater, WaterType = IsUnderWater(Torso.Position.X, Torso.Position.Y, Torso.Position.Z)
- GravityForce.Force = Vector3.new(0, UnderWater and ((Mass * WorldGravity) - (Mass * WaterGravity)) or 0, 0)
- for _, Effect in pairs(UnderWaterSoundEffects) do
- Effect.Enabled = UnderWater
- end
- end)
- game:GetService("RunService").RenderStepped:Connect(function()
- local UnderWater, WaterType = IsUnderWater(Camera.CFrame.X, Camera.CFrame.Y, Camera.CFrame.Z)
- UnderLavaEffect.Enabled = UnderWater and WaterType == "Lava"
- UnderWaterEffect.Enabled = UnderWater and WaterType == "Normal"
- UnderWaterSwampEffect.Enabled = UnderWater and WaterType == "Swamp"
- end)
- script.Parent.DescendantAdded:Connect(function() ShouldUpdateMass = true end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement