Hi so I have this sword system made that when you reach a specific amount of coins you get it. For some reason when ever I gain clicks the tool un equips auto maticly. How do I fix this?
local RepStore = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(Player)
local Stats = Instance.new("Folder")
Stats.Name = "leaderstats"
Stats.Parent = Player
local Coins = Instance.new("IntValue")
Coins.Value = 0
Coins.Name = "Coins"
Coins.Parent = Stats
local Swords = Instance.new("Folder")
Swords.Name = "Swords"
Swords.Parent = game.StarterPack
Player.CharacterAdded:Connect(function(Character)
local SwordFolder = RepStore.Swords
local Highest = 0
local Dictionary = {
[100] = SwordFolder.Sword1,
[200] = SwordFolder.Sword2,
[300] = SwordFolder.Sword3,
}
local function Check()
for req, sword in pairs(Dictionary) do
if Coins.Value <= req then
Player.Backpack:ClearAllChildren()
for _,v in pairs(Character:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
end
end
end
if Coins.Value >= req and req > Highest then
Highest = req
end
if Coins.Value >= req then
Player.Backpack:ClearAllChildren()
local NewSword = Dictionary[Highest]:Clone()
for _,v in pairs(Character:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
end
end
NewSword.Parent = Player.Backpack
end
end
end
Check()
Coins.Changed:Connect(function()
Check()
end)
end)
end)
Remove the for loop when you add the tool. You already cleared all children from the player’s backpack, don’t repeat again, especially continously. It probably keeps deleting like that.
if Coins.Value >= req then
Player.Backpack:ClearAllChildren()
local NewSword = Dictionary[Highest]:Clone()
for _,v in
pairs(Character:GetChildren()) do -- Remove this
if v:IsA("Tool") then --Remove this
v:Destroy() -- Remove this
end -- Remove this
end
NewSword.Parent = Player.Backpack
end
end
end
Okay, for the first little bit of code here, can you delete the same stuff I told you to do before, except different line
local function Check()
for req, sword in pairs(Dictionary) do
if Coins.Value <= req then
Player.Backpack:ClearAllChildren()
for _,v in pairs(Character:GetChildren()) do -- Delete this
if v:IsA("Tool") then -- Delete this
v:Destroy() -- Delete this
end -- delete this
end
end
make a if statment and add a bool value (ill just call it equipped val) that you can duplicate and check so that if the equipped val dosen’t equal true then add this tool and you can do this for every tool and if you make a varriable for the cloned val, then you don’t have to worry about which clone you are reffering to
local equippedval = Instance.new("BoolVal") -- this is the equipped val
if equippedval.Value ~= true then
local function Check()
for req, sword in pairs(Dictionary) do
if Coins.Value <= req then
Player.Backpack:ClearAllChildren()
equippedval.Value == true
for _,v in pairs(Character:GetChildren()) do -- Delete this
if v:IsA("Tool") then -- Delete this
v:Destroy() -- Delete this
end -- delete this
end
end
local RepStore = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(Player)
local Stats = Instance.new("Folder")
Stats.Name = "leaderstats"
Stats.Parent = Player
local Coins = Instance.new("IntValue")
Coins.Value = 0
Coins.Name = "Coins"
Coins.Parent = Stats
local Swords = Instance.new("Folder")
Swords.Name = "Swords"
Swords.Parent = game.StarterPack
Player.CharacterAdded:Connect(function(Character)
local SwordFolder = RepStore.Swords
local Highest = 0
local Dictionary = {
[100] = SwordFolder.Sword1,
[200] = SwordFolder.Sword2,
[300] = SwordFolder.Sword3,
}
local equippedval = Instance.new("BoolVal") -- this is the equipped val
if equippedval.Value ~= true then
local function Check()
for req, sword in pairs(Dictionary) do
if Coins.Value <= req then
Player.Backpack:ClearAllChildren()
equippedval.Value = true
end
end
end
local RepStore = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(Player)
local Stats = Instance.new("Folder")
Stats.Name = "leaderstats"
Stats.Parent = Player
local Coins = Instance.new("IntValue")
Coins.Value = 0
Coins.Name = "Coins"
Coins.Parent = Stats
local Swords = Instance.new("Folder")
Swords.Name = "Swords"
Swords.Parent = game.StarterPack
Player.CharacterAdded:Connect(function(Character)
local SwordFolder = RepStore.Swords
local Highest = 0
local Dictionary = {
[100] = SwordFolder.Sword1,
[200] = SwordFolder.Sword2,
[300] = SwordFolder.Sword3,
}
local equippedval = Instance.new("BoolVal") -- this is the equipped val
if equippedval.Value ~= true then
local function Check()
for req, sword in pairs(Dictionary) do
if Coins.Value <= req then
Player.Backpack:ClearAllChildren()
equippedval.Value = true
end
end
end
end