Making A Shop Script

So currently I have a script that checking wether you’re backpack has these 3 exact items: Wood, wood and stone. (In any order) If they are there, then delete them and replace it with a sword kept in “lighting”
My script is… inefficent:

local Player = game.Players.LocalPlayer
local button = script.Parent
local Light = game:GetService("Lighting")
local woodClone = Light.Wood
local stoneClone = Light.Stone

button.MouseButton1Click:Connect(function()
	local backpack = Player:WaitForChild("Backpack")
	if backpack then
		print("Found Player Backpack")
		
		local woodCheck = backpack:FindFirstChild("Wood")
		 if woodCheck then
			woodCheck:Destroy()
			local woodCheck2 = backpack:FindFirstChild("Wood")
			
			if woodCheck2 then
				woodCheck2:Destroy()
				local stoneCheck = backpack:FindFirstChild("Stone")
				
				if stoneCheck then
					print("2 wood and 1 stone")
					stoneCheck:Destroy()
					local strongsword = Light.SwordOfLight:Clone()
					strongsword.Parent = backpack
				else
					stoneClone:Clone()
					stoneClone.Parent = backpack
				end
				
			else
				woodClone:Clone()
				woodClone.Parent = backpack
				print("1 wood")
			end
			
		 else
			print("No wood")
		 end
	end
end)

Is there are more easier/efficent way of doing this??? Thanks!

Please use some type of value storage like IntValue. You can tie the value to the player then index it when you need to see what value they have

game.Players.PlayerAdded:Connect(function(plr)
  local woodValue = Instance.new("IntValue")
  woodValue.Name = "Wood"
  woodValue.Value = 0 -- or whatever you want for testing or a datastore
  woodValue.Parent = plr -- could stick in it a folder for orginization

  local stoneValue = Instance.new("IntValue") -- repeat for stone
  stoneValue.Name = "Wood"
  stoneValue.Value = 0 
  stoneValue.Parent = plr
end)

Then you can see how much stone or wood they have with

plr.Wood.Value

plr.Stone.Value
-- then just do an if >= to see if they have an amount needed
if plr.Stone.Value >= 1 and plr.Wood.Value >= 2 then
  -- you can craft !
  plr.Wood.Value -= 2
  plr.Stone.Value -= 1 -- subtract crafting amount
end

These snipets are a rough overview of how it can work for simple value storing

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.