Scripting Help Roblox

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.

How would I do that? Sorry i don’t do scripting and my scripter isn’t awnsering for some reason

Ive added comments to show you what to remove

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

This is located in the last bit of block of code

It’s still un equipping my tool when ever I gain fingers

Do you have an error in output?

there is no error in the code either. Everything works it just un equips tool by itself when ever you gain

Are you sure it unequips or destroys the tool completely?

It destroys tools and unequips fine and works fine too

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

It duplicates tool for some reason but didnt unequip atleast

Duplicate tool meaning like if I have a new tool then it will give me sword one and two at the same time for some reason and have 2 tools

I think it was meant like that but im not exactly sure

Its supposed to be like if player has more than 300 coins then it will clear backpack and give the new tool

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

so like:

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

ah! dont put 2 equal signs like i did when setting equippedval to true


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


It’s saying close end in like line 37

1 Like

add another end so 4 ends. My bad! :sweat_smile:

1 Like
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