My local script won't properly take values from my module script

Two posts back to back… yikes lol

I’m trying to make a game and I’m using a module script to store player stats.
One of my scripts is editing a value inside, and I’m trying to call that same value from a local script in a shop ui.
Here’s some of my code, these are just snippets:

Module script
image

local module = {}

module.Stats = {

	["Power"] = 1,
	["Money"] = 0,

}

return module

Script that changes the value of money (the full script is too long so just assume I’ve called everything correctly here:
image

local ClickDetector = script.Parent:FindFirstChild("ClickDetector")
local Health = script.Parent.Parent.Health
local OreData = require(game.ReplicatedStorage.OreData)
local PlayerStats = require(game.ReplicatedStorage.PlayerStats)
local ore = script.Parent
local billboard = script.Parent:FindFirstChild("Billboard")
local Debounce = false

-- unrelated lines of code here

local function OreMined()
	print(script.Parent.Parent.Name .. " Mined")
	PlayerStats.Stats["Money"] += OreData.Data[ore.Parent.Name]["Value"]
	print(PlayerStats.Stats["Money"])
end

local function onMouseClick()
	
	if Health.Value > 0 and Debounce == false then
		
		Health.Value -= Power
		
		if Health.Value <= 0 then
			Health.Value = 0
			OreMined()
			Debounce = true
			RegenerateOre()
		end
		UpdateDisplay()
	end
end

Shop script (this one is giving me a lot of issues)
image

local ShopButton = script.Parent
local ShopUI = ShopButton.ShopUI
local PlayerStats = require(game.ReplicatedStorage.PlayerStats)

local function ShopButtonPressed()
	if ShopUI.Visible == false then
		ShopUI.Visible = true
	else
		ShopUI.Visible = false
	end
end

local function round(n)
	return math.floor(n + 0.5)
end

ShopButton.MouseButton1Down:Connect(ShopButtonPressed)

local Upgrade1 = ShopUI.ScrollingFrame.Upgrade1
local Purchase1 = Upgrade1.Purchase1

local function Purchase1Pressed()
	print "Buy button pressed"
	local StartingAmount = 10
	local Increase = 1.2

	local RequiredAmount = StartingAmount

	print ("Required Amount " .. RequiredAmount)
	print ("Player has ".. PlayerStats.Stats["Money"])

	Upgrade1.Cost.Text = "$" .. RequiredAmount

	if tonumber(PlayerStats.Stats["Money"]) >= tonumber(RequiredAmount) then

		print "Has enough money"

		PlayerStats.Stats["Power"] += 1

		PlayerStats.Stats["Upgrade1"] += 1
		PlayerStats.Stats["Money"] -= RequiredAmount
		RequiredAmount *= Increase
		round(RequiredAmount)
		Upgrade1.Cost.Text = "$" .. RequiredAmount
	else
		print "Not enough money"
	end
end

Purchase1.MouseButton1Down:Connect(Purchase1Pressed)

I have a script running in ServerScriptService printing the amount of money the player has at all times. Here’s the code if it’s relavent:
image

local PlayerStats = require(game.ReplicatedStorage.PlayerStats)

while true do
	print(PlayerStats.Stats["Money"])
	wait(1)
end

Another relevant script that displays the amount of money the player has:
image

local PlayerStats = require(game.ReplicatedStorage.PlayerStats)
local RunService = game:GetService("RunService")


while true do
	script.Parent.Text = ("Money: " .. tostring(PlayerStats.Stats["Money"]))
	RunService.Heartbeat:Wait()
end

As expected, it starts out by printing 0s, once I mine the first ore (set to $11 for debug purposes) it seems to be outputting $11. The money display also shows the updated value. I have debug print statements inside the shop script that was broken and they’re saying that I don’t have enough money.

image

I know this is probably an issue with the way I’m dealing with local and server sided scripts. I have no idea what to do here though and it’s my first time trying to make a solo game. Any help at all would be greatly appreciated! :slight_smile:

EDIT: Changing the shop script from a local script to a regular script fixed the issue, but this means that it’s all server sided. I’m not sure how to handle all of this from the player’s side.

If you are updating the module script values from a server script, It will not mirror that to the client side. So, I recommend using RemoteFunctions. Good luck with your game!

1 Like

Basically Module Scripts aren’t replicated across Roblox’s Client-Server boundary.

Ah I see the issue, I’ll have to hunt down a tutorial on using RemoteFunctions then, thanks so much for the help!

No problem! Heres how you use a RemoteFunction:

Invoking Side:

local value = remoteFunction:InvokeServer() – invoking with a variable is optional but its good for storing the return value

Returning Side:

remoteFunction.OnServerInvoke = function(player)
return thevalueyouwanttoreturn – optional but useful
end

1 Like