Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

what can i do to improve this weapon upgrade script? After the player gets a lot of guns it stops

Reposting this as i made a new system. im working off a shop script and using it to replace guns with their respective upgrades. here's the script:

local Item = script.Parent.Item
local Price = script.Parent.Price
local Currency = script.Parent.Currency
script.Parent.ClickDetector.MouseClick:Connect(function(player)
	local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value)
    local ItemName = game.Lighting.WSTORAGE:FindFirstChild("C-3000")
	if CurrencyName.Value >=Price.Value and player.Backpack:FindFirstChild("C-3000") == nil and player.Backpack:FindFirstChild("USP") ~= nil and player.Character:FindFirstChild("C-3000") == nil then
	CurrencyName.Value = CurrencyName.Value - Price.Value
		player.Backpack:FindFirstChild("USP"):Destroy()
		ItemName:Clone().Parent = player.Backpack -- dont change anything here
	else
		local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value)
		local ItemName = game.Lighting.WSTORAGE:FindFirstChild("Your Revenge")
		if CurrencyName.Value >=Price.Value and player.Backpack:FindFirstChild("Your Revenge") == nil and player.Backpack:FindFirstChild("AK74") ~= nil and player.Character:FindFirstChild("Your Revenge") == nil then
			CurrencyName.Value = CurrencyName.Value - Price.Value
			player.Backpack:FindFirstChild("AK74"):Destroy()
			ItemName:Clone().Parent = player.Backpack -- dont change anything here
		else
			local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value)
			local ItemName = game.Lighting.WSTORAGE:FindFirstChild("Dragon Rage")
			if CurrencyName.Value >=Price.Value and player.Backpack:FindFirstChild("Dragon Rage") == nil and player.Backpack:FindFirstChild("Dragon Breath") ~= nil and player.Character:FindFirstChild("Dragon Rage") == nil then
				CurrencyName.Value = CurrencyName.Value - Price.Value
				player.Backpack:FindFirstChild("Dragon Breath"):Destroy()
				ItemName:Clone().Parent = player.Backpack -- dont change anything here
			else
				local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value)
				local ItemName = game.Lighting.WSTORAGE:FindFirstChild("Flaming Sun")
				if CurrencyName.Value >=Price.Value and player.Backpack:FindFirstChild("Flaming Sun") == nil and player.Backpack:FindFirstChild("Dragon Rage") ~= nil and player.Character:FindFirstChild("Flaming Sun") == nil then
					CurrencyName.Value = CurrencyName.Value - Price.Value
					player.Backpack:FindFirstChild("Dragon Rage"):Destroy()
					ItemName:Clone().Parent = player.Backpack -- dont change anything here
				else
					local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value)
					local ItemName = game.Lighting.WSTORAGE:FindFirstChild("Lamentation")
					if CurrencyName.Value >=Price.Value and player.Backpack:FindFirstChild("Lamentation") == nil and player.Backpack:FindFirstChild("Galil") ~= nil and player.Character:FindFirstChild("Lamentation") == nil then
						CurrencyName.Value = CurrencyName.Value - Price.Value
						player.Backpack:FindFirstChild("Galil"):Destroy()
						ItemName:Clone().Parent = player.Backpack -- dont change anything here
					else
						local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value)
						local ItemName = game.Lighting.WSTORAGE:FindFirstChild("Ultima Lazer")
						if CurrencyName.Value >=Price.Value and player.Backpack:FindFirstChild("Ultima Lazer") == nil and player.Backpack:FindFirstChild("Lazer") ~= nil and player.Character:FindFirstChild("Ultima Lazer") == nil then
							CurrencyName.Value = CurrencyName.Value - Price.Value
							player.Backpack:FindFirstChild("Lazer"):Destroy()
							ItemName:Clone().Parent = player.Backpack -- dont change anything here
						else
							local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value)
							local ItemName = game.Lighting.WSTORAGE:FindFirstChild("The Reaper")
							if CurrencyName.Value >=Price.Value and player.Backpack:FindFirstChild("The Reaper") == nil and player.Backpack:FindFirstChild("PPSH-41") ~= nil and player.Character:FindFirstChild("The Reaper") == nil then
								CurrencyName.Value = CurrencyName.Value - Price.Value
								player.Backpack:FindFirstChild("PPSH-41"):Destroy()
								ItemName:Clone().Parent = player.Backpack -- dont change anything here
							else
								local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value)
								local ItemName = game.Lighting.WSTORAGE:FindFirstChild("RAYGUN X2")
								if CurrencyName.Value >=Price.Value and player.Backpack:FindFirstChild("RAYGUN X2") == nil and player.Backpack:FindFirstChild("RAYGUN") ~= nil and player.Character:FindFirstChild("RAYGUN X2") == nil then
									CurrencyName.Value = CurrencyName.Value - Price.Value
									player.Backpack:FindFirstChild("RAYGUN"):Destroy()
									ItemName:Clone().Parent = player.Backpack -- dont change anything here
								else
									local CurrencyName = player.leaderstats:FindFirstChild(Currency.Value)
									local ItemName = game.Lighting.WSTORAGE:FindFirstChild("Tornado XR2")
									if CurrencyName.Value >=Price.Value and player.Backpack:FindFirstChild("Tornado XR2") == nil and player.Backpack:FindFirstChild("Tornado") ~= nil and player.Character:FindFirstChild("Tornado XR2") == nil then
										CurrencyName.Value = CurrencyName.Value - Price.Value
										player.Backpack:FindFirstChild("Tornado"):Destroy()
										ItemName:Clone().Parent = player.Backpack -- dont change anything here
									else
											print "No Upgrade needed"
	
										
end
end
end
end
end				
end				
end
end
end		
end)

I'm not getting any errors and the script seems to work, but some times it'll just print the "No Upgrade needed" when i have tools in the inventory that could be upgraded. what can i do? i thought about limiting the inventory space but im having a hard time making a script that'll limit the inventory w/o just deleting the next item given. is there anything i can do to improve this script? thx

4 comments

0

Why do you use else that many times?

0

because its in a single brick and im not sure how i would make it so it checks for each weapon without using "else"

0

maybe check out for i,v in pairs & tables, they sound like theyd be useful for you.

1

As said above, I recommend not to use else that many times, and use i,v in pairs & tables.

1 answer

Hey!

I'd highly recommend you used :GetChildren(). Here's a sample.

script.Parent.MouseButton1Click:Connect(function()
	local Player = game.Players.LocalPlayer
	local s = Player:WaitForChild("Backpack"):GetChildren())
	
	if s[1].Name == "ToolNameHere" and s[2].Name == s[1].Name then
		s[2]:Destroy()
		if s:WaitForChild(s[3]) then
		s[3]:Destroy()
			end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.