Tools (a Card) wil not be destroyed

I need help with a script that should destroy the cards after discarding after 5 seconds

local PlayerService = game:GetService("Players")
local DebrisService = game:GetService("Debris")

local function RemoveToolFromBackpack(player)
    local backpack = player:FindFirstChild("Backpack")
    if backpack then
        local tool = backpack:FindFirstChild("Keycard Level 1") or
                     backpack:FindFirstChild("Keycard Level 2") or
                     backpack:FindFirstChild("Keycard Level 3") or
                     backpack:FindFirstChild("Keycard Level 4")

        if tool then
            tool.Parent = game.Workspace  -- Přesuneme nástroj do pracovního prostoru (země)
            print("Karta byla odhozena na zem.")
            coroutine.wrap(function()
                wait(5)
                if tool and tool.Parent == game.Workspace then
                    tool:Destroy()  -- Zničíme kartu, pokud je stále na zemi
                    print("Karta byla zniÄŤena.")
                end
            end)()
        else
            print("V batohu nemáte žádnou kartu.")
        end
    end
end

PlayerService.PlayerAdded:Connect(function(player)
    player:GetMouse().KeyDown:Connect(function(key)
        if key == "Backspace" then
            RemoveToolFromBackpack(player)
        end
    end)
end)

video example:

thanks in advance for your help
Cody

1 Like

is this a local script or server script?

1 Like

This script in ServerScriptService as Script.

1 Like

you cant get player inputs from the server, you have to use a local script and a remote. Insert a remote into replicated storage, and insert a localscript into starterpack

put this in the local script:

game:GetService("UserInputService").InputEnded:Connect(function(key)
 if key.KeyCode == Enum.KeyCode.Backspace then
game.ReplicatedStorage.PUTREMOTENAMEHERE:FireServer()
 end
end)

and change this in the script you showed

--OLD REMOVE THIS

PlayerService.PlayerAdded:Connect(function(player)
    player:GetMouse().KeyDown:Connect(function(key)
        if key == "Backspace" then
            RemoveToolFromBackpack(player)
        end
    end)
end)

--PUT THIS INSTEAD
game.replicatedstorage.PUTREMOTENAMEHERE.OnServerEvent(RemoveToolFromBackpack)
1 Like

so i did as i was advised.
Make script in the RS and name ToolsDestroyed
after make LocalScript in the StartPack.

  1. Script name ToolsDestroyed in RS
local PlayerService = game:GetService("Players")
local DebrisService = game:GetService("Debris")

local function RemoveToolFromBackpack(player)
    local backpack = player:FindFirstChild("Backpack")
    if backpack then
        local tool = backpack:FindFirstChild("Keycard Level 1") or
                     backpack:FindFirstChild("Keycard Level 2") or
                     backpack:FindFirstChild("Keycard Level 3") or
                     backpack:FindFirstChild("Keycard Level 4")

        if tool then
            tool.Parent = game.Workspace  -- Přesuneme nástroj do pracovního prostoru (země)
            print("Karta byla odhozena na zem.")
            coroutine.wrap(function()
                wait(5)
                if tool and tool.Parent == game.Workspace then
                    tool:Destroy()  -- Zničíme kartu, pokud je stále na zemi
                    print("Karta byla zniÄŤena.")
                end
            end)()
        else
            print("V batohu nemáte žádnou kartu.")
        end
    end
end

--PUT THIS INSTEAD
game.replicatedstorage.ToolsDestroyed.OnServerEvent(RemoveToolFromBackpack)

2.Script as LocalScript name Comunication in StarterPack (the name is not important here)

game:GetService("UserInputService").InputEnded:Connect(function(key)
 	if key.KeyCode == Enum.KeyCode.Backspace then
		game.ReplicatedStorage.ToolsDestroyed:FireServer()
 	end
end)

and it doesn’t work :sleepy:
Cody

sorry the last line of the server script has a mistake, replace it with this:

game.ReplicatedStorage.ToolsDestroyed.OnServerEvent:Connect(RemoveToolFromBackpack)

also dont put the scrupt in replicated storage, put it in serverscript service, put a remote event in replicated storage and name it ToolsDestroyed

it still doesn’t work .
uáááááá
For example I make primitive game uncopylocked.

Cody

Roblox Output reports an error.

@12312ababc’s solution is more complex than it needs to be.
A script inside the tool that watches for a change of parent works just fine:

local tool = script.Parent

tool:GetPropertyChangedSignal("Parent"):Connect(function() -- listen for changes to tool.Parent
	if tool.Parent == game.Workspace then
		task.wait(5)
		if tool and tool.Parent == game.Workspace then
			-- known bug: it'll still get destroyed if it's dropped, picked
			-- up, and then dropped again (before the 5 seconds are over)
			tool:Destroy()
		end
	end
end)

Before setting the tool’s parent to workspace you could just schedule its destruction with Debris.

local Debris = game:GetService('Debris')

Debris:AddItem(tool, 5)
tool.Parent = game.Workspace

Alternatively you could also just use a Delay.

task.delay(5, function()
	tool:Destroy()
	print('Tool destroyed')
end)
tool.Parent = game.Workspace

I have now 2 Scripts

1. Script in forder “StarterPlayerScripts”

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ToolsRemoved = ReplicatedStorage:WaitForChild("ToolsRemoved")

game:GetService("UserInputService").InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Backspace then
        ToolsRemoved:FireServer()
    end
end)

after 2. Script in the folder “ServerScriptService” with name “ToolsRemoved”

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ToolsRemoved = ReplicatedStorage:WaitForChild("ToolsRemoved")
local Debris = game:GetService("Debris")

local function RemoveActiveTool(player)
    print("Removing active tool...")
    
    local function FindActiveTool()
        local character = player.Character
        if character then
            for _, child in ipairs(character:GetChildren()) do
                if child:IsA("Tool") then
                    return child
                end
            end
        end
        return nil
    end

    local activeTool = FindActiveTool()
    if activeTool then
        print("Active tool found:", activeTool.Name)
        activeTool.Parent = game.Workspace  -- Přesuneme nástroj do pracovního prostoru (země)
        if activeTool.Parent == game.Workspace then
            print("Aktivovaný nástroj byl úspěšně odhozen na zem.")
            
            -- Přidáme nástroj do seznamu položek pro smazání službou Debris
            Debris:AddItem(activeTool, 5) -- Nástroj bude zničen po uplynutí 5 sekund
            
            wait(5)  -- Počkáme 5 vteřin
            if activeTool.Parent == game.Workspace then
                activeTool:Destroy()  -- Zničíme aktivovaný nástroj, pokud je stále na zemi
                print("Aktivovaný nástroj byl zničen.")
            end
        else
            warn("Nepodařilo se přesunout aktivovaný nástroj do pracovního prostoru.")
        end
    else
        warn("Hráč nemá žádný aktivní nástroj.")
    end
end

ToolsRemoved.OnServerEvent:Connect(RemoveActiveTool)
print("Tool removal script initialized.")

and RemoteEvent in folder ReplicatedStorage with name “ToolsRemoved”

Video example:

uncopylocked game is here

thanks for the idea, I kept combining it through one big script.
it works
Cody

I found a function with the same name in v HandleServer into the Tools at Fe Gun Kit :upside_down_face:


Tool:GetPropertyChangedSignal("Parent"):Connect(function()
	if Tool.Parent:IsA("Backpack") then
		Player = Tool.Parent.Parent
		Character = Player.Character
		if Character and Character:FindFirstChild(Settings.Weapon_WeaponWeldPart0Name) and Settings.Weapon_AttachWeapon then
			local ModelInstance = Instance.new("Model", Character)
			ModelInstance.Name = Tool.Name.."Clone"
			local NewTool = Tool:Clone()
			local Weld = Instance.new("Weld")
			if Character:FindFirstChild(Settings.Weapon_WeaponWeldPart0Name) then
				Weld.Parent = Character[Settings.Weapon_WeaponWeldPart0Name]
				Weld.Part0 = Character[Settings.Weapon_WeaponWeldPart0Name]
			elseif Character:FindFirstChild("Torso") then
				Weld.Parent = Character["Torso"]
				Weld.Part0 = Character["Torso"]
			end
			for _, Object in pairs(NewTool:GetChildren()) do
				if Object:IsA("BasePart") then
					Object.Parent = ModelInstance
					if Object.Name == "Default" then
						Weld.Part1 = ModelInstance["Default"]
					end
				end
			end
			Weld.C0 = Settings.Weapon_WeaponWeldC0
			Weld.C1 = Settings.Weapon_WeaponWeldC1
			ModelInstance["Default"].CanCollide = false
			NewTool:Destroy()
end