What do you want to achieve? Keep it simple and clear!
Hello, so what am trying to achieve is to only allow players to have a tool at a time. When they interact with the proximity if they have any tool that exist in their inventory. Then it will delete em and clone the Ak47.
What is the issue? Include screenshots / videos if possible!
there is no error in the output. It gives the Ak47 after the player interacted with the proximity but is not deleting the Starter tool that exist in their inventory.
local Ak47 = game.ReplicatedStorage:WaitForChild("Ak47")
local ProximityPrompt = workspace.Sw.ProximityPrompt
ProximityPrompt.Triggered:Connect(function(player)
if not player.Backpack:FindFirstChild(Ak47.Name) then
print("Player doesn't have " .. Ak47.Name)
Ak47:Clone()
Ak47.Parent = player.Backpack
else
print("Player already have " .. Ak47.Name)
for _,v in pairs(game.StarterPack:GetChildren()) do
if v.Name == "ClassicSword" and not player.Backpack:FindFirstChild(Ak47.Name) then
print(v.Name .. " has been deleted!")
v:Destroy()
end
end
end
end)
for _,v in pairs(player.Backpack:GetChildren()) do
if v.Name == "ClassicSword" and not player.Backpack:FindFirstChild(Ak47.Name) then
print(v.Name .. " has been deleted!")
v:Destroy()
end
end
Because according to the API:
When a player’s character spawns, the contents of the StarterPack and their StarterGear are copied into their Backpack . Once a character dies, the Backpack is removed and a new one is created – populating it using the contents of StarterPack and StarterGear.
Could you include the output as well in another video? If I understood what you said correctly then you want there to be only one tool in the backpack at a time. The script makes it look like your trying to have only one AK. Maybe add another variable for backpack? I believe the line would be
Local Backpack = player.Backpack(“Backpack ”)
I’m assuming you want the player to have only 1 tool at a time.
local Ak47 = game.ReplicatedStorage:WaitForChild("Ak47")
local ProximityPrompt = workspace.Sw.ProximityPrompt
ProximityPrompt.Triggered:Connect(function(player)
local a = player.BackPack:GetChildren()
local b = player.Character:GetChildren()
local c = {table.unpack(a),table.unpack(b)}
for i,v in pairs(c) do
if v:IsA("Tool") then
v:Destroy()
end
end
local d = Ak47:Clone()
d.Parent = player.Backpack
end)
Just replace your code with this and it should work.
Edit: did some testing with merging tables, doesn’t work as expected. This may not work.
I never knew this function existed, thank you for educating me. Your solution is way less complicated than mine lol (AND IT MIGHT NOT EVEN WORK!!!), I check every instance inside the Character and BackPack to see if there is a tool.
ClearAllChildren() may delete some stuff you wanted to keep though.
local Ak47 = game.ReplicatedStorage:WaitForChild("Ak47")
local ProximityPrompt = workspace.Sw.ProximityPrompt
ProximityPrompt.Triggered:Connect(function(player)
for _,v in pairs(game.StarterPack:GetChildren()) do
if v.Name == "ClassicSword" and not player.Backpack:FindFirstChild(Ak47.Name) then
print(v.Name .. " has been deleted!")
v:Destroy()
end
end
if not player.Backpack:FindFirstChild(Ak47.Name) then
print("Player doesn't have " .. Ak47.Name)
local Clone = Ak47:Clone()
Clone.Parent = player.Backpack
print("Player doesn't have " .. Ak47.Name)
else
print("Player already have " .. Ak47.Name)
end
end)
local StarterPack = game:GetService("StarterPack")
local Replicated = game:GetService("ReplicatedStorage")
local Ak47 = Replicated:WaitForChild("Ak47")
local SW = workspace:WaitForChild("Sw")
local Prompt = SW:WaitForChild("ProximityPrompt")
Prompt.Triggered:Connect(function(Player)
local Character = Player.Character
local Backpack = Player.Backpack
local StarterGear
if Character and Backpack then
local CharacterTool = Character:FindFirstChild(Ak47.Name)
local BackpackTool = Backpack:FindFirstChild(Ak47.Name)
if not (CharacterTool or BackpackTool) then
local Ak47Clone = Ak47:Clone()
Ak47Clone.Parent = Backpack
elseif CharacterTool or BackpackTool then
local Sword = Backpack:FindFirstChild("ClassicSword")
if Sword then
Sword:Destroy()
end
end
end
end)
I provided this in the other thread you posted, this is working for me.
The pleasure is mine to assist a fellow developer! With developers eager to resolve all questions/problems proposed on the devforum, discussing the matter helps with retain desired information.
In regard to the prior usage of ClearAllChildren() in the code written by myself, the simple process of rectification & a miniscule amount of drive of stimulating manner (expectations in mind) will yield results desired.