Greetings, developers! I have made a wonderful drink system, however, I need to check what tool is equipped in a player’s backpack amongst other possible tools named the same thing, and name the one that’s equipped. I have read other threads about this, nothing seems to work. Here is the script, do your magic!
local function onTriggered(Player)
if Player and Player.Character then
local Backpack = Player:WaitForChild("Backpack")
if Backpack:FindFirstChild("Cup") then
local Cup = Backpack:FindFirstChild("Cup")
if Cup and Cup:IsA("Tool") then
Cup.Name = "Cup w/ Ice"
end
end
end
end
script.Parent.Triggered:Connect(function(Player)
onTriggered(Player)
end)
The Backpack would be where the child was added, and I just noticed it was an event, which as I stated, could possibly fire way before the script is used.
If you want to check which tool is equipped, you would look inside the player’s character as that’s where equipped tools are stored. I don’t know what you mean by
So you’re suggesting I should sacrifice performance, and add a script, and a bool value to every tool, (possibly hundreds), just to check if a tool is equipped? I feel like there’s a better solution.
So my mistake, it did work, however, now it names ANY new child that’s added “Cup w/ Ice”. How would I combat that? Script:
local function onTriggered(Player)
if Player and Player.Character then
local Backpack = Player:WaitForChild("Backpack")
if Backpack:FindFirstChild("Cup") then
local Cup = Backpack:FindFirstChild("Cup")
local equippedTool
Player.Character.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
equippedTool = child
equippedTool.Name = "Cup w/ Ice"
end
end)
end
end
end
script.Parent.Triggered:Connect(function(Player)
onTriggered(Player)
end)
Tool.Equipped and a bool wouldn’t affect performance all that much actually. Especially doing it client side. I don’t know what information you’ve been reading, but unless your game has hundreds of tools it wouldn’t hurt your performance. 100’s of Tools meaning you physically have hundreds of tools not that every player accounts for 100 or more tools. Tool.Equipped is ran client side anyways there wouldn’t be performance issues server side.
local char = Player.Character
local equippedTool = char and char:FindFirstChildWhichIsA("Tool")
if equippedTool then
print(equippedTool)
else
print("No tool equipped!")
end