Integer Value doesn't change when performed arithmetic in a server-side script

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!

image

This seems to be all over the place. The reason your IntValue isn’t changing is because you’re referencing the Value in your variables, not the object.

For example, use:

local Durian_Amount = Amount_Folder:FindFirstChild("Durian_Amount")
Durian_Amount.Value += 1

instead of:

local Durian_Amount = Amount_Folder:FindFirstChild("Durian_Amount").Value
Durian_Amount = Durian_Amount + 1

I also don’t see the reason you need to use a RemoteEvent here. You can change values from the server, and they’ll replicate automatically to clients.

1 Like

The reason why I use a RemoteEvent is because I have to access the values of a specific player’s character. I can’t access the LocalPlayer nor the LocalPlayer’s character on the server. Maybe I’m wrong though I just started scripting again and I’m pretty rusty :sweat_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.