What is this ServerScript doing?

So this local script is sending the serverscript the item the player wants to buy
local replicatedStorage = game:GetService(“ReplicatedStorage”)–Defines replicated storage
local selectedItem = script.Parent.Parent.Parent:WaitForChild(“SelectedItem”)–Defines what selectedItem is

local success--Variable

script.Parent.MouseButton1Click:Connect(function()
	
	success = false--Makes success false
	if selectedItem.Value ~= nil then--If the value isn't equal to nil
		selectedItem.Value = script.Parent.Parent.Name
		success = replicatedStorage.CheckSale:InvokeServer(selectedItem.Value)--Make a request to the remote function
	end
	
	if success then
		print("Purchased!")
		
	else
			
			print("Purchase Failed!")
	end
	
end

But what is this serverscript doing? I don’t know what item is because I never defined it.

local replicatedStorage = game:GetService("ReplicatedStorage")--Defines replicated storage
local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems")--Defines the items in ServerStorage

replicatedStorage.CheckSale.OnServerInvoke = (function(player,item)--Responds to the remote function  request
	
	local price = shopItems[item].Points.Value--Defines the price of the item
	
	if player.leaderstats.Points.Value >= price then--Check to see if the player has enough money
		
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - price--Subtracts the price
		
		local gear = shopItems[item[item]]:Clone()--Clones the item they bought
		gear.Parent = player.Backpack--Put the clone in their backpack
		
		
		return true
	else
			return false
	
	end
end)

So the way this ServerScript works is that it starts with an OnServerInvoke event on the CheckSale RemoteEvent. Inside it, it defines the price of the item (price), it then checks if the player has enough money, then it subtracts the price from the amount of money the player has, then clones the item, which is defined at function(player,*item*) into the players backpack. Lastly, the function returns true. If the player doesn’t have enough money, then it returns false.

In other words, it’s a thing to check if the player has enough money to give them an item, and item was defined initially on the function.

1 Like

Does the first script makes sense (local script)? It’s not updating selectedItem.Value

Yep. All it does is to see if selectedItem.Value exists and use it for the name of the object (not sure where it points to), then it calls the CheckSale RemoteFunction using selectedItem.Value for the item to buy and see whether or not it returns true. If it does it will print “Purchased!”, otherwise it would print “Purchase Failed!”.