CollectionService not working with my Trees which have Proximity Prompts

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


5 Likes

i have better solution for you to make axe system, make that when tool is activated, a invisible part appears for a moment in front of it, then you give size of this part or make bounding box, then use workspace:getPartsInPart(this new part made) with that it will return a table of parts that was inside, then it’s easy, make loop:

for i, v in HittedParts do
if v.Parent.Name == "Tree" then
-- chopping tree code
end
end

this solution don’t need any collection service, but only a activated event

2 Likes

Eh honestly I just want to get it finished this way, because even though your way may be better I am not to confident with scripting right now. No offence to you of course, I really appreciate the help.

3 Likes

collection service is for tagging, making axe system with it it’s a unpractical solution, you can make all trees have proximity prompt, this is bad practice, but for now, and then when you trigger it, it checks if you have axe, for me you should keen on simplier things that tree choping, make low cost project, this mean something like one model, it will help you understand more

3 Likes

Yeah I understand, but the script isn’t going into the trees it is only the axe so I have to have it so the axe script tells me which tree has been cut somehow, and how am I going to make it so the server knows which tree to delete when I am cutting it with the axe?

2 Likes