How would I detect if a player is stuck with his car in terrain water?

Hello,

I am working on a offroad car simulation game, I got everything working fine.
I changed the properties of the water so it looks like mud, but I cant get the detection to work.

I want it so if a player is stuck for 5 seconds in water there should popup a text on th player’s screen. But it does not work rn.

Current Code:

local timer = 0
local deb = false

local Part = script.Parent
Part.Touched:Connect(function() end)

function CheckIfPlayerIsInArea(Part, Character)
	local touching = Part:GetTouchingParts()
	for i = 1,#touching do
		if touching[i] == Character.HumanoidRootPart then
			return true
		end
	end
	return false
end

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		repeat
			if CheckIfPlayerIsInArea(Part, hit.Parent) == true then
				timer = timer + 1
			else
				if timer <= 0 then
					timer = 0
				else
					timer = timer - 1
				end
			end
			wait(1)
		until timer >= 3
		if timer >= 3 then
			-- Player is stuck
			print("Player is stuck")
		end
	end
end)

Thanks in advance.

1 Like

ummm I think that the problem is that water isnt a part. Maybe u should look up into regions3

Perhaps you could shoot a raycast under the car and check if the material is Enum.Material.Water. Make sure you set “ignore water” to false when casting the ray though.

Here is a tutorial on RayCasting: Documentation - Roblox Creator Hub (Note: This uses the old raycastig system.)

Once you understand how raycasting works, you could use the new Raycast function of Workspace, which you can find here: WorldRoot | Documentation - Roblox Creator Hub

(The only major difference is that it returns the results in a table rather than a tuple, and takes ignoreWater as a value in a table rather than just a parameter)

3 Likes

I put a invisible part on the water.