I need help with a script

Hi, so im making a script in a part that detects the terrain material thing “water”
And i have no i dea how to do so, i searched up but nothing was relevant for me, if you would like to help, feel free this is the code btw

local part = script.Parent
local damageAmount = 10 

local function shootRay()
	local position = part.Position
	local forwardVector = part.CFrame.LookVector

	local ray = Ray.new(position, forwardVector * 2)

	local hit, hitPosition, hitNormal = workspace:FindPartOnRay(ray)

	if hit then
		print("Hit something at:", hitPosition)

		local terrain = workspace.Terrain
		

		if Idk what do define here do something here == Enum.Material.Water then -- here how the hell
			local character = game.Players:GetPlayerFromCharacter(hit.Parent)
			if character then
				local humanoid = character.Character:FindFirstChildOfClass("Humanoid")
				if humanoid then
					humanoid:TakeDamage(damageAmount)
					print("Player took damage in water!")
				end
			end
		end

	else
		print("Ray did not hit anything.")
	end
end

while wait(2) do
	shootRay()
end

so at line 18 is the place where idk anymore, im casting a ray 2 studs from the look vector of the part and want to detect water but how?

this water btw


that yellow thing is the part, the script is parented into the part

The fourth value returned by :FindPartOnRay() is the material, so try:

local hit, hitPosition, hitNormal, Material = workspace:FindPartOnRay(ray)
print(Material)

That did not work, it still printed nothing it prints enum.Material air

Look into this and add raycastparams and add
raycastParams.IgnoreWater = false
and try again.

I dont want it to ignore water, i want it to detect water and then run something
My original question was on how to achieve that

Set it to false, sorry I copied it and forgot to change it I feel it’s set to true unless you change it .

I’m not entirely familiar with raycasting but I would recommend to cast 2 rays. 1 ray could have its parameters set to IgnoreWater = true and the other to IgnoreWater = false and if the position which the ray casts hit is the same position then there is no water but if the positions they hit are different then it didn’t hit water.

I could also just put a invisible part and call it something

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