I’m currently building a primitive inventory system, where when an object is clicked, a value changes corresponding to how many of that object the player has clicked. This acts as a very basic inventory system that I will further develop.
This is the object that when clicked, fires a RemoteEvent, which is placed in ReplicatedStorage. The object is then destroyed immediately as the RemoteEvent is fired.
Script (Server Side)
local Fruit_Name = Fruit.Name
local ClickDetector = script.Parent.ClickDetector
local Harvest_Sound = game.Workspace.Harvest_Sound
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("Harvest")
local function Call_Harvest(class)
RemoteEvent:FireClient(class, "Durian") --Firing the remote event when function is run and sending the name of fruit
Harvest_Sound:Play()
Fruit:Destroy()
end
ClickDetector.MouseClick:Connect(Call_Harvest) --Connecting the function when object is clicked```
Afterwards, a script named “Harvest_Handler” inside the StarterPlayer folder is activated, and adds 1 to the value of whatever object was picked up. The values are located in a folder under a player’s character named “Inventory_Values”.
Local Script (Client Side)
--Global player variables:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game:GetService("Players").LocalPlayer
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("Harvest")
local Character = Player.Character or Player.CharacterAdded:Wait()
local Amount_Folder = Character:FindFirstChild("Inventory_Values")
--Accessing the player gui variables:
local Player_GUI = Player.PlayerGui
local Local_Stats_GUI = Player_GUI:WaitForChild("Stats_GUI")
--Accessing the different fruit labels:
local Banana_Label = Local_Stats_GUI.Inventory.ScrollingFrame.Banana
local Orange_Label = Local_Stats_GUI.Inventory.ScrollingFrame.Orange
local Apple_Label = Local_Stats_GUI.Inventory.ScrollingFrame.Apple
local Plum_Label = Local_Stats_GUI.Inventory.ScrollingFrame.Plum
local Durian_Label = Local_Stats_GUI.Inventory.ScrollingFrame.Durian
--All the inventory values:
local Banana_Amount = Amount_Folder:FindFirstChild("Banana_Amount").Value
local Orange_Amount = Amount_Folder:FindFirstChild("Orange_Amount").Value
local Apple_Amount = Amount_Folder:FindFirstChild("Apple_Amount").Value
local Plum_Amount = Amount_Folder:FindFirstChild("Plum_Amount").Value
local Durian_Amount = Amount_Folder:FindFirstChild("Durian_Amount").Value
--The main function that adds the values for each fruit value:
RemoteEvent.OnClientEvent:Connect(function(class)
if class == "Banana" then
Banana_Amount = Banana_Amount + 1
elseif class == "Orange" then
Orange_Amount = Orange_Amount + 1
elseif class == "Apple" then
Apple_Amount = Apple_Amount + 1
elseif class == "Plum" then
Plum_Amount = Plum_Amount + 1
elseif class == "Durian" then
Durian_Amount = Durian_Amount + 1
print(Durian_Amount)
print("...")
end
end)
I have tried multiple times using print statements to identify whether or not certain variables are referring to the right instance, but nothing helps fix the problem at hand. Any help or suggestions are greatly appreciated!