I am trying to make a grid inventory, and I got stuck on rotation.
The rotation itself works, but the tile claim doesn’t work.
I used this as my base: [Open Source] Grid Based Inventory Example
Here are my scripts:
Client:
local isDragging = false
local vals = require(script.Parent.Parent:WaitForChild("SharedValues"))
local uis = game:GetService("UserInputService")
local dragicon = script.Parent.Parent.Parent:WaitForChild("InvUI"):WaitForChild("ILDragIcon")
local __oldDraggedObject, __previousInventoryFrame, __previouslyRecordedMousePosition, newRotation
local oldRotation = 0
local mouse = game.Players.LocalPlayer:GetMouse()
local imenu = script.Parent.Parent.Parent.InvUI.InteractMenu
local player = game.Players.LocalPlayer
vals.BE.BEBeginDragItem.Event:Connect(function(objToDrag)
if(isDragging==false) then
isDragging = true
dragicon.Visible = true
dragicon.Size = objToDrag.Size
dragicon.Image = objToDrag.Image
dragicon.Rotation = objToDrag.Rotation
__oldDraggedObject = objToDrag
oldRotation = objToDrag.Rotation
uis.InputBegan:Connect(function(input)
if (uis:GetFocusedTextBox()) then
return; -- make sure player's not chatting!
end
if input.KeyCode == Enum.KeyCode.R then
if dragicon.Rotation == 0 then
dragicon.Rotation = 90
newRotation = 90
elseif dragicon.Rotation == 90 then
dragicon.Rotation = 0
newRotation = 0
end
end
end)
--__oldDraggedObject.Visible = falseotation
__oldDraggedObject = objToDrag
end
end)
vals.BE.BEShowMenu.Event:Connect(function(object, object2, mode)
if object2 then
if mode == "ownInventory" then
imenu.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
imenu.Visible = true
print("Showed menu!")
imenu.Selected.Value = object2.Name
end
end
end)
vals.BE.BEEndDragItem.Event:Connect(function()
if(isDragging==true) then
isDragging = false
dragicon.Visible = false
dragicon.Rotation = newRotation
--__oldDraggedObject.Visible = true
local size = dragicon.AbsoluteSize
local relitiveCoords = __previouslyRecordedMousePosition - Vector3.new(__previousInventoryFrame.AbsolutePosition.X - ((-size.X/2)+24), __previousInventoryFrame.AbsolutePosition.Y - ((-size.Y/2)+24), 0)
vals.RE.REDropItem:FireServer(__oldDraggedObject, __previousInventoryFrame, relitiveCoords, newRotation, oldRotation)
end
end)
vals.BE.BEDragOverInventory.Event:Connect(function(targetFrame)
__previousInventoryFrame = targetFrame
end)
uis.InputChanged:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local size = dragicon.AbsoluteSize
__previouslyRecordedMousePosition = input.Position
dragicon.Position = UDim2.new(0, input.Position.X - (size.X/2), 0, input.Position.Y - (size.Y/2))
end
end)
Server:
local im = require(game.ReplicatedStorage.Modules.InventoryManager)
script.Parent.Parent.Storage.RE.REDropItem.OnServerEvent:Connect(function(player, itemDropped, frameToMoveTo, relitiveCoords, rotation, oldRotation)
print("Server event received?")
local storageIndex = im.getStorageDataIndex(frameToMoveTo)
if(storageIndex~=nil) then
itemDropped.Rotation = rotation
local storageData = im.getStorageData(storageIndex)
local previousIndex = im.getStorageDataIndex(itemDropped.Parent)
local currentData = {
["storageIndex"] = previousIndex,
["data"] = im.getStorageData(previousIndex),
["pos"] = {
["x"] = math.floor(itemDropped.Position.X.Offset/48),
["y"] = math.floor(itemDropped.Position.Y.Offset/48)
},
["size"] = {
["x"] = math.floor(itemDropped.Size.X.Offset/48),
["y"] = math.floor(itemDropped.Size.Y.Offset/48)
},
["rotation"] = oldRotation
}
local newData = {
["storageIndex"] = storageIndex,
["data"] = storageData,
["pos"] = {
["x"] = math.floor(relitiveCoords.X/48),
["y"] = math.floor(relitiveCoords.Y/48)
},
["size"] = {
["x"] = math.floor(itemDropped.Size.X.Offset/48),
["y"] = math.floor(itemDropped.Size.Y.Offset/48)
},
["rotation"] = rotation
}
print("aaa-storageIndex: ", tostring(currentData.storageIndex))
print("aaa-pos: ", tostring(currentData.pos.x), "-", tostring(currentData.pos.y))
print("aaa-size: ", tostring(currentData.size.x), "-", tostring(currentData.size.y))
print("bbb-storageIndex: ", tostring(newData.storageIndex))
print("bbb-pos: ", tostring(newData.pos.x), "-", tostring(newData.pos.y))
print("bbb-size: ", tostring(newData.size.x), "-", tostring(newData.size.y))
currentData.data.data.unclaimTiles(currentData.pos.x, currentData.pos.y, currentData.size.x, currentData.size.y)
if(newData.data.data.canClaimTiles(newData.pos.x, newData.pos.y, newData.size.x, newData.size.y)) then
newData.data.data.claimTiles(newData.pos.x, newData.pos.y, newData.size.x, newData.size.y)
itemDropped.Parent = frameToMoveTo
itemDropped.Position = UDim2.new(0, newData.pos.x * 48, 0, newData.pos.y * 48)
else
itemDropped.Rotation = currentData.rotation
currentData.data.data.claimTiles(currentData.pos.x, currentData.pos.y, currentData.size.x, currentData.size.y)
end
print(itemDropped.Name)
print(frameToMoveTo.Name)
end
end)
Inventory manager module:
local module = {}
local createdStorages = {}
local storageIndexes = {}
local previousIndex = 0
module.getStorageDataIndex = function(storage)
for i, v in pairs(storageIndexes) do
if(storage==v) then
return i
end
end
return nil
end
--Get the index from module.getStorageDataIndex
module.getStorageData = function(index)
return createdStorages[index]
end
module.CreateInventoryGrid = function(parent, tilesX, tilesY, gridName)
local inventoryTab = script:WaitForChild("InventoryTab"):Clone()
local invStorage = script:WaitForChild("FrameInventory"):Clone()
local __slot = script:WaitForChild("ImgSlot")
invStorage.Size = UDim2.new(0, 48*tilesX, 0, 48*tilesY)
inventoryTab.TLTitle.Text = gridName ~= nil and tostring(gridName) or "Storage"
local data = {}
data.tiles = {}
inventoryTab.Size = UDim2.new(0,(tilesX<3 and 3 * 48 or 48*tilesX),0,24+(tilesY*48)+12)
inventoryTab.Parent = parent
invStorage.Position = UDim2.new(0,0,0,24)
invStorage.Parent = inventoryTab
storageIndexes[previousIndex] = invStorage
createdStorages[previousIndex] = {
["tab"] = inventoryTab,
["storage"] = invStorage,
["data"] = data
}
previousIndex = previousIndex+1
print("Inventories created: " .. tostring(previousIndex))
for _x = 0, tilesX-1 do
data.tiles[_x] = {}
for _y = 0, tilesY-1 do
local slot = __slot:Clone()
slot.Position = UDim2.new(0, 48*_x, 0, 48*_y)
slot.Parent = invStorage
data.tiles[_x][_y] = {
["claimed"] = false,
["frame"] = slot,
["owner"] = nil
}
end
end
--Getting the tile data
data.getTileData = function(x, y)
if(data.tiles[x]~=nil) then
if(data.tiles[x][y]~=nil) then
return data.tiles[x][y]
end
end
return nil
end
--Checking if the tile is claimed (any border tiles are automatically claimed, returning true
data.isTileClaimed = function(x, y)
local tdata = data.getTileData(x, y)
if(tdata~=nil) then
if(tdata.claimed==true) then
return true
else
return false
end
end
return true
end
data.claimTile = function(x, y, ownerdata)
local tdata = data.getTileData(x, y)
if(tdata~=nil) then
tdata.claimed = true
tdata.owner = ownerdata
tdata.frame.BackgroundColor3 = Color3.new(0.133333, 0.133333, 0.133333)
end
end
data.unclaimTile = function(x, y)
local tdata = data.getTileData(x, y)
if(tdata~=nil) then
tdata.claimed = false
tdata.owner = nil
tdata.frame.BackgroundColor3 = Color3.new(0.203922, 0.203922, 0.203922)
end
end
data.claimTiles = function(x, y, w, h, ownerdata)
for _x = 0, w-1 do
for _y = 0, h-1 do
data.claimTile(_x + x, _y + y, ownerdata)
end
end
end
data.unclaimTiles = function(x, y, w, h)
for _x = 0, w-1 do
for _y = 0, h-1 do
data.unclaimTile(_x + x, _y + y)
end
end
end
data.canClaimTiles = function(x, y, w, h)
for _x = 0, w-1 do
if(data.tiles[_x + x]==nil) then return false end
for _y = 0, h-1 do
if(data.tiles[_x + x][_y + y]==nil) then return false end
if(data.tiles[_x + x][_y + y]).claimed == true then return false end
end
end
return true
end
--Getting the tile
data.getTile = function(x, y)
local tdata = data.getTileData(x, y)
return tdata ~= nil and tdata.frame or nil
end
return data, invStorage, inventoryTab
end
return module