Hello fellow devs!
I am developing special vacuum cleaners for CO2 as tools right now.
I want to show a tool GUI (screen GUI) displaying the tool name, the current load and the max load only when the tool is equipped.
When I tested, there are no error messages, but the GUI is never shown.
Below is the code:
--Services--
local StarterGui = game:GetService("StarterGui")
-- Models and parts--
local tool = script.Parent
local head = tool.Head
local cone = tool.Cone
local player = game:GetService("Players"):WaitForChild("LocalPlayer")
-- Values --
local maxLoad = tool:WaitForChild("MaxLoad").Value
-- GUI --
local toolGui = StarterGui:WaitForChild("ToolGui")
local loadFrame = toolGui:WaitForChild("LoadFrame")
local toolNameLabel = loadFrame.ToolName
local currentLoadLabel = loadFrame.CurrentLoadLabel
local maxLoadLabel = loadFrame.MaxLoadLabel
-- Sounds --
local vacuumCleanerSound = tool:WaitForChild("VacuumCleanerSound")
-- Constance --
local SUCKINGPOWER = 10000
local currentLoad = 0
-- Tool Events --
tool.Equipped:Connect(function()
toolGui.Enabled = true --Display Tool GUI
vacuumCleanerSound:Play()
print("Tool Equipped")
toolNameLabel.Text = tool.Name
maxLoadLabel.Text = maxLoad
local ap_a1 = Instance.new("Attachment", head)
while wait(.1) do
local aoe = workspace:GetPartsInPart(cone)
for _, a in pairs(aoe) do
if a.Parent and a.Parent.Name == "CO2" then
local CO2 = a.Parent
local ap_a0 = Instance.new("Attachment", CO2.Carbon)
local alignPosition = Instance.new("AlignPosition", CO2.Carbon)
alignPosition.Mode = "TwoAttachment"
alignPosition.Attachment0 = ap_a0
alignPosition.Attachment1 = ap_a1
alignPosition.MaxForce = SUCKINGPOWER
print("Found a CO2")
end
end
end
end)
tool.Unequipped:Connect(function()
toolGui.Enabled = false -- Hide Tool GUI
vacuumCleanerSound:Stop()
end)
head.Touched:Connect(function(hit)
if hit.Parent and hit.Parent.Name == "CO2" and currentLoad < maxLoad then
hit.Parent:Destroy()
currentLoad +=1
currentLoadLabel.Text = tostring(currentLoad)
end
end)
If you want, pls check the folder structures on the right side below:
Now I am at a loss what to do next.
So please help me!