IntValue doens't change in server-side, but does in client-side

Hello! So, I have a store system and a currency system, the “problem” I’m having is that the currency, when used in the store, is only spent on the client-side (Because, the script that manages this is a LocalScript), but not on the server-side. Which is a big problem for datastore etc. I want to know how I could affect server-side spending as well. Thanks!

ShopConfigurations

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ShopSystem = ReplicatedStorage.ShopSystem
local ShopItems = ShopSystem.ShopItems

local ShopModule = {
	
	Grasslands = {
		ItemOne = {
			ItemOne = ShopItems.Grasslands.ToolOne;
			Cost = 250
		};
		
		ItemTwo = {
			ItemTwo = ShopItems.Grasslands.ToolTwo;
			Cost = 300
		};
		
		ItemThree = {
			ItemThree = ShopItems.Grasslands.ToolThree;
			Cost = 350
		};
		
		ItemFour = {
			ItemFour = ShopItems.Grasslands.ToolFour;
			Cost = 400
		};
		
		ItemFive = {
			ItemFive = ShopItems.Grasslands.ToolFive;
			Cost = 450
		};
		
		ItemSix = {
			ItemSix = ShopItems.Grasslands.ToolSix;
			Cost = 500
		};
		
		ItemSeven = {
			ItemSeven = ShopItems.Grasslands.ToolSeven;
			Cost = 550
		};
		
		ItemEight = {
			ItemEight = ShopItems.Grasslands.ToolEight;
			Cost = 600
		};
		
		ItemNine = {
			ItemNine = ShopItems.Grasslands.ToolNine;
			Cost = 650
		};
		
		ItemTen = {
			ItemTen = ShopItems.Grasslands.ToolTen;
			Cost = 700
		}
	}
}

return ShopModule

ShopHandle

--Variables.
	local ShopGui = script.Parent.Parent
	local Frame = ShopGui.Frame
	local PlayerService = game:GetService("Players")
--
	local ShopConfigurations = ShopGui.ShopConfigurations
	local ShopModule = require(ShopConfigurations)
--
	local BuyButtonTable = {
		BuyButtonOne = Frame:FindFirstChild("BuyFrameOne");
		BuyButtonTwo = Frame:FindFirstChild("BuyFrameTwo");
		BuyButtonThree = Frame:WaitForChild("BuyFrameThree");
		BuyButtonFour = Frame:FindFirstChild("BuyFrameFour");
		BuyButtonFive = Frame:FindFirstChild("BuyFrameFive");
		BuyButtonSix = Frame:FindFirstChild("BuyFrameSix");
		BuyButtonSeven = Frame:FindFirstChild("BuyFrameSeven");
		BuyButtonEight = Frame:FindFirstChild("BuyFrameEight");
		BuyButtonNine = Frame:FindFirstChild("BuyFrameNine");
		BuyButtonTen = Frame:FindFirstChild("BuyFrameTen")
		
	}
--
	local PlayerService = game:GetService("Players")
	local LocalPlayer = PlayerService.LocalPlayer
	local PlayerBackpack = LocalPlayer.Backpack
--
	local CurrencyFolder = LocalPlayer:WaitForChild("CurrencyFolder")
--
	local GoldCurrency = CurrencyFolder:WaitForChild("GoldCurrency")
	local GoldCurrency = CurrencyFolder:WaitForChild("GoldCurrency")
	
--

BuyButtonTable.BuyButtonOne.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemOne.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemOne.Cost
		
		BuyButtonTable.BuyButtonOne.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonOne.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonOne.Interactable = false
		
		local ToolClone = ShopModule.Grasslands.ItemOne.ItemOne:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

BuyButtonTable.BuyButtonTwo.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemTwo.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemTwo.Cost
		
		BuyButtonTable.BuyButtonTwo.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonTwo.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonTwo.Interactable = false

		local ToolClone = ShopModule.Grasslands.ItemTwo.ItemTwo:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

BuyButtonTable.BuyButtonThree.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemThree.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemThree.Cost
		
		BuyButtonTable.BuyButtonThree.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonThree.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonThree.Interactable = false

		local ToolClone = ShopModule.Grasslands.ItemThree.ItemThree:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

BuyButtonTable.BuyButtonFour.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemFour.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemFour.Cost
		
		BuyButtonTable.BuyButtonFour.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonFour.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonFour.Interactable = false
		
		local ToolClone = ShopModule.Grasslands.ItemFour.ItemFour:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

BuyButtonTable.BuyButtonFive.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemFive.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemFive.Cost
		
		BuyButtonTable.BuyButtonFive.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonFive.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonFive.Interactable = false

		local ToolClone = ShopModule.Grasslands.ItemFive.ItemFive:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

BuyButtonTable.BuyButtonSix.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemSix.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemSix.Cost
		
		BuyButtonTable.BuyButtonSix.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonSix.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonSix.Interactable = false

		local ToolClone = ShopModule.Grasslands.ItemSix.ItemSix:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

BuyButtonTable.BuyButtonSeven.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemSeven.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemSeven.Cost
		
		BuyButtonTable.BuyButtonSeven.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonSeven.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonSeven.Interactable = false
		
		local ToolClone = ShopModule.Grasslands.ItemSeven.ItemSeven:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

BuyButtonTable.BuyButtonEight.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemEight.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemEight.Cost
		
		BuyButtonTable.BuyButtonEight.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonEight.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonEight.Interactable = false
		
		local ToolClone = ShopModule.Grasslands.ItemEight.ItemEight:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

BuyButtonTable.BuyButtonNine.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemNine.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemNine.Cost
		
		BuyButtonTable.BuyButtonNine.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonNine.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonNine.Interactable = false
		
		local ToolClone = ShopModule.Grasslands.ItemNine.ItemNine:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

BuyButtonTable.BuyButtonTen.BuyButton.MouseButton1Click:Connect(function()
	if GoldCurrency.Value >= ShopModule.Grasslands.ItemTen.Cost then
		GoldCurrency.Value = GoldCurrency.Value - ShopModule.Grasslands.ItemTen.Cost
		
		BuyButtonTable.BuyButtonTen.OwnedLabel.Transparency = 0.3
		BuyButtonTable.BuyButtonTen.OwnedLabel.TextTransparency = 0
		BuyButtonTable.BuyButtonTen.Interactable = false

		local ToolClone = ShopModule.Grasslands.ItemTen.ItemTen:Clone()
		ToolClone.Parent = PlayerBackpack
	end
end)

PrincipalCurrency

--Variables.
	local PlayerService = game:GetService("Players")
	
	local DataStoreService = game:GetService("DataStoreService")
	local Data = DataStoreService:GetDataStore("Data")
--

--Functions.
	PlayerService.PlayerAdded:Connect(function(Player)
		
		local CurrencyFolder = Instance.new("Folder")
		CurrencyFolder.Name = "CurrencyFolder"
		CurrencyFolder.Parent = Player
		
		local GoldCurrency = Instance.new("IntValue")
		GoldCurrency.Name = "GoldCurrency"
		GoldCurrency.Parent = CurrencyFolder
		
		local GemCurrency = Instance.new("IntValue")
		GemCurrency.Name = "GemCurrency"
		GemCurrency.Parent = CurrencyFolder
		
		---------------------------------------------
		
		local Data = Data:GetAsync(Player.UserId)

		if Data then
			if Data[1] and Data[2] then
				GoldCurrency.Value = Data[1]
				GemCurrency.Value = Data[2]
			end
		end
		
		GoldCurrency.Changed:Connect(function()
			GoldCurrency. Value = math.clamp(GoldCurrency.Value, 0, 10000)
		end)
		
		GemCurrency.Changed:Connect(function()
			GemCurrency. Value = math.clamp(GemCurrency.Value, 0, 5000)
		end)

	end)
	
	PlayerService.PlayerRemoving:Connect(function(Player)
		
		local CurrencyFolder = Player:WaitForChild("CurrencyFolder")

		local GoldCurrency = CurrencyFolder:WaitForChild("GoldCurrency")
		local GemCurrency = CurrencyFolder:WaitForChild("GemCurrency")

		Data:SetAsync(Player.UserId, {GoldCurrency.Value, GemCurrency.Value})
	end)
--

This sort of inquiry is known as Client->Server communication. You want the server to replicate change from the client into the server for usage such as Client->Server replication, data storage, etc.

The way to go about this is to use a RemoteEvent.

To setup a remote event, you have to first create the remote event object in a service where both the client and the server has access to; the most common choice is ReplicatedStorage (additional information).

After which, reference the event and fire it to the server whenever you need change or action to occur on the server; you can also include additional arguments into the event that you wish to provide to the server (such as client-side values). It is important to remember that firing to the server can only occur in a LocalScript!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local changeColourEvent = ReplicatedStorage:FindFirstChild("ChangeColour")

local oldColour = "Red"
local newColour = "Blue"

changeColourEvent:FireServer(oldColour, newColour)

Then, you need to receive the call from the event in a server script, from which you can do whatever you need from the server. It is important to remember that the player object is attached alongside all other arguments when a call is received by default, and it is also the first argument attached, therefore, reference the player first then all other arguments sent (in order).

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local changeColourEvent = ReplicatedStorage:FindFirstChild("ChangeColour")

changeColourEvent.OnServerEvent:Connect(function(player, oldColour, newColour)
    print(oldColour)
    print(newColour)
    -- Anything else that is needed to be done on the server
end)

That pretty much summarises the idea of Client->Server communication, and hopefully, this should give you a general idea on how to approach your problem. I avoided going into the other functions and purposes of remote events, such as Client->Server->Client and Server->Client->Server communication for the sake of providing relevant information to assist you in this problem, however, for future reference, you can find more information about remote events and the concept of the Client-Server boundary here:

3 Likes

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