Touch function isn't detecting what I'm looking for

The Context:
I’m making a bakery game, and one of the most basic things you should have about a bakery game is to have a functional drink system.

The issue:
I’m having an issue with a script. It detects when it’s being touched, but I am unaware on how to find a tool in a player’s hand/character.

What I think is wrong with the issue:
It’s detecting the Torso, not the drink, for some reason.

The script:

local InputDrink = script.Parent.RequiredDrink.Value
local OutputDrink = script.Parent.OutputDrink.Value

function tch(h)
	print("Touched!")
	if (h.Parent.Name == InputDrink) then
		h.Parent.Name = OutputDrink
		script.DP:Play()
	end
end

script.Parent.Parent.Parent.Functionality.Touch.Touched:connect(tch)
2 Likes

Can you explain to us how it works?

2 Likes

You touch the drink with the cup in your hand, and it will rename it to something else as long as it’s named a value I set it as.

It’s a StringValue, both of them.

1 Like

Perhaps the CanTouch property of the tool is set to false?

1 Like

Did this print statement gets fired?

Yes, the statement was fired, I’ve also tried to do print(h).

The CanTouch prperty of the tool is set to true.

Then it’s most likely your if statement evaluated soemthing to failed.

I’d get the character from the touched base part:

local InputDrink = script.Parent.RequiredDrink.Value
local OutputDrink = script.Parent.OutputDrink.Value

function tch(h)
	print("Touched!")
	if (h.Parent.Name == InputDrink) then
		h.Parent.Name = OutputDrink
		script.DP:Play()
	else
		local character = h.Parent
		local tool = character:FindFirstChildOfClass("Tool")
		if tool and (tool.Name == InputDrink) then
			h.Parent.Name = OutputDrink
			script.DP:Play()
		end
	end
end

script.Parent.Parent.Parent.Functionality.Touch.Touched:connect(tch)

The code above checks if the hit part is the handle of the correct tool, if it isn’t it checks if the hit part is part of a character holding the correct tool.

It did not work, could it be that I am dragging the tool from ReplicatedStorage to my character in game?

1 Like

I seem to see the problem then, you are creating variables for the string value

local InputDrink = script.Parent.RequiredDrink.Value
local OutputDrink = script.Parent.OutputDrink.Value

You aren’t actually getting the Value property and the string within it, you are getting the string right at the start
Simply put .Value later

local InputDrink = script.Parent.RequiredDrink
local OutputDrink = script.Parent.OutputDrink

function tch(h)
	print("Touched!")
	if (h.Parent.Name == InputDrink.Value) then
		h.Parent.Name = OutputDrink.Value
		script.DP:Play()
	else
		local character = h.Parent
		local tool = character:FindFirstChildOfClass("Tool")
		if tool and (tool.Name == InputDrink.Value) then
			h.Parent.Name = OutputDrink.Value
			script.DP:Play()
		end
	end
end

script.Parent.Parent.Parent.Functionality.Touch.Touched:connect(tch)
2 Likes