Collision problem

So im trying to make a water rising mechanic like in flood escape but the script just acts very weird, its either not working at all and it cant detect the player touching the water or it detects it from very far away ranges (not even close or touching the water)

local water = script.Parent -- Assuming the water is a child of this script's parent
local originalHeight = water.Position.y -- Store the original height of the water
local maxHeight = 170 -- Set the maximum height of the water
local riseTime = 20 -- Set the time it takes for the water to rise to its maximum height (in seconds)
local drownHeight = 5 -- Set the height at which the water can drown the player
local currentTween = nil -- Variable to hold the current water rise tween

-- Function to rise the water
local function RiseWater()
	-- Get the initial position of the water
	local initialPosition = water.Position

	-- Define the end size of the water
	local endSize = Vector3.new(water.Size.x, maxHeight, water.Size.z)

	-- Define the end position where the water will rise to (horizontal movement only)
	local endPosition = Vector3.new(initialPosition.x, initialPosition.y, initialPosition.z + water.Size.z/2) 

	-- Tween the water's size to the end size over the specified rise time
	local sizeTweenInfo = TweenInfo.new(riseTime, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	currentTween = game:GetService("TweenService"):Create(water, sizeTweenInfo, {Size = endSize})

	-- Play the size tween
	currentTween:Play()
end

-- Function to stop the water rise tween and lower the water back to its original height
local function StopWaterRise()
	if currentTween then
		currentTween:Cancel()
		local sizeTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
		local sizeTween = game:GetService("TweenService"):Create(water, sizeTweenInfo, {Size = Vector3.new(water.Size.x, 0, water.Size.z)})
		sizeTween:Play()
		wait(1) -- Adjust the time the water takes to lower to the original height
		water.Position = Vector3.new(water.Position.x, originalHeight, water.Position.z)

		-- Update collisions of other parts in the game
		for _, part in ipairs(game.Workspace:GetDescendants()) do
			if part:IsA("BasePart") then
				part:SetPrimaryPartCFrame(part.CFrame)
			end
		end
	end
end

-- Function to teleport the player to the spawn position
local function TeleportPlayerToSpawn(player)
	local spawn = game.Workspace:FindFirstChild("Gate6Spawn")
	if spawn then
		local spawnPosition = spawn.Position
		local character = player.Character
		if character then
			character:SetPrimaryPartCFrame(CFrame.new(spawnPosition))
		end
	end
end

-- Function to check if the player is drowned
local function CheckPlayerDrowned(player)
	local character = player.Character
	if character then
		local torso = character:FindFirstChild("HumanoidRootPart") or character.PrimaryPart
		if torso then
			local playerPosition = torso.Position
			-- Calculate the maximum height the water can reach
			local maxWaterHeight = originalHeight + maxHeight
			local ray = Ray.new(playerPosition, Vector3.new(0, -1, 0))
			local _, _, _, _, intersection = game.Workspace:FindPartOnRayWithIgnoreList(ray, {water})
			if intersection then
				local intersectionPosition = intersection.Position
				local isInWater = intersectionPosition.Y < maxWaterHeight and intersectionPosition.Y > originalHeight
				if isInWater then
					-- Player is below the water level, so they are in danger of drowning
					-- Teleport the player to the spawn position
					TeleportPlayerToSpawn(player)
				end
			end
		end
	end
end

-- Check the boolean value periodically
while true do
	if game.Workspace.Gate6.Bool.Value == true then
		RiseWater() -- Start rising the water if the boolean value is true
		-- Check if any players are drowned
		for _, player in ipairs(game.Players:GetPlayers()) do
			CheckPlayerDrowned(player)
		end
	else
		StopWaterRise() -- Stop the water rise tween if the boolean value is false
	end
	wait(1) -- Check the boolean value every second (adjust as needed)
end
2 Likes

Try this:

local water = script.Parent -- Assuming the water is a child of this script's parent
local originalHeight = water.Position.Y -- Store the original height of the water
local maxHeight = 170 -- Set the maximum height of the water
local riseTime = 20 -- Set the time it takes for the water to rise to its maximum height (in seconds)
local drownHeight = 5 -- Set the height at which the water can drown the player
local currentTween = nil -- Variable to hold the current water rise tween

-- Function to rise the water
local function RiseWater()
	-- Define the end size of the water
	local endSize = Vector3.new(water.Size.X, maxHeight, water.Size.Z)

	-- Tween the water's size to the end size over the specified rise time
	local sizeTweenInfo = TweenInfo.new(riseTime, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	currentTween = game:GetService("TweenService"):Create(water, sizeTweenInfo, {Size = endSize})

	-- Play the size tween
	currentTween:Play()
end

-- Function to stop the water rise tween and lower the water back to its original height
local function StopWaterRise()
	if currentTween then
		currentTween:Cancel()
		local sizeTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
		local sizeTween = game:GetService("TweenService"):Create(water, sizeTweenInfo, {Size = Vector3.new(water.Size.x, 0, water.Size.z)})
		sizeTween:Play()
		wait(1) -- Adjust the time the water takes to lower to the original height
		water.Position = Vector3.new(water.Position.X, originalHeight, water.Position.Z)
	end
end

-- Function to teleport the player to the spawn position
local function TeleportPlayerToSpawn(player)
	local spawn = game.Workspace:FindFirstChild("SpawnLocation")
	if spawn then
		local spawnPosition = spawn.Position
		local character = player.Character
		if character then
			character:SetPrimaryPartCFrame(CFrame.new(spawnPosition))
		end
	end
end

-- Function to check if the player is drowned
local function CheckPlayerDrowned(player)
	local character = player.Character
	if character then
		local torso = character:FindFirstChild("HumanoidRootPart") or character.PrimaryPart
		if torso then
			local waterTop = water.Position.Y + water.Size.Y / 2
			local torsoBottom = torso.Position.Y - torso.Size.Y / 2

			if torsoBottom < waterTop - drownHeight then
				-- Player's torso is submerged below drownHeight
				TeleportPlayerToSpawn(player)
			end
		end
	end
end

-- Listen for changes in the Gate6 Bool value
if game.Workspace.Gate6.Bool.Value then
	RiseWater()
else
	StopWaterRise()
end

-- Check if any players are drowned periodically
while true do
	for _, player in ipairs(game.Players:GetPlayers()) do
		CheckPlayerDrowned(player)
	end
	wait(0.1) -- Check every second (adjust as needed)
end

2 Likes

Thanks you very much it worked!

2 Likes