Untitled

 avatar
unknown
lua
3 months ago
2.2 kB
20
Indexable
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local commonFunctions = require(ServerScriptService.server.commonFunctions)
local itemFunctions = require(ServerScriptService.server.itemFunctions)
local playerFunctions = require(ServerScriptService.server.playerFunctions)
local stationFunctions = require(ServerScriptService.server.stationFunctions)
local components = require(ReplicatedStorage.shared.components)
return function(world)
	local _, restaurant = world:query(components.Restaurant)()
	if restaurant == nil then return end
	for stationId, stationPickup, usedBy in world:query(components.StationPickup, components.UsedBy) do
		world:remove(stationId, components.UsedBy)
		local stationItemContext = commonFunctions.getItemContext(world, stationId)
		local userItemContext = commonFunctions.getItemContext(world, usedBy.id)
		if userItemContext.item and userItemContext.itemTree then
			if world:contains(userItemContext.hasItem.id) and world:get(userItemContext.hasItem.id, components.Extinguisher) then continue end
			if stationItemContext.item and stationItemContext.itemTree then
				local inserted = itemFunctions.insertNode(
					world,
					restaurant.nodes.items,
					stationItemContext.itemTree.tree,
					userItemContext.itemTree.tree
				)
				if not inserted then
					commonFunctions.swapItems()(world, stationId, usedBy.id)
					commonFunctions.playSwapSound()(world, stationId)
				else
					world:remove(usedBy.id, components.HasItem)
				end
			else
				commonFunctions.swapItems()(world, stationId, usedBy.id)
				commonFunctions.playSwapSound()(world, stationId)
			end
		else
			if stationItemContext.item and stationItemContext.itemTree then
				commonFunctions.swapItems()(world, stationId, usedBy.id)
				commonFunctions.playSwapSound()(world, stationId)
			else
				local newItemId = world:spawn()
				stationFunctions.spawnItem(world, newItemId, stationPickup.components)
				world:insert(usedBy.id, components.HasItem({id = newItemId}))
				local player = world:get(usedBy.id, components.Player)
				if player then
					playerFunctions.awardPoints(world, player.player, 5)
				end
			end
		end
	end
end
Editor is loading...
Leave a Comment