Hi, so i know this is a lot to ask for, but i was watching the tutorial by alvin blox on how to make a sword fight game, so i was on the part where he was coding the shop and everything, so i did it and it all works fine, but i felt like of course, it would be better if i actually understood it lol. This is because it was pretty confusing for me, so i was wondering if anyone could help me maybe understand it a bit more? Thank you very much.

here is the link of the video: https://www.youtube.com/watch?v=WN932TP8GUU&t=2801s&ab_channel=AlvinBlox

this is the first script inside the shop gui (a local script)

-- Core UI localscript

local availableTools = game.ReplicatedStorage:WaitForChild("GetTools"):InvokeServer()
local mainFrame = script.Parent:WaitForChild("MainFrame")
local safeArea = mainFrame:WaitForChild("SafeArea")
local itemInformation = safeArea:WaitForChild("ItemInformation")
local infoFrame = itemInformation.InfoFrame
local selectedItem = itemInformation.SelectedItem
local equippedItem = itemInformation.EquippedItem
local numberOfItems = #availableTools

local itemFrame = safeArea.ItemFrame
local buyButton = infoFrame.BuyButton
local equippedItemViewport = script.Parent:WaitForChild("EquippedItemViewport")
local itemViewport = itemInformation.ItemViewport


game.ReplicatedStorage.SendEquipped.OnClientEvent:Connect(function(equipped)
	equippedItem.Value = equipped
	if equippedItem.Value ~= "" then
		local fakeCam = Instance.new("Camera")
		fakeCam.Parent = equippedItemViewport
		local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(equippedItem.Value.."Handle"):Clone()
		handle.Parent = equippedItemViewport
		equippedItemViewport.CurrentCamera = fakeCam
		fakeCam.CFrame = handle.CameraCFrame.Value
	end
end)

game.ReplicatedStorage.ShopOpen.OnClientEvent:Connect(function()
	script.Parent.MainFrame.Visible = true
end)

local PADDING_X = 0.02
local DROPDOWN_Y = 0.13
local DROPDOWN_X = 0.25

local item1 = itemFrame:WaitForChild("Item1")

local box
local numRows = 1

for i = 1,numberOfItems,1 do
	
	if i == 1 then
		box = item1
	else
		
		box = item1:Clone()
		box.Name = "Item"..i
		box.Parent = itemFrame
		
		
		if (i-1) / (4*numRows) == 1 then
			-- New row
			numRows = numRows + 1
			box.Position = UDim2.new(PADDING_X,0,box.Position.Y.Scale,0) + UDim2.new(0,0,DROPDOWN_Y*(numRows - 1))
			
		else
			-- Add to the X only
			box.Position = itemFrame["Item"..(i-1)].Position + UDim2.new(DROPDOWN_X,0,0,0)
		end
		
		
	end
	
	
	box.MouseButton1Click:Connect(function()
		for _, v in pairs(itemViewport:GetChildren()) do
			if not v:IsA("Frame") then
				v:Destroy()
			end
		end
		
		local itemViewportCam = Instance.new("Camera")
		itemViewportCam.Parent = itemViewport
		
		local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1].."Handle"):Clone()
		handle.Parent = itemViewport
		
		itemViewport.CurrentCamera = itemViewportCam
		itemViewportCam.CFrame = handle.CameraCFrame.Value
		
		local owned = game.ReplicatedStorage.ItemCheck:InvokeServer(availableTools[i][1])
		
		if equippedItem.Value == availableTools[i][1] then
			infoFrame.Cash.Text = "Owned"
			infoFrame.BuyButton.Text = "Unequip"
			infoFrame.Damage.Text = availableTools[i][3]
			infoFrame.Description.Text = availableTools[i][4]
		elseif owned == true then
			infoFrame.Cash.Text = "Owned"
			infoFrame.BuyButton.Text = "Equip"
			infoFrame.Damage.Text = availableTools[i][3]
			infoFrame.Description.Text = availableTools[i][4]
		else
			infoFrame.BuyButton.Text = "Buy"
			infoFrame.Cash.Text = "$"..availableTools[i][2]
			infoFrame.Damage.Text = availableTools[i][3]
			infoFrame.Description.Text = availableTools[i][4]
		end
		
		infoFrame.ItemName.Text = availableTools[i][1]
		selectedItem.Value = availableTools[i][1]
		
		for _, v in pairs(itemFrame:GetChildren()) do
			if v:IsA("ImageButton") then
				v.BorderSizePixel = 0
			end
		end
		
		itemFrame["Item"..i].BorderSizePixel = 2
		
	end)
	
	local fakeCam = Instance.new("Camera")
	fakeCam.Parent = box.VPF
	local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1].."Handle"):Clone()
	handle.Parent = box.VPF
	box.VPF.CurrentCamera = fakeCam
	fakeCam.CFrame = handle.CameraCFrame.Value
	itemFrame["Item"..i].ItemName.Text = availableTools[i][1]
	
end


buyButton.MouseButton1Click:Connect(function()
	local result = game.ReplicatedStorage.PurchaseItem:InvokeServer(selectedItem.Value)
	if result == true then
		buyButton.BackgroundColor3 = Color3.fromRGB(71, 255, 129)
		buyButton.Frame.BackgroundColor3 = Color3.fromRGB(0, 223, 82)
		buyButton.Text = "Bought!"
		script.Parent.Purchase:Play()
		wait(0.5)
		buyButton.Text = "Equip"
		buyButton.BackgroundColor3 = Color3.fromRGB(71, 255, 129)
		buyButton.Frame.BackgroundColor3 = Color3.fromRGB(0, 223, 82)
	elseif result == "NotEnoughPoints" then
		buyButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
		buyButton.Frame.BackgroundColor3 = Color3.fromRGB(193, 0, 0)
		buyButton.Text = "Not enough points!"
		script.Parent.Beep:Play()
		wait(0.5)
		buyButton.Text = "Buy"
		buyButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
		buyButton.Frame.BackgroundColor3 = Color3.fromRGB(193, 0, 0)
	elseif result == "Equipped" then
		equippedItem.Value = selectedItem.Value
		buyButton.BackgroundColor3 = Color3.fromRGB(71, 255, 129)
		buyButton.Frame.BackgroundColor3 = Color3.fromRGB(0, 223, 82)
		buyButton.Text = "Equipped!"
		script.Parent.equip2:Play()
		wait(0.5)
		buyButton.Text = "Unequip"
		buyButton.BackgroundColor3 = Color3.fromRGB(71, 255, 129)
		buyButton.Frame.BackgroundColor3 = Color3.fromRGB(0, 223, 82)
	elseif result == "Unequipped" then
		equippedItem.Value = ""
		buyButton.BackgroundColor3 = Color3.fromRGB(71, 255, 129)
		buyButton.Frame.BackgroundColor3 = Color3.fromRGB(0, 223, 82)
		buyButton.Text = "Unequipped!"
		script.Parent.uneqip:Play()
		wait(0.5)
		buyButton.Text = "Equip"
		buyButton.BackgroundColor3 = Color3.fromRGB(71, 255, 129)
		buyButton.Frame.BackgroundColor3 = Color3.fromRGB(0, 223, 82)
		
	end
end)


if equippedItem.Value ~= "" then
	local fakeCam = Instance.new("Camera")
	fakeCam.Parent = equippedItemViewport
	local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(equippedItem.Value.."Handle"):Clone()
	handle.Parent = equippedItemViewport
	equippedItemViewport.CurrentCamera = fakeCam
	fakeCam.CFrame = handle.CameraCFrame.Value
end

equippedItem:GetPropertyChangedSignal("Value"):Connect(function()
	if equippedItem.Value ~= "" then
		
		for _, v in pairs(equippedItemViewport:GetChildren()) do
			if not v:IsA("Folder") then
				v:Destroy()
			end
		end
		
		local fakeCam = Instance.new("Camera")
		fakeCam.Parent = equippedItemViewport
		local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(equippedItem.Value.."Handle"):Clone()
		handle.Parent = equippedItemViewport
		equippedItemViewport.CurrentCamera = fakeCam
		fakeCam.CFrame = handle.CameraCFrame.Value
	else
		for _, v in pairs(equippedItemViewport:GetChildren()) do
			if not v:IsA("Folder") then
				v:Destroy()
			end
		end
	end
end)

the next one is a script in server script service here:

game.ReplicatedStorage:WaitForChild("GetTools").OnServerInvoke = function(player)
	local items = {}
	
	for _, object in pairs(game.ServerStorage:WaitForChild("Items"):GetChildren()) do
		local itemProperties = {object.Name,object.Price.Value,object.Dmg.Value,object.Desc.Value}
		table.insert(items,itemProperties)
	end
	
	return items
end

game.ReplicatedStorage:WaitForChild("ItemCheck").OnServerInvoke = function(player,itemName)
	if game.ServerStorage.PlayerData:FindFirstChild(player.Name).Inventory:FindFirstChild(itemName) then
		return true
	else
		return false
	end
	
end

game.ReplicatedStorage:WaitForChild("PurchaseItem").OnServerInvoke = function(player,itemName)
	local Points = player.leaderstats.Points
	local item = game.ServerStorage.Items:FindFirstChild(itemName)
	
	if item then
		--Item exists
		if game.ServerStorage.PlayerData[player.Name].Inventory:FindFirstChild(itemName) then
			if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= itemName then
				--Currently unequipped
				game.ServerStorage.PlayerData[player.Name].Equipped.Value = itemName
				return "Equipped"
			else
				game.ServerStorage.PlayerData[player.Name].Equipped.Value = ""
				return "Unequipped"
			end
		end
		
		if Points.Value >= item.Price.Value then
			Points.Value = Points.Value - item.Price.Value
			
			local itemValue = Instance.new("ObjectValue")
			itemValue.Name = itemName
			itemValue.Parent = game.ServerStorage.PlayerData[player.Name].Inventory
			
			return true
		else
			
			return "NotEnoughPoints"
		end
		
		
	else
		return "NoItem"
	end
end

once again thanks a lot if you can help!

2 comments

0

If you were watching his tutorial there is high chance he explained during process, I suggest you to skip back and analyze script along with him and understand it step by step.

0

ok, thanks