So currently I have a system in a module script where if a player joins it will give the items to a inventory, now this works, but now I am stuck, Originally I had an inventory when opened you can equip your weapon, armour and potions. So thinking on this I want get the inventory done right, so the inventory in my game is you open it and you can equip, now when you go to sell your items, would it open the same inventory? or would it open a new one with duplicates? Currently I have this:
As you can see the inventory will open up, and the camera would change showing your character on the left, the coloured in squares represent boxes, the top one is armour, the middle on is weapon, and the bottom one is moves and potions. Now this is the really messy code I currently have:
add.AddedEvent = function(item, class, player, equiptable, serial) -- Starting the game and gaining something like a sword (class - Weapon/Potion/Armour)
local items = ReplicatedStorage[class]:GetChildren()
if item then
for num, rarity in pairs(script.Rarities:GetChildren()) do
if item.Stats.Rarity.Value == rarity.Name then
if equiptable == serial then
local rare = rarity:Clone() -- Clone the rarity ui
local clone = item:Clone() -- Cloning the tool
clone.Parent = rare.ItemView -- Setting the tool to the viewportFrame
local bp = item:Clone()
bp.Parent = player.Backpack
local Camera = Instance.new("Camera") -- Creating a Camera
rare.ItemView.CurrentCamera = Camera -- Setting the viewports CurrentCamera to the new one
Camera.Parent = rare.ItemView -- The camera parent is the viewportFrame
Camera.CFrame = CFrame.new(item.Handle.Position + Vector3.new(6, 4, -1), item.Handle.Position)--Camera.CFrame = CFrame.new(Vector3.new(0,3,0), v.Handle.Position)-- Position of the camera
rare.Parent = player.PlayerGui.Inventory.Inventory
rare:TweenPosition(UDim2.new(-1.015, 0, 0.771, 0),"Out","Quart",0.1) -- Side of the screen
print("Added "..item.Name.." to your inventory")
local animation = clone.AttackScript.Animation.Id.Value
local Tool = item:Clone() -- Clones the item
Tool.Parent = player.Character -- Gives it to player
Tool.Name = "EquippedWeapon"
local char = player.Character
char.Animate.toolnone.ToolNoneAnim.AnimationId = "http://www.roblox.com/asset/?id="..animation
else
local rare = rarity:Clone() -- Clone the rarity ui
local clone = item:Clone() -- Cloning the tool
clone.Parent = rare.ItemView -- Setting the tool to the viewportFrame
local bp = item:Clone()
bp.Parent = player.Backpack
local Camera = Instance.new("Camera") -- Creating a Camera
rare.ItemView.CurrentCamera = Camera -- Setting the viewports CurrentCamera to the new one
Camera.Parent = rare.ItemView -- The camera parent is the viewportFrame
Camera.CFrame = CFrame.new(item.Handle.Position + Vector3.new(6, 4, -1), item.Handle.Position)--Camera.CFrame = CFrame.new(Vector3.new(0,3,0), v.Handle.Position)-- Position of the camera
rare.Parent = player.PlayerGui.Inventory.Inventory[class.."Inv"]
print("Added "..item.Name.." to your inventory")
end
end
end
end
end
This is a really messy script, and makes me want to start this script over again, this script is when a player is added it will create the box in the inventory, and if they had it last equip it will remain on the left side, for some reason this seems that it can be done with a lot less code. This is the code that calls on this module:
give.Give = function(tool, tablearea, player, equiptable) -- Tool being weapon/armour/potions / Tablearea being Weapons.WeaponTable
for num, item in pairs(ReplicatedStorage[tool]:GetChildren()) do
for i,itemData in pairs(tablearea) do
if item.Stats.Serial.Value == i and itemData.Quantity >= 1 then -- if they own it // The value in quantity is 0
for i = 1,itemData.Quantity do
adding.AddedEvent(item, tool, player, equiptable[tool], i)
end
end
end
end
end
As you can see this one is a lot more pleasing to look at, it is nice a simple. The fuction is used in the datastoreHandler which calls on this function when a player joins. So this what I currently have when a player clicks on the one of the inventory weapons:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Equip_Event = ReplicatedStorage:WaitForChild("Events"):WaitForChild("Equip")
local UnEquip_Event = ReplicatedStorage:WaitForChild("Events"):WaitForChild("UnEquip")
local player = game:GetService("Players").LocalPlayer
-- Event :FireServer
script.Parent.MouseButton1Click:Connect(function()
local item2 = script.Parent.Parent.ItemView:GetChildren()
for i, v in pairs(item2) do
if v:IsA("Tool") then
for _, c in pairs(v:GetChildren()) do
if c:IsA("Script") then
local tool = v
local animation = c:WaitForChild("Animation"):WaitForChild("Id").Value
local frame = script.Parent.Parent
local char = player.Character
Equip_Event:FireServer(tool, animation, frame)
end
end
end
end
end)
Is there a way I can make this more efficient maybe using one script? Currently this is a local script inside a button, and I believe this will just use a lot of unnecessary memory, if a player was to have a lot of weapons.
Sorry for throwing code out there, I just want to know is there a better way of approaching this with keeping it clean, and less memory intensive. Also, would this be the best way to add it to the inventory, I was think of using :EquipTool(tool) which I tried, but I’m not sure how to check if there is a tool equipped, right now my system creates a clone of the tool and calls it “EquippedWeapon”.
Usually when I have to go off I create a todo list of what I will do the next time im on;
When player joins it should get the current frame of the item they have and move it to the left.
Fix the issue with viewport frame showing a sword really far away.
Make Unequip so:
issue with player attacking another player. Should not happen
if player is in players then can't attack something like this maybe
UpdateData module needs fixing issue with it.
These are still issue I need to fix, but I will fix them when rewriting my code.
So these are my questions:
(Some of these questions where in the explanation it’s self, so please read them)
Is this the best possible way to do this?
Since I am going to rewrite this regardless, is there anything I should do more efficiently then before?
Also for moves, how does this work exactly? I know you’ll probably need a local script in the StarterPlayer, but would you need a module which contains a list of moves? How would these get added to inventory? I know there is tutorial on moves, but not selectable ones.
Adding armour how does this exactly through welds? or through replacing separate body parts?
So from the todo list, is there anything I could Implement to check if It’s attacking a player, and make it unequip? I’ll show you my current sword attack script:
tool.Activated:Connect(function()
local humanoid = tool.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then --Prevent any delayed replication errors
if not debounce and swing1 then
swing1 = false
activated = true
debounce = true
local anim = humanoid:LoadAnimation(Animation)
anim:Play()
sound:Play()
wait(cooldown)
debounce = false
anim:Stop()
elseif not swing1 and not debounce then
swing1 = true
activated = true
debounce = true
local anim = humanoid:LoadAnimation(Animation2)
anim:Play()
sound:Play()
wait(cooldown)
debounce = false
anim:Stop()
end
end
end)
script.Parent.Handle.DamagePart.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and activated then
humanoid:TakeDamage(Damage)
DmgInd.indicators(Damage, hit)
activated = false
end
end)
Thank you, if you did read this all I really Appreciate it, and would like some answers, Thank you. Sorry for the lengthy post. If you need to ask me any questions I will be happy to answer them! Sorry for asking so many questions, I just want to get everything right when doing making this game.