I have a inventory script which works in studio when I play test, but when I test in the actual game the script doesnt work well and mostly breaks and does not return any errors.
In studio:
https://gyazo.com/801e710f6d1e69d1d16c47be22d5624chttps://i.gyazo.com/801e710f6d1e69d1d16c47be22d5624c.gif(image larger than 10 MB)
In actual game:
https://gyazo.com/8c081d572902988b5ac0f706653008fehttps://i.gyazo.com/8c081d572902988b5ac0f706653008fe.gif(image larger than 10 MB)
-- client
local function equipItem(i)
if i then
if vm then
vm:Destroy()
vm = nil
_G.vm = nil
renderVm = false
end
vm = mapData.Viewmodels[i.Name]:Clone()
vm.Parent = camera
_G.vm = vm
renderVm = true
currentItem = i
end
end
local function unequipItem(i)
if i then
if vm then
vm:Destroy()
vm = nil
_G.vm = nil
renderVm = false
end
renderVm = false
currentItem = nil
end
end
lmb.OnGlobalClick:Connect(function(clicked)
if clicked:IsA("ImageButton") and clicked.Parent:FindFirstAncestor("holder") then
if character.gameData.inventory[clicked.Parent.Name].Value ~= nil then
if clicked.equipped.Value == false then
remotes.equipItem:FireServer(character.gameData.inventory[clicked.Parent.Name].Value)
for _, equipped in pairs(UI.inventory.holder:GetDescendants()) do
if equipped:IsA("BoolValue") then
equipped.Value = false
end
end
clicked.equipped.Value = true
currentItem = character.gameData.inventory[clicked.Parent.Name].Value
equipItem(character.gameData.inventory[clicked.Parent.Name].Value)
else
if clicked.equipped.Value == true then
remotes.unequipItem:FireServer(character.gameData.inventory[clicked.Parent.Name].Value)
clicked.equipped.Value = false
currentItem = nil
unequipItem(character.gameData.inventory[clicked.Parent.Name].Value)
end
end
else
if clicked.equipped.Value == false then
for _, equipped in pairs(UI.inventory.holder:GetDescendants()) do
if equipped:IsA("BoolValue") and equipped.Value == true then
remotes.unequipItem:FireServer(character.gameData.inventory[equipped.Parent.Parent.Name].Value)
unequipItem(character.gameData.inventory[equipped.Parent.Parent.Name].Value)
end
end
clicked.equipped.Value = true
currentItem = character.gameData.inventory[clicked.Parent.Name].Value
remotes.equipItem:FireServer(character.gameData.inventory[clicked.Parent.Name].Value)
equipItem(character.gameData.inventory[clicked.Parent.Name].Value)
end
end
end
end)
rmb.OnGlobalClick:Connect(function(clicked)
if clicked:IsA("ImageButton") and clicked.Parent:FindFirstAncestor("holder") then
if character.gameData.inventory[clicked.Parent.Name].Value ~= nil then
if currentItem == character.gameData.inventory[clicked.Parent.Name].Value then
clicked.equipped.Value = false
unequipItem(character.gameData.inventory[clicked.Parent.Name].Value)
remotes.dropItem:FireServer(character.gameData.inventory[clicked.Parent.Name].Value)
else
remotes.dropItem:FireServer(character.gameData.inventory[clicked.Parent.Name].Value)
end
end
end
end)
-- server
remotes.equipItem.OnServerEvent:Connect(function(player, item)
if item then
if loadedAnim then
loadedAnim:Stop()
end
for _, tool in pairs(player.Character:GetChildren()) do
if tool:IsA("Tool") and item ~= tool then
player.Character.Humanoid:UnequipTools(tool)
tool.Parent = inv[player.Name]
if player.Character.RightHand.Motor6D.Part1 == tool.PrimaryPart then
player.Character.RightHand.Motor6D:Destroy()
end
end
end
remotes.equipItem:FireClient(player, item)
player.Character.Humanoid:EquipTool(item)
item.Parent = player.Character
item.PrimaryPart.CanCollide = false
local joint = Instance.new("Motor6D", player.Character.RightHand)
joint.Part0 = player.Character.RightHand
joint.Part1 = item.PrimaryPart
loadedAnim = player.Character.Humanoid:LoadAnimation(item.hold)
loadedAnim:Play()
end
end)
remotes.unequipItem.OnServerEvent:Connect(function(player, item)
if item then
for _, items in pairs(player.Character:GetChildren()) do
if items:IsA("Tool") then
items.Parent = inv[player.Name]
end
end
if player.Character.RightHand:FindFirstChild("Motor6D") then
player.Character.RightHand.Motor6D:Destroy()
player.Character.Humanoid:UnequipTools(item)
end
loadedAnim:Stop()
end
end)
remotes.dropItem.OnServerEvent:Connect(function(player, item)
if item then
if player.Character.RightHand:FindFirstChild("Motor6D") then
if player.Character.RightHand.Motor6D.Part1 == item.PrimaryPart then
player.Character.RightHand.Motor6D:Destroy()
if loadedAnim then
loadedAnim:Stop()
end
end
end
for _, value in pairs(player.Character.gameData.inventory:GetChildren()) do
if value:IsA("ObjectValue") then
if value.Value == item then
value.Value = nil
end
end
end
item.Parent = mapData.Items
item.PrimaryPart.CanCollide = true
item:SetPrimaryPartCFrame(player.Character.PrimaryPart.CFrame)
end
end)