I know this program may not be written absolutely perfectly, I am a beginner scripter. So I wrote my program for an Axe and Tree system and I want to apply it so that each Tree is able to be affected by the Axe, and each tree is in a CollectionService. I am not really too confident with CollectionService, I tagged my trees and they have prompts etc but once I use the prompt for one tree the system bugs a little and both trees disappear(have been cut down) even if I only used the proximity prompt of one tree, then the proximity prompt on the other tree is left and the other tree is gone fully as it should be.
Sorry if the above paragraph was a bit confusing I can phrase it better if needed!
The script is located within the Axe Tool in StarterPlayer.
local PPS = game:GetService("ProximityPromptService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Tree = game.Workspace.Trees.Tree
local CollectionService = game:GetService("CollectionService")
local ProxPrompt = Tree.ProxPrompt
local Health = Tree.Health.Value
for _,Tree in CollectionService:GetTagged("Tree")do
local function ToolEquipped()
local tool = player.Character:FindFirstChildWhichIsA("Axe")
if tool ~= nil then
ProxPrompt.Enabled = true
end
end
local function CheckPrompt()
if player.Character:FindFirstChild("Axe") then
ProxPrompt.Enabled = true
print("(hit " .. tostring(script.Parent.Name) .. ")")
while Health > 0 do
ProxPrompt.Enabled = false
print("Health is" .. tostring(Health) .. ")")
Health = Health - 1
task.wait(1)
end
for index, v in pairs(Tree:GetDescendants()) do
if not v:IsA("BasePart") then
continue
end
task.spawn(function()
v.Anchored = false
v.Transparency = 0.3
task.wait(3)
v.Transparency = 1
v.CanCollide = false
end)
end
print("Health is 0")
task.wait(30)
Health = 5
for _, v in pairs (Tree:GetDescendants())do
if v.Name == 'Part' then
v.Anchored = true
v.Transparency = 0
v.CanCollide = true
end
end
else
ProxPrompt.Enabled = false
print("Hit Cancelled")
player.Character.ChildAdded:Connect(function()
ToolEquipped(ProxPrompt)
end)
end
ProxPrompt.Enabled = true
end
ProxPrompt.Triggered:Connect(CheckPrompt)
end