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)
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.
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)