Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to destroy a tool for all players?

Currently the tool is only destroyed for the player who clicks it.

localscript

local tool = script.Parent
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local hum = char:WaitForChild("Humanoid")

local HungerValue = player:WaitForChild("Stats").Hunger
local ThirstValue = player:WaitForChild("Stats").Thirst

local anim = hum:LoadAnimation(script.Parent.Animations:WaitForChild("Animation"))
local parts = tool.Parts:GetChildren()

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.Remotes:WaitForChild("FoodEvent")

amount = 2
playing = false
debounce = false

tool.Activated:Connect(function()
	if amount == 0 then
		tool:Destroy()
	else
		if playing == false then
			playing = true
		anim:Play()
		amount = amount - 1
		ThirstValue.Value = ThirstValue.Value + 5
		HungerValue.Value = HungerValue.Value + 5
		if HungerValue.Value > 100 then
			HungerValue.Value = 100
		end
		if ThirstValue.Value > 100 then
			ThirstValue.Value = 100
		end
		wait(2)
		playing = false
		if amount == 0 then
			debounce = true
			end
		end
	end
end)

local function dequip()
	for _,parts in pairs(parts) do
					parts.CanCollide = true
				end
end

local function equip()
	for _,parts in pairs(parts) do
					parts.CanCollide = false
				end
end

if debounce == true then
	Event:FireServer(tool)
end


tool.Equipped:Connect(equip)
tool.Unequipped:Connect(dequip)

Normal script


	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local Event = ReplicatedStorage.Remotes:WaitForChild("FoodEvent")
 
	local function onCreatePartFired(tool)
		wait(0.25)
		tool:Destroy()
	end
 
Event.OnServerEvent:Connect(onCreatePartFired)

1 comment

0

Im not sure but try to make game.Workspace.Starterpack.Tool:Destroy() if you want it to dissapear btw change tool to the correct name and the location too

1 answer

Script:

local tool = script.Parent
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local hum = char:WaitForChild("Humanoid")

local HungerValue = player:WaitForChild("Stats").Hunger
local ThirstValue = player:WaitForChild("Stats").Thirst

local anim = hum:LoadAnimation(script.Parent.Animations:WaitForChild("Animation"))
local parts = tool.Parts:GetChildren()

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.Remotes:WaitForChild("FoodEvent")

amount = 2
playing = false
debounce = false

tool.Activated:Connect(function()
    if amount == 0 then
        tool:Destroy()
    else
        if playing == false then
            playing = true
        anim:Play()
        amount = amount - 1
        ThirstValue.Value = ThirstValue.Value + 5
        HungerValue.Value = HungerValue.Value + 5
        if HungerValue.Value > 100 then
            HungerValue.Value = 100
        end
        if ThirstValue.Value > 100 then
            ThirstValue.Value = 100
        end
        wait(2)
        playing = false
        if amount == 0 then
            debounce = true
            end
        end
    end
end)

local function dequip()
    for _,parts in pairs(parts) do
                    parts.CanCollide = true
                end
end

local function equip()
    for _,parts in pairs(parts) do
                    parts.CanCollide = false
                end
end

while wait(.5) do
       if debounce == true then
         Event:FireServer(tool)
     end
end


tool.Equipped:Connect(equip)
tool.Unequipped:Connect(dequip)

LocalScript:

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Event = ReplicatedStorage.Remotes:WaitForChild("FoodEvent")

    local function onCreatePartFired(tool)
        wait(0.25)
        for i,v in pairs(game.Players:GetChildren()) do
                local FindTool = v.Backpack:FindFirstChild(tool)
                if FindTool then
                      FindTool:Destroy()
                end
          end
    end

Event.OnServerEvent:Connect(onCreatePartFired)

This discussion is preserved as it was. New answers, comments, and votes are closed.