hey guys i need to know how to remove duplications of tools in my roblox game i want the player to only have 1 copy of the tool not 2 just 1, how can i do this?
When adding a new tool to the player check if the tool already exists.
If it does then don’t add the weapon to their Backpack.
example:
local player = game.Players:FindFirstChild("WhateverPlayer")
local playerBackPack = player.Backpack
local toolName = "theToolsName"
if playerBackPack:FindFirstChild(toolName) == nil then -- if tool doesn't already exist then continue
-- code that adds weapon
end
i made this and it didnt work do you know a solution?
game.Players.PlayerAdded:Connect(function(plr)
local saber1 = "Lightsaber I"
local saber2 = "Lightsaber II"
local saber3 = "Lightsaber III"
local saber4 = "Lightsaber IV"
local saber5 = "Lightsaber V"
local saber6 = "LukeSaber"
local playerBackPack = plr.Backpack
plr.CharacterAdded:Connect(function(char)
local hum = char:FindFirstChild("Humanoid")
hum.HealthChanged:Connect(function()
if hum.Health <= 0 then
wait(6)
if playerBackPack:FindFirstChild(saber1) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber1 = game.ReplicatedStorage.Sabers["Lightsaber I"]:Clone()
saber1.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber2) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 2 then
local saber2 = game.ReplicatedStorage.Sabers["Lightsaber II"]:Clone()
saber2.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber3) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber3 = game.ReplicatedStorage.Sabers["Lightsaber III"]:Clone()
saber3.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber4) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber4 = game.ReplicatedStorage.Sabers["Lightsaber IV"]:Clone()
saber4.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber5) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber5 = game.ReplicatedStorage.Sabers["Lightsaber V"]:Clone()
saber5.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber6) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber6 = game.ReplicatedStorage.Sabers["LukeSaber"]:Clone()
saber6.Parent = plr:WaitForChild("Backpack")
end
end
end
end)
end)
end)
remove all the code from the PlayerAdded function, example below.
game.Players.PlayerAdded:Connect(function(player)
plr = player
end)
local saber1 = "Lightsaber I"
local saber2 = "Lightsaber II"
local saber3 = "Lightsaber III"
local saber4 = "Lightsaber IV"
local saber5 = "Lightsaber V"
local saber6 = "LukeSaber"
local playerBackPack = plr.Backpack
plr.CharacterAdded:Connect(function(char)
local hum = char:FindFirstChild("Humanoid")
hum.HealthChanged:Connect(function()
if hum.Health <= 0 then
wait(6)
if playerBackPack:FindFirstChild(saber1) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber1 = game.ReplicatedStorage.Sabers["Lightsaber I"]:Clone()
saber1.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber2) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 2 then
local saber2 = game.ReplicatedStorage.Sabers["Lightsaber II"]:Clone()
saber2.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber3) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber3 = game.ReplicatedStorage.Sabers["Lightsaber III"]:Clone()
saber3.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber4) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber4 = game.ReplicatedStorage.Sabers["Lightsaber IV"]:Clone()
saber4.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber5) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber5 = game.ReplicatedStorage.Sabers["Lightsaber V"]:Clone()
saber5.Parent = plr:WaitForChild("Backpack")
end
end
if playerBackPack:FindFirstChild(saber6) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber6 = game.ReplicatedStorage.Sabers["LukeSaber"]:Clone()
saber6.Parent = plr:WaitForChild("Backpack")
end
end
end
this should work
i tried it and it didnt work correctly this is what happend:
robloxapp-20230507-1152415.wmv (2.9 MB)
Sorry for replying extremely late, but I edited your code a bit. The problem seems to come from you reusing the same variables for the saber name and the actual saber clone. This code should hopefully work.
game.Players.PlayerAdded:Connect(function(plr)
local saber1 = "Lightsaber I"
local saber2 = "Lightsaber II"
local saber3 = "Lightsaber III"
local saber4 = "Lightsaber IV"
local saber5 = "Lightsaber V"
local saber6 = "LukeSaber"
local playerBackPack = plr.Backpack
plr.CharacterAdded:Connect(function(char)
wait(6)
if playerBackPack:FindFirstChild(saber1) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber1Clone = game.ReplicatedStorage.Sabers[saber1]:Clone()
saber1Clone.Parent = playerBackPack
end
end
if playerBackPack:FindFirstChild(saber2) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 2 then
local saber2Clone = game.ReplicatedStorage.Sabers[saber2]:Clone()
saber2Clone.Parent = playerBackPack
end
end
if playerBackPack:FindFirstChild(saber3) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber3Clone = game.ReplicatedStorage.Sabers[saber3]:Clone()
saber3Clone.Parent = playerBackPack
end
end
if playerBackPack:FindFirstChild(saber4) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber4Clone = game.ReplicatedStorage.Sabers[saber4]:Clone()
saber4Clone.Parent = playerBackPack
end
end
if playerBackPack:FindFirstChild(saber5) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber5Clone = game.ReplicatedStorage.Sabers[saber5]:Clone()
saber5Clone.Parent = playerBackPack
end
end
if playerBackPack:FindFirstChild(saber6) == nil then -- if tool doesn't already exist then continue
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber6Clone = game.ReplicatedStorage.Sabers[saber6]:Clone()
saber6Clone.Parent = playerBackPack
end
end
end)
end)
sadly this doesnt work either… i dont know what is going on but it looks like it should work
maybe try something like
if backpack:FindFirstChild(tool) then return end
toolclone = game.replicatedstorage.tool:clone()
toolclone.parent = backpack
im not sure though maybe i can help i have nothing to do
the other codes do seem like they should be working
Put backpack under CharacterAdded connection.
i am not sure what you mean by this, do you have an example?
do you have an example? like i dont know what i am suppose to do with this
so like for your code it would look like this i think?
if playerBackPack:FindFirstChild(saber1) then return end
if plr:WaitForChild("leaderstats")["Weapon N/B"].Value == 1 then
local saber1Clone = game.ReplicatedStorage.Sabers[saber1]:Clone()
saber1Clone.Parent = playerBackPack
end
end
idk if this will work but i guess it could be worth a shot if ur still struggling. I did something like this and it works for me.
Just define backpack on characteradded since it refreshes everytime player respawns
i will check if this works tmr morning, ty for your time!
Hello, you can try this:
game.PlayersAdded:Connect(function(Player : Player)
local Backpack = Player.Backpack
local CurrentItemsTable = {}
local function UpdateBackpackItems(Child)
if not CurrentItemsTable[Child.Name] then CurrentItemsTable[Child.Name] = 1
Child.Destroyed:Connect(function()
if CurrentItemsTable[Child.Name] then
CurrentItemsTable[Child.Name] = nil
end
end)
else Child:Destroy() end
end)
Backpack.ChildAdded:Connect(UpdateBackpackItems)
—Listener when Player character
loads
Player.CharacterAdded:Connect(function(c)
table.clear(CurrentItemsTable)
end)
end)
this looks good but i dont know how i could edit this cuz i never script like this
could you tell me how i can easily change this?
You don’t have to change many things as it manually
saves a tool into a table and checks everytime the player gets added a new tool in his backpack
fr? ok let me check it out with the old script
i have noticed that the issue isnt with the player spawning it gives the player tools like 2/3 seconds after the player spawned, do you know how to fix that?