How can I check if the mouse is touching Water, not other terrain

Hello scripters :> I’m trying to make an ability where the player summons a ball of water when their mouse is hovering over Terrain water. I know most of what I need to do for the script but I don’t know how to check if the mouse is touching water and not other types of terrain.

here’s what I have so far:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character

local waterBall = RS.waterBall:Clone()

local target = mouse.Target

UIS.InputBegan:Connect(function(input,gameprocessed)
	if gameprocessed then return end
	
	if input.KeyCode == Enum.KeyCode.Q then
		if mouse.Target == workspace.Terrain then
		end
	end
end)
2 Likes

Mouse.Target.Name?

never tried it before so yeah might not work.

I tried that but roblox classifies all types of terrain and just Terrain plus I don’t think mouse.Target.Name is a thing

1 Like

Definitely would not work since Water is part of the Terrain instance.

As for the OP, I believe this is what you are looking for.

2 Likes
local underwater = game.Workspace.underwater
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

if mouse.Target = underwater then

end)

`local underwater = game.Workspace.underwater
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

if mouse.Target = underwater then

end)

Try to make a part into water and name it water
So u can workspace.Water

Try to make a non collide-able part and make it invisible and stretch it to the length and size of the water, and add it into the script.

1 Like

You can send a raycast from the player’s character towards the position of the mouse. Here is an example:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Whitelist
rayParams.FilterDescendantsInstances = {workspace.Terrain}
rayParams.IgnoreWater = false

local plr = Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()

UserInputService.InputBegan:Connect(function(i,isTyping)
	if isTyping then return end
	
	if i.KeyCode == Enum.KeyCode.Q then
		local raycast = workspace:Raycast(character.HumanoidRootPart.Position, (mouse.Hit.p - character.HumanoidRootPart.Position).Unit * 1000,rayParams)
		
		if raycast and raycast.Material == Enum.Material.Water then
			print("Mouse hit water!")
		end
	end
end)
3 Likes

U made it more clear tnx and also dont forget to name ur part so u can workspace.Water :slight_smile: :wink:

1 Like

This worked wonders, thanks so much!

but seriously, you saved me lol

2 Likes