Hello, I have a problem, I want to remove a tool in the player backpack when playing but… I have still the tool.
local rs = game:GetService("ReplicatedStorage")
rs.DestroyTool.OnServerEvent:Connect(function(plr, toolName)
local possibleTool = plr.Character:FindFirstChild(toolName)
if possibleTool then
print(plr.Character:FindFirstChild(toolName), plr.Character:FindFirstChild(toolName).Parent)
plr.Character:FindFirstChild(toolName):Destroy()
print(plr.Character:FindFirstChild(toolName), plr.Character:FindFirstChild(toolName).Parent)
end
end)
-- # Services
local ReplicatedStorage = game:GetService('ReplicatedStorage');
-- # Event
local DestroyTool = ReplicatedStorage:WaitForChild('DestroyTool');
-- # Functions
local GetAllTools = function(player: Player): {Tool?}
local Tools = {};
local Backpack = player.Backpack;
for _, v in ipairs(Backpack:GetChildren()) do
if v:IsA('Tool') then
table.insert(Tools, v);
end;
end;
local Character = player.Character;
if Character == nil then
return Tools;
end;
for _, v in ipairs(Character:GetChildren()) do
if v:IsA('Tool') then
table.insert(Tools, v);
end;
end;
return Tools;
end;
local OnServerEvent = function(player: Player, ToolName: string)
local AllTools = GetAllTools(player);
for _, v in ipairs(AllTools) do
if v.Name == ToolName then
v:Destroy();
end;
end;
end;
-- # Connections
DestroyTool.OnServerEvent:Connect(OnServerEvent);
-- # Usage (Client)
DestroyTool:FireServer('M9');
Maybe you have two tools with the same name inside your character.
local rs = game:GetService("ReplicatedStorage")
rs.DestroyTool.OnServerEvent:Connect(function(plr, toolName)
for i, v in pairs(plr.Character:GetChildren()) do
if v:IsA("Tool") and v.Name == toolName then
v:Destroy()
end
end
end)
This code will destroy all tools inside the character with the same name as toolName.
Also, tools can be parented to Player’s Backpack when unequipped.
local rs = game:GetService("ReplicatedStorage")
rs.DestroyTool.OnServerEvent:Connect(function(plr, toolName)
local backpack = plr:FindFirstChild("Backpack")
if backpack then
local possibleTool = backpack:FindFirstChild(toolName)
if possibleTool then
print(possibleTool, possibleTool.Parent)
possibleTool:Destroy()
print(backpack:FindFirstChild(toolName))destroyed
end
end
end)
local rs = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
rs.RequestTool.OnServerEvent:Connect(function(plr, category, toolName)
local t = ss.Weapons:FindFirstChild(category)
if t then
t = t:FindFirstChild(toolName)
if t then
local tool = t:Clone()
tool.Name = toolName
tool.Parent = plr.Character
end
end
end)
That event is probably being called twice. This can happen when you emit the event everytime a part is touched, for example, which can occur multiple times in the same second.
You don’t check if the player already has that tool either.
local rs = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
rs.RequestTool.OnServerEvent:Connect(function(plr, category, toolName)
local weaponsCategory = ss.Weapons:FindFirstChild(category)
if weaponsCategory then
local weapon = weaponsCategory:FindFirstChild(toolName)
if weapon then
if not player.Character or player.Character:FindFirstChild(toolName) or player.Backpack:FindFirstChild(toolName) then
-- end the function
return
end
local tool = weapon:Clone()
tool.Name = toolName --unnecessary line
tool.Parent = plr.Character
end
end
end)
or
if (player.Character and not player.Character:FindFirstChild(toolName)) or player.Backpack:FindFirstChild(toolName) then
local tool = weapon:Clone()
tool.Name = toolName --unnecessary line
tool.Parent = plr.Character
end
It’s not when I am touching a part , but when I am pressing “1”, “&” or 1 from numpad.
local UIS = game:GetService("UserInputService")
local rs = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
UIS.InputBegan:Connect(function(key, gp)
if key.KeyCode == Enum.KeyCode.Two or key.KeyCode == Enum.KeyCode.Tilde or key.KeyCode == Enum.KeyCode.KeypadTwo then
local tool = rs.RequestTool:FireServer("Secondary", "M9")
local char = plr.Character
local animator = char.Humanoid.Animator
local Equipping = script.Equipping
local EquippingTrack = animator:LoadAnimation(Equipping)
local Equipped = script.Equipped
local EquippedTrack = animator:LoadAnimation(Equipped)
local hasTool = rs.HasTool:InvokeServer("M9")
if not hasTool then
EquippingTrack:Play()
EquippingTrack.Stopped:Wait()
EquippedTrack:Play()
else
rs.DestroyTool:FireServer("M9")
for i, anim in ipairs(animator:GetPlayingAnimationTracks()) do
if anim.Name == "Equipped" or anim.Name == "Equipping" then
anim:Stop()
end
end
end
end
end)