Hello!
I believe this is my fifth post here. Anyways, I want to try attempt to make an inventory system for my Hello Neighbor type game. (don’t worry the game isn’t identical). I have code written in my local script and server script.
Let’s also begin with the Gui.
Here’s the Gui.
Here’s the Gui’s descendants.
I’m trying to make it so when you equip a tool, it’s put in the WorldModel, and each time you unequip a tool, its removed from the WorldModel and put in the workspace.Tools folder.
Now, lets go to the server script.
Here’s the EquipTools script. It’s located in StarterCharacterScripts
local tvt = script.Parent:WaitForChild("ThrowObjectEvent")
local evt = script.Parent:WaitForChild("EquipToolsEvent")
local etp = script.Parent:WaitForChild("EquippedToolPos")
local eqt
local function putToolInInv(plr, tool, slot)
local gui = plr.PlayerGui.Inventory
local slots = {gui.Slot1, gui.Slot2, gui.Slot3, gui.Slot4}
local clonedTool = tool:Clone()
clonedTool.Parent = slots[slot]
clonedTool.CFrame = CFrame.new(0, 0, 0)
clonedTool.Anchored = true
end
local function equipTool(plr, mse, tgt, hit)
if tgt.Parent.Name == "Tools" and etp.Parent.ToolEquipped.Value == false and (plr.Character.HumanoidRootPart.Position - hit.p).magnitude <= 8 then
local tool = tgt
local weld = Instance.new("WeldConstraint", etp)
eqt = tool
etp.Parent.ToolEquipped.Value = true
tool.Parent = etp
tool.CFrame = etp.CFrame
weld.Part0 = etp
weld.Part1 = tool
for i, v in pairs(tool:GetDescendants()) do
if v:IsA("MeshPart") or v:IsA("BasePart") then
v.CanCollide = false
end
end
putToolInInv(plr, tool, 1)
end
end
evt.OnServerEvent:Connect(function(plr, mse, tgt, hit)
if tgt then
equipTool(plr, mse, tgt, hit)
end
plr.Character.Humanoid.Died:Connect(function()
if etp.Parent.ToolEquipped.Value == true then
for i, v in pairs(etp:GetChildren()) do
if v:IsA("BasePart") or v:IsA("MeshPart") then
v.Parent = workspace.Tools
for i, v in pairs(eqt:GetDescendants()) do
if v:IsA("MeshPart") or v:IsA("BasePart") then
v.CanCollide = true
end
end
eqt = nil
etp:ClearAllChildren()
etp.Parent.ToolEquipped.Value = false
end
end
else
etp:ClearAllChildren()
end
end)
end)
tvt.OnServerEvent:Connect(function(plr, mse, tgt, hit, thw)
if thw and etp.Parent.ToolEquipped.Value == true then
eqt.Parent = workspace.Tools
etp:ClearAllChildren()
for _, v in pairs(eqt:GetDescendants()) do
if v:IsA("MeshPart") or v:IsA("BasePart") then
v.CanCollide = true
end
end
local throwDirection = (hit.p - etp.Position).unit
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = throwDirection * 48
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.P = 5000
bodyVelocity.Parent = eqt
task.wait(.1)
bodyVelocity:Destroy()
eqt = nil
etp.Parent.ToolEquipped.Value = false
elseif etp.Parent.ToolEquipped.Value == true then
eqt.Parent = workspace.Tools
etp:ClearAllChildren()
for i, v in pairs(eqt:GetDescendants()) do
if v:IsA("MeshPart") or v:IsA("BasePart") then
v.CanCollide = true
end
end
eqt = nil
etp.Parent.ToolEquipped.Value = false
end
end)
Now the local script, also located in StarterCharacterScripts
local plr = game:GetService("Players").LocalPlayer
local uis = game:GetService("UserInputService")
local mse = plr:GetMouse()
local tse = script.Parent.InventoryEvent
local ticket = 0
local timeHeld = .4
local isHolding = false
local debugMode = workspace.debugMode.Value
function btnHeld()
isHolding = true
end
function btnRels()
isHolding = false
script.Parent.ThrowObjectEvent:FireServer(mse, mse.Target, mse.Hit, false)
end
uis.InputBegan:Connect(function(i, g)
if not g then
if i.KeyCode == Enum.KeyCode.F or i.KeyCode == Enum.KeyCode.ButtonY then
script.Parent.EquipToolsEvent:FireServer(mse, mse.Target, mse.Hit)
elseif i.UserInputType == Enum.UserInputType.MouseButton2 or i.KeyCode == Enum.KeyCode.ButtonX then
ticket = ticket + 1
local currentTicket = ticket
if timeHeld > 0 then
delay(timeHeld, function()
if ticket == currentTicket then
btnHeld()
script.Parent.ThrowObjectEvent:FireServer(mse, mse.Target, mse.Hit, true)
end
end)
else
btnHeld()
end
elseif i.KeyCode == Enum.KeyCode.One then
tse:FireServer(1)
elseif i.KeyCode == Enum.KeyCode.Two then
tse:FireServer(2)
elseif i.KeyCode == Enum.KeyCode.Three then
tse:FireServer(3)
elseif i.KeyCode == Enum.KeyCode.Four then
tse:FireServer(4)
end
end
end)
uis.InputEnded:Connect(function(i, g)
if not g then
if i.UserInputType == Enum.UserInputType.MouseButton2 or i.KeyCode == Enum.KeyCode.ButtonX then
ticket = ticket + 1
btnRels()
end
end
end)
Alright, here’s a summary of what I would like.
1, 2, 3 and 4 on the keyboard change between equipped tools. (same with the scrollwheel if possible)
The tools are stored in workspace.Tools
That’s pretty much it. Would be nice if you guys on the Forum helped me out!