ModuleScript values updating to server but not client

I am trying to create a script where when the user presses a button a LocalScript fires a remote event, which updates the ModuleScript within the player’s PlayerGui. The server script works as intended and updates the value within the ModuleScript, however the value does not update for the client.

So far, I have tried using a bunch of print statements, but all that I’ve figured out is that the server script is doing what it’s supposed to, but the client is not recognizing the changes made by the server. How do I fix this?

LocalScript:

FPS.MouseButton1Click:Connect(function()
	if SettingsModule.FPS == false then
		Remote:FireServer("FPS",true)
		FPS.Text = "> FPS: " .. tostring(SettingsModule.FPS)
		print(SettingsModule.FPS)
	else
		Remote:FireServer("FPS",false)
		FPS.Text = "> FPS: " .. tostring(SettingsModule.FPS)
		wait(1) -- added this here to bug test to see if the client was printing before the server but its not
		print(SettingsModule.FPS) -- prints false, instead of true like its supposed to. the server prints "true"
	end
end)

Server Script:

Remote.OnServerEvent:Connect(function(player,var,value)
	print(player) -- no problems
	print(var) -- no problems
	print(value) -- no problems
	local SettingsModule = require(player.PlayerGui.Main.SettingsModule)
	print(SettingsModule[var]) -- prints true since the value hasnt been changed yet
	SettingsModule[var] = value
	print(SettingsModule[var]) -- prints false since it was changed
	print(SettingsModule.FPS) -- prints false since it was changed (client continues to print true)
end)

Any ideas? This has stumped me, any help is greatly appreciated.

1 Like

I am assuming that your ModuleScript is just a normal dictionary/table with key/value pairs. Regardless, when you run the game, a copy of that ModuleScript is loaded onto the clientside and another on the server side. These are two separate memory items/locations.

image

You need to change it by a client script if you want this to work. If you plan on using this SettingsModule thing for more stuff, you could make a remote that syncs up the settings table. So…

Server Code:

function onSettingsUpdated(player, newSettings)
  PathToSettingsRemote:FireClient(player, table.clone(newSettings))
end)

Remote.OnServerEvent:Connect(function(player,var,value)
	local SettingsModule = require(player.PlayerGui.Main.SettingsModule)
	SettingsModule[var] = value

    onSettingsUpdated(player, SettingsModule)
end)

Client:

PathToSettingsRemote.OnClientEvent:Connect(function(newSettings)
    SettingsModule = newSettings
end)

FPS.MouseButton1Click:Connect(function()
	if SettingsModule.FPS == false then
		Remote:FireServer("FPS",true)
		FPS.Text = "> FPS: " .. tostring(SettingsModule.FPS)
		print(SettingsModule.FPS)
	else
		Remote:FireServer("FPS",false)
		FPS.Text = "> FPS: " .. tostring(SettingsModule.FPS)
		wait(1) -- added this here to bug test to see if the client was printing before the server but its not
		print(SettingsModule.FPS) -- prints false, instead of true like its supposed to. the server prints "true"
	end
end)

Let me know how it works, I haven’t tested the code so.

5 Likes

Just had to make some changes because of the way I setup my ModuleScript, but it works. Thank you so much! Thanks for going out of your way to create that graphic btw, that really helped.

1 Like

No problem, glad I could help. If you found it particularly useful, I recommend that you mark it as a solution so that other players with similar issues can find it easier. Thanks!

3 Likes

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