Tool Required To Pick Up Not Working

hello! im trying to make a script that makes it so that the player needs a broom equipped in order for them to pick up trash in my game.

if they dont have a broom, the script is supposed to make a gui visible that basically tells the player that they need a broom first, but none of it is working.

vid:
Tool Required Not Working (sorry for the terrible quality)

heres my script:

local Part = script.Parent.Parent
local Prompt = script.Parent
local GUI = game:GetService("Players").LocalPlayer.PlayerGui:WaitForChild("BroomGUI")
local GetABroomText = GUI.GetABroomText

Prompt.Triggered:Connect(function(player)
	if player then
		local objfolder = player:WaitForChild("ObjectiveFolder")
		local thingrequired = objfolder:WaitForChild("Thing Required")
		local amtcollected = thingrequired:WaitForChild("Amount Collected")
		local hasItem = false

		-- Check if the player has the required item
		for _, item in pairs(player.Backpack:GetChildren()) do
			if item.Name == "Broom" then
				hasItem = true
				break
			end
		end

		if hasItem then
			amtcollected.Value = amtcollected.Value + 1
			Part:Destroy()
			game.Workspace.BroomSweep:Play()
		else
			-- Show GUI
			GetABroomText.Visible = true
			game.Workspace.GetABroomSound:Play()

			-- Wait for 2 seconds
			wait(2)

			-- Hide GUI
			GetABroomText.Visible = false
		end
	end
end)

any help would be GREATLY appreciated! :slight_smile:

2 Likes

so I noticed you used a localscript (because of the localscript syntax) in a part,
but a localscript does not run in any part other than the plrs char or replicatedfirst

so you’d turn it in a serverscript by doing this:

local Part = script.Parent.Parent
local Prompt = script.Parent

Prompt.Triggered:Connect(function(player)
	local GUI = player.PlayerGui:WaitForChild("BroomGUI")
	local GetABroomText = GUI.GetABroomText
	-- the player will never be null when a prox prompt is triggered
	local objfolder = player:WaitForChild("ObjectiveFolder")
	local thingrequired = objfolder:WaitForChild("Thing Required")
	local amtcollected = thingrequired:WaitForChild("Amount Collected")

		-- Check if the player has the required item by searching for it in its backpack or if it's equipped
	local item = player.Backpack:FindFirstChild("Broom") or player.Character:FindFirstChild("Broom")
	if item then--item is not nil, so it exists
		amtcollected.Value += 1
		Part:Destroy()
		game.Workspace.BroomSweep:Play()
	else
		-- Show GUI
		GetABroomText.Visible = true
		game.Workspace.GetABroomSound:Play()

		-- Wait for 2 seconds
		task.wait(2)
		-- Hide GUI
		GetABroomText.Visible = false
	end
end)

also if you are planning to have a lot of these, then it would probably be a better idea to use CollectionService.
(to minimalize the amount of script instances needed)

so like this:

local CollectionService = game:GetService("CollectionService")
local proxprompts = CollectionService:GetTagged("YOUR TAG HERE")

for i,v in pairs(proxprompts) do
 local part = v.Parent
 v.Triggered:Connect(function(player :Player)
    local GUI = player.PlayerGui:WaitForChild("BroomGUI")
	local GetABroomText = GUI.GetABroomText
	-- the player will never be null when a prox prompt is triggered
	local objfolder = player:WaitForChild("ObjectiveFolder")
	local thingrequired = objfolder:WaitForChild("Thing Required")
	local amtcollected = thingrequired:WaitForChild("Amount Collected")

    local  item = player.Backpack:FindFirstChild("Broom") or player.Character:FindFirstChild("Broom")
    if item then workspace.BroomSweep:Play() part:Destroy() amtcollected.Value += 1 return end
   
   GetABroomText.Visible = true
   workspace.GetABroomSound:Play()
   task.wait(2)
   GetABroomText.Visible = false
 end)
end

you then just add a tag to each one of your prox prompts and that’s it.

1 Like

life-saver. thank you SO MUCH.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.