Why is this touch event acting weirdly?

alright so basically, i made a part or “zone” thats supposed to change the colour of the water, when you walk into it. It works, but for some reason whenever a tool is equipped, it will change the colour of the water despite me using FindFirstChild(). Any idea whats wrong with it?

code:

local terrain = game.Workspace.Terrain

local tweenservic = game:GetService("TweenService")
local info = TweenInfo.new(1, Enum.EasingStyle.Linear)
local red = {WaterColor = Color3.fromRGB(255, 0, 4)}
local blue = {WaterColor = Color3.fromRGB(12, 84, 92)}

local changered = tweenservic:Create(terrain, info, red)
local changeblue = tweenservic:Create(terrain, info, blue)

game.Workspace["Chagne water"].Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then 
		changered:Play()
	end
end)

game.Workspace["Chagne water"].TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		changeblue:Play()
	end
end)

2 Likes

Does it have to do with the mispelling?
And is CanTouch enabled?

2 Likes

try this instead, this is how i always check if a player hit and without the paret:FindFirstChild
if it works, i’d love if you solution this :slight_smile:

local terrain = game.Workspace.Terrain

local tweenservic = game:GetService("TweenService")
local info = TweenInfo.new(1, Enum.EasingStyle.Linear)
local red = {WaterColor = Color3.fromRGB(255, 0, 4)}
local blue = {WaterColor = Color3.fromRGB(12, 84, 92)}

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local changered = tweenservic:Create(terrain, info, red)
local changeblue = tweenservic:Create(terrain, info, blue)

game.Workspace["Chagne water"].Touched:Connect(function(hit)
	if hit:IsDescendantOf(Character) then 
		changered:Play()
	end
end)

game.Workspace["Chagne water"].TouchEnded:Connect(function(hit)
	if it:IsDescendantOf(Character) then
		changeblue:Play()
	end
end)

oh haha no its not a typo, i just didnt care enough to spell it correctly. and for cantouch, its turned off.

1 Like

I tried this out, and honestly there isnt much of a change.
Heres a video showing whats happening: https://youtu.be/GUFnlKf4rqA

It’s registering the touched event of the tool being added while you’re inside the box.

Try doing a check inside to check if the touched item is a Tool. If it is then don’t do changered:Play()

thats exactly what i did. i checked if the thing inside has a humanoid, otherwise it shouldnt work.

Where are you checking for a Humanoid?
I just see IsDescendandOf(Character).

Try:

game.Workspace["Chagne water"].Touched:Connect(function(hit)    
	if hit:IsDescendantOf(Character) and not hit.Parent.ClassName == "Tool" then 
		changered:Play()
	end
end)

Be aware I’m not exactly sure if that’s the correct terminology or heirarchy. If your tool has multiple Parts it may cause issues. If it’s just one Part named Handle then your could probably use hit.Name == "Handle"

EDIT Sorry, was looking at @capsori’s post.
Doing a double check to see if it’s a tool is probably still a good idea.

1 Like

I tried doing that, didnt really have any affect for some reason. Im gonna try this tomorrow again.

Uhh when you equip a tool its placed inside your character, meaning your code finds a humanoid in the parent of the tool and triggers the code

You need to check if hit is a tool or character part
You could do this by naming the tool, “tool” so you know its a tool, or whatever indicates that hit Is a tool

It would look something like this:


if hit.Name ~= “Tool” and  hit.Parent:FindFirstChild("Humanoid") then

I wonder if it’s a debounce issue.
Touched events, especially with Humanoids, can fire multiple times when a player touches it.

lol, @schaap5347, so basically what I said a couple posts up? :wink:

Lmao just trying to help didnt see your post

1 Like
local terrain = script.Parent
local tweenService = game:GetService("TweenService")
local info = TweenInfo.new(1)
local red = {WaterColor = Color3.fromRGB(255, 0, 4)}
local blue = {WaterColor = Color3.fromRGB(12, 84, 92)}

local changered = tweenService:Create(terrain, info, red)
local changeblue = tweenService:Create(terrain, info, blue)

local function handleTouched(hit)
    local character = hit.Parent:FindFirstChild("Humanoid")
    if character and not hit.Parent:FindFirstChild("Tool") then
        changered:Play()
    end
end

local function handleTouchEnded(hit)
    local character = hit.Parent:FindFirstChild("Humanoid")
    if character and not hit.Parent:FindFirstChild("Tool") then
        changeblue:Play()
    end
end

script.Parent["Chagne water"].Touched:Connect(handleTouched)
script.Parent["Chagne water"].TouchEnded:Connect(handleTouchEnded)

Or

local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid and humanoid.Parent == game.Players.LocalPlayer.Character then

You can also use IsA:(“Model”) and do something with the character from there that doesn’t affect tool

Are people forgetting that the Touched event will fire multiple times depending on the amount of parts that touch it? Simply check on both occasions if the part is the HumanoidRootPart, then it should only account for one hit. If this is a server script you want everyone to influence, simply use a method that checks for all touching parts.

1 Like

the issue is not that the event is firing multiple times or whatever, the issue is that whenever you equip a tool, for some reason it registers as the player leaving the zone. which i am trying to figure out how to fix.

also yes i did try checking if hit.parent:FindFirstChild(“HumanoidRootPart”), it works the same as using the humanoid only, but it does not solve the issue of “equipping a tool will for some reason count as leaving the zone”. And i also tried making an arguement where if hit.parent :findfirstchild(“tool”), then it shouldnt fire, but still.

heres a video to show what i mean: https://youtu.be/gjlCTpumb9c

edit heres the script aswell that i used in that video:


local tweenservic = game:GetService("TweenService")
local info = TweenInfo.new(1, Enum.EasingStyle.Linear)
local red = {WaterColor = Color3.fromRGB(255, 0, 4)}
local blue = {WaterColor = Color3.fromRGB(12, 84, 92)}

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local changered = tweenservic:Create(terrain, info, red)
local changeblue = tweenservic:Create(terrain, info, blue)


game.Workspace["Change water"].Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") and hit.Parent.Name ~= "Radio" then
		changered:Play()
	end
end)

game.Workspace["Change water"].TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") and hit.Parent.Name ~= "Radio"  then
		changeblue:Play()
	elseif hit.Parent:FindFirstChild("Handle") then
		print("rizz")
	end
end)

I tried this, its honestly smoother than find first child, but still doesnt solve the issue.

Ok try this, you will need to give the toolmodel

local ToolModel = MODELHERE


game.Workspace["Change water"].Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") and not hit:IsDescendantOf(ToolModel) then
		changered:Play()
elseif hit:IsDescrndantOf(ToolModel) then
print(“tool”)
	end
end)

1 Like

Tried it, but unfortunately now it only works if the player is holding the tool, which i mean technically solves the issue of the script thinking the player left the zone, but if the player isnt holding the tool, then an error will appear.