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

How to remove a tool which is in the backpack?

Hello, i have a pick up script to pick up a tool from the ground. but i want to remove the tool immediately.

i have this script in workspace but nothing works how can i fix this?

local player = game.Players.LocalPlayer
local itemName = "SledgeHammer"
if player.Backpack:FindFirstChild(itemName) and player.Backpack[itemName]:IsA("Tool") then
	player.Backpack.SledgeHammer:Destroy()
end

i changed game.Players.LocalPlayer to game:GetService("Players") but still not working.

4 comments

0

local scripts don't work in workspace, it should be a server script

0

so any edit needed for that except for server script

0

yes, local scripts can't use LocalPlayer, you will need to change the way you include player

0

so how can indicate all players?

2 answers

If you want this to work in the local script, and the server can see it, then add a RemoteFunction/RemoteEvent.

If you just want it serversided then type the following script:

local player = game:GetService("Players").PlayerAdded:Connect(function()
local itemName = "SledgeHammer"
if player.Backpack:FindFirstChild(itemName) and player.Backpack[itemName]:IsA("Tool") then
    player.Backpack.SledgeHammer:Destroy()
end
end)

If this doesn't work try this:

local player = game:GetService("Players")
local backpack = player:WaitForChild("Backpack")
local character = player.Character or player.CharacterAdded:Wait()

local function Tools(container)
    for _, item in pairs(container:GetChildren()) do
        if item:IsA("Tool") and item.Name:match("SledgeHammer") then
            item:Destroy()
        end
    end
end

Tools(backpack)
Tools(character)

Local scripts only works when it's a descendant of any following objects/services:

  • A Player’s Backpack, such as a child of a Tool

  • A Player’s character model

  • A Player’s PlayerGui

  • A Player’s PlayerScripts.

  • The ReplicatedFirst service

Note: Let me know if it works, as I haven't tested it out.

10 comments

0

it's not working it says player is an unknown global. im using it as a server script in server script service

1

Which script?

1

Try again.

0

not working i tried multiple ways changed the tool name changed the script but still not working.

1

where is it placed?

1

it should be a server script in the workspace

0

yeah.i did it like that.

1

Did you try both?

1

Also, try using print statements, otherwise I can't help you.

0

nothing prints to output i changed the destroy as a print

You can use FindFirstChild and local variable and from there can just delete the tool.

local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(Player)
	local sledgeHammer = Player.Backpack:FindFirstChild('SledgeHammer')
	if sledgeHammer then 
		sledgeHammer:Destroy()
	end
end)

Use a server script and put it in ServerScriptService, that way exploiters cannot spoof this code.

1 comment

0

It's not working

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