Hello developers,
Today I wanted to create a survival type game and I managed to make a script that when you hit trees with a Axe tool, the trees change their transparency until they become invisible and after a few seconds they return to normal. My problem is this: I want to make a ScreenGui with a TextLabel, and at each hit with the tool I want the number of materials (wood) to appear in the TextLabel.
Could you please help me? Thank you.
This is the local Script that I placed it in the Axe tool.
local tool = script.Parent
local maxTransparency = 1.0
local waitTime = 1.0 -- Adjust this value to set the wait time in seconds
local resetTime = 10.0 -- Adjust this value to set the reset time in seconds
local canHit = true
local woodCount = 0 -- Variable to keep track of the wood count
-- Find or create the TextLabel in PlayerGui
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local woodCountLabel = playerGui:FindFirstChild("WoodCountLabel")
if not woodCountLabel then
woodCountLabel = Instance.new("TextLabel")
woodCountLabel.Name = "WoodCountLabel"
woodCountLabel.Text = "Wood: 0"
woodCountLabel.Size = UDim2.new(0, 200, 0, 50)
woodCountLabel.Position = UDim2.new(0.5, -100, 0, 10)
woodCountLabel.Parent = playerGui
end
local function setTransparency(part)
if part:IsA("BasePart") then
local newTransparency = part.Transparency + 0.4
part.Transparency = math.min(newTransparency, maxTransparency)
-- Disable collisions only when the part becomes completely transparent
part.CanCollide = part.Transparency < 1.0
end
end
local function handleTreeModel(treeModel)
if treeModel and treeModel:IsA("Model") then
for _, part in pairs(treeModel:GetDescendants()) do
setTransparency(part)
end
woodCount = woodCount + 1
-- Update the TextLabel directly
woodCountLabel.Text = "Wood: " .. woodCount
else
warn("Invalid tree model:", treeModel)
end
end
-- Add a function to set the current tree when the player clicks on it
tool.Activated:Connect(function()
if canHit then
canHit = false -- Prevent further hits until wait time is over
local mouse = player:GetMouse()
local target = mouse.Target
if target and target:IsA("BasePart") and target.Parent and target.Parent:IsA("Model") and target.Parent.Name == "tree" then
handleTreeModel(target.Parent)
else
warn("No valid tree selected.")
end
wait(waitTime) -- Wait for the specified time before allowing another hit
canHit = true -- Enable hitting again after the wait time
end
end)
-- Timer to reset transparency after a certain time
while true do
wait(resetTime)
-- Reset the tree transparency back to 0
for _, descendant in pairs(workspace:GetDescendants()) do
if descendant:IsA("BasePart") and descendant.Parent and descendant.Parent:IsA("Model") and descendant.Parent.Name == "tree" then
descendant.Transparency = 0
descendant.CanCollide = true
end
end
end