How can i fix this with the swimming module?

I got a script from youtube where you can swim in a desired part, i was able to a part thats swimmable in but with my other script where its a randomiser for maps to be chosen, whenever the desired swimmable part is loaded in the script stops working (the desired part is apart of a folder inside of serverstorage) but when i first put the desired swimmable part in workspace in the start of the server, thats only when it works but i want it to work when its loaded in, does anyone know why this is happening?
this is the map randomiser

local LobbyMinutes = 0
local LobbySeconds = 15

local GameMinutes = 4
local GameSeconds = 30

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coinEvent = game:GetService("ServerStorage"):FindFirstChild("CoinEvent")
local multEvent = game:GetService("ServerStorage"):FindFirstChild("MultEvent")
local deletecoinsevent = game:GetService("ServerStorage"):FindFirstChild("DeleteCoinsEvent")
local deletemultsevent = game:GetService("ServerStorage"):FindFirstChild("DeleteMultEvent")
local TimerValues = ReplicatedStorage:WaitForChild("TimerValues")
local gameTimer = TimerValues.GameTimer
local lobbyTimer = TimerValues.LobbyTimer
local finishline = script.Parent -- Change this
local placements = {}
local enableevent = ReplicatedStorage.Remotes.PEnableEvent
local disableevent = ReplicatedStorage.Remotes.PDisableEvent
local placeevent = game.ReplicatedStorage.Remotes.PlacementEvent
local Players = game:GetService("Players")

local TweenService = game:GetService("TweenService")
local FadeInfo = TweenInfo.new(1.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)

function teleportPlayers(RP,SpawnPart)
	if RP ~= nil then
		if SpawnPart ~= nil then
			RP.CFrame = SpawnPart.CFrame
		end
	end
end


game.ReplicatedStorage:FindFirstChild("Remotes"):FindFirstChild("GetPoints").OnServerEvent:Connect(function(Player,Point)
	local PlayerPoint = Player:FindFirstChild("Points")
	if PlayerPoint ~= nil then
		PlayerPoint.Value = Point
	end
end)


while true do
	lobbyTimer.Minutes.Value = LobbyMinutes
	lobbyTimer.Seconds.Value = LobbySeconds

	repeat wait(1)
		if lobbyTimer.Seconds.Value > 0 then
			lobbyTimer.Seconds.Value -= 1
		else
			if lobbyTimer.Minutes.Value > 0 then
				lobbyTimer.Minutes.Value -= 1
				lobbyTimer.Seconds.Value = 59
			end
		end
	until lobbyTimer.Minutes.Value == 0 and lobbyTimer.Seconds.Value == 0
	
	local MapstoCoins = {
		["SnowMap1"] = "CoinSpawnSnow",
		["DesertMap1"] = "CoinSpawnDesert",
		["BeachMap1"] = "CoinSpawnBeach"
	}
	local MapstoLobby = {
		["SnowMap1"] = "SnowLobby",
		["DesertMap1"] = "DesertLobby",
		["BeachMap1"] = "BeachLobby"
	}
	local MapstoMult = {
		["DesertMap1"] = "MultSpawnDesert",
		["SnowMap1"] = "MultSpawnSnow",
		["BeachMap1"] = "MultSpawnBeach"
	}

	local mapHolder = game:GetService("ServerStorage").MapHolder
	local coinsHolder = game:GetService("ServerStorage").CoinsHolder
	local lobbyHolder = game:GetService("ServerStorage").LobbyHolder
	local multHolder = game:GetService("ServerStorage").MultHolder
	local Children = mapHolder:GetChildren()
	local CoinChildren = coinsHolder:GetChildren()
	local LobChildren = lobbyHolder:GetChildren()
	local MultChildren = multHolder:GetChildren()
	local Index = math.random(1,#Children)
	local randomInt = Children[Index]

	local finalMap = mapHolder:FindFirstChild(randomInt.Name):Clone()  
	for _,coinmaps in pairs(CoinChildren) do
		if coinmaps.Name == MapstoCoins[randomInt.Name] then
			local finalcoinMap = coinmaps:Clone()
			for _,lobmaps in pairs(LobChildren) do
				if lobmaps.Name == MapstoLobby[randomInt.Name] then
					local finalobby = lobmaps:Clone()
					for _,multmap in pairs(MultChildren) do
						if multmap.Name == MapstoMult[randomInt.Name] then
							local finalmultMap = multmap:Clone()
	finalcoinMap.Parent = workspace.ChosenCoinMap
	finalmultMap.Parent = workspace.ChosenMultMap
	finalobby.Parent = workspace
	coinEvent:Fire(coinmaps)
	multEvent:Fire(multmap)
	print("coineventfired")
	finalMap.Parent = workspace
	

	for _, LocalPlayer in pairs(game:GetService("Players"):GetChildren()) do
		if LocalPlayer.Character then
			if LocalPlayer:FindFirstChild('PlayerGui') and LocalPlayer.Character:FindFirstChild('HumanoidRootPart') then
				local GUI = Instance.new("ScreenGui")
				GUI.Name = "Fade"
				GUI.Parent = LocalPlayer.PlayerGui
				GUI.ResetOnSpawn = false
				GUI.IgnoreGuiInset = true
				GUI.Enabled = true

				local NewValue = Instance.new("BoolValue")
				NewValue.Name = "Ingame"
				NewValue.Parent = LocalPlayer.Character
				NewValue.Value = false

				local Frame = Instance.new("Frame")
				Frame.Parent = GUI 
				Frame.BackgroundTransparency = 1
				Frame.BackgroundColor3 = Color3.fromRGB(0,0,0)
				Frame.Visible = true
				Frame.Size = UDim2.new(1,0,1,0)
				Frame.Position = UDim2.new(0,0,0,0)
				-- goes to riderhandler
		enableevent:FireClient(LocalPlayer)
				

				local TweenIn = TweenService:Create(Frame,FadeInfo,{BackgroundTransparency = 0})
				TweenIn:Play()

				TweenIn.Completed:Connect(function()
					teleportPlayers(LocalPlayer.Character.HumanoidRootPart,finalMap:FindFirstChild('SpawnPart'))
					wait(2)
					local TweenOut = TweenService:Create(Frame,FadeInfo,{BackgroundTransparency = 1})
					TweenOut:Play()

					TweenOut.Completed:Connect(function()
						GUI:Destroy()
					end)
				end)
			end
		end
	end

	wait(1.6 + 2)

	gameTimer.Minutes.Value = GameMinutes
	gameTimer.Seconds.Value = GameSeconds

	repeat wait(1)
		if gameTimer.Seconds.Value > 0 then
			gameTimer.Seconds.Value -= 1
		else
			if gameTimer.Minutes.Value > 0 then
				gameTimer.Minutes.Value -= 1
				gameTimer.Seconds.Value = 59
			end
		end
	until gameTimer.Minutes.Value == 0 and gameTimer.Seconds.Value == 0

	for _, LocalPlayer in pairs(game:GetService("Players"):GetChildren()) do
		if LocalPlayer.Character then
			if LocalPlayer:FindFirstChild('PlayerGui') and LocalPlayer.Character:FindFirstChild('Ingame') then
				local GUI = Instance.new("ScreenGui")
				GUI.Name = "Fade"
				GUI.Parent = LocalPlayer.PlayerGui
				GUI.ResetOnSpawn = false
				GUI.IgnoreGuiInset = true
				GUI.Enabled = true

				local Frame = Instance.new("Frame")
				Frame.Parent = GUI 
				Frame.BackgroundTransparency = 1
				Frame.BackgroundColor3 = Color3.fromRGB(0,0,0)
				Frame.Visible = true
				Frame.Size = UDim2.new(1,0,1,0)
				Frame.Position = UDim2.new(0,0,0,0)

				local TweenIn = TweenService:Create(Frame,FadeInfo,{BackgroundTransparency = 0})
				TweenIn:Play()

				TweenIn.Completed:Connect(function()
					LocalPlayer:LoadCharacter()
					teleportPlayers(LocalPlayer.Character.HumanoidRootPart,game.Workspace.GameEndSpawn)
					wait(2)
					local TweenOut = TweenService:Create(Frame,FadeInfo,{BackgroundTransparency = 1})
					TweenOut:Play()

					TweenOut.Completed:Connect(function()
						GUI:Destroy()
												
					
					end)
				end)
			end
		end
	end
	
	
	game.ReplicatedStorage:FindFirstChild("Remotes"):FindFirstChild("GetPoints"):FireAllClients()
	
	task.wait(2.5)
	
	local placements = {}
	for i,plr in pairs(game.Players:GetPlayers()) do
		table.insert(placements,{['Player'] = plr,['Points'] = plr.Points.Value})
	end
	
	table.sort(placements,function(a,b)
		return a.Points > b.Points
	end)
							placeevent:FireAllClients({placements[1],placements[2],placements[3],placements[4],placements[5],placements[6],placements[7],placements[8],placements[9],placements[10],placements[11],placements[12],placements[13],placements[14],placements[15],placements[16],placements[17],placements[18],placements[19],placements[20]})
	
	for _,GetPl in pairs(Players:GetPlayers()) do
		--goes to riderhandler
		disableevent:FireClient(GetPl)
		GetPl.Points.Value = 0
		GetPl.PointMultiply.Value = 0
	end
	wait(1.6)
	deletecoinsevent:Fire()
	deletemultsevent:Fire()
	finalMap:Destroy()
	finalcoinMap:Destroy()
	finalobby:Destroy()
	finalmultMap:Destroy()
	wait(3)
						end
						end
		end
	end
		end
	end
	end

this is the water swimming scripts

this is the desired swimmable parts (its in a folder called water and the parts are each called swim)

this is the high perfomance water script, the main problem im having is the zone local variable, which decides what the desired swimmable parts are

--[[
Script made by gpm231 on 2022

--For info about this check the devforum post --> https://devforum.roblox.com/t/high-performance-water-swimmable-part-20/2038483

--Contact:
-Roblox: gpm231
-Twitter: @gpm231RBLX
-Discord: gpm231#9505
--]]

if not game:IsLoaded() then --wait for assets
	game.Loaded:Wait()
end

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

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 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 --tap event connection

local debounce = false --for mobile devices to allow jumping out of water

local function folderChanged() --if you want to update the water parts this tracks the change
	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()
	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)

	--Getting out of water
	local function gOut()
		if not isTouchScreen then
			swimModule:GetOut()
			swimModule:Stop()
		else
			for i = 1, 20 do --weird stuff mobile needs
				swimModule:GetOut()
				swimModule:Stop()
			end

			debounce = true
			task.wait(0.25) --weird stuff mobile needs
			debounce = false
		end
	end
	if table.find(getKeycodesPressed(), Enum.KeyCode.Space or table.find(getKeycodesPressed(), Enum.KeyCode.ButtonA)) and not isHeadIn and isLowerIn then
		gOut()
	end

	--Exception handlers
	if isUpperIn and isLowerIn then
		swimModule:Start()
		if gotOut then --for when the player goes back into the water after jumping out
			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

	--Going up forces
	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) --force that make you go up by pressing space
		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) --force that makes you go up by being idle
				end
			else
				force.Force = Vector3.new(0, defaultForce, 0) --force that makes you stay still when floating in the surface
			end
		end
	end

	--Mobile tap detection
	tapCon = game:GetService("UserInputService").TouchTapInWorld:Connect(function()
		if not swimModule.Enabled or isHeadIn or not isLowerIn then return end
		gOut()
	end)

	--Underwater lighting
	if isCameraIn then
		lightingModule:Add()
	else
		lightingModule:Remove()
	end
end)

--Player died handler
local function died()
	lightingModule:Remove()
	con:Disconnect()
end
local humCon = hum.Died:Connect(died)

it does seem a bit long and confusing, so please tell me if i need to add more information :pray:

1 Like

Make sure the script is disabled before you load the part.
Considering what you said, I think this might be the solution.
And disable and re-enable the script every time you reload it

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