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)
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)
so i did as i was advised.
Make script in the RS and name ToolsDestroyed
after make LocalScript in the StartPack.
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)
@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)
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”
I found a function with the same name in v HandleServer into the Tools at Fe Gun Kit
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