Interaction between ModuleScript, Workspace and LocalScript (Editing a GUI from both workspace and a localscript)

Hello,

  1. What do you want to achieve? Keep it simple and clear!

So, I have:
• a ModuleScript inside my ServerScriptService.
• a LocalScript inside the StarterCharacterScripts
• and a Script inside my Workspace, that triggers a function when its Parent is touched

The idea is that my LocalScript is used to initialize a part of the GUI (a bottom HUD bar, that changes color depending on a StringValue)
The Script inside the Workspace, that triggers a function when its Parent Part is touched also updates the GUI in the same way as the LocalScript
That is why I’m using a ModuleScript: because I’d like to avoid repeating myself. (I’ll use the same function in other scripts too)

However…

  1. What is the issue? Include screenshots / videos if

The thing is that I’m using a Script in my workspace (LocalScripts don’t work there if I remember correctly) and my ModuleScript is in the same place as my LocalScript: inside the StarterCharacterScripts. Which of course, causes infinite yielding (yay).
So, big brain time, I’ll save my ModuleScript in the ServiceScriptService service: now the infinite yielding is inside the LocalScript (yes, I changed the require()'s argument)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Solutions that I’ve tried are mentionned up there:
• Moving the ModuleScript to ServiceScriptService

I also tried this:
• Changing my LocalScript to a regular Script, inside StarterCharacterScripts, but I will not be able to access the LocalPlayer object (which I need in the ModuleScript, because I want to change the player’s HUD)

If you need more details tell me, I tried my best to explain the original problem, and my native language is french, so please excuse any grammar mistakes.

Thank you guys for reading

Can you please attach the code snippets that would be most relevant?

I see no mention of ReplicatedStorage. Did you try moving it there? Both the server and the client can access it if it’s there. Just know that they won’t receive the same instance, as each machine (each client and the server) receive a different instance on requiring the module.

1 Like

Sure.

Here is the ModuleScript

local module = {}

function module.updateGui(player)
	
	local style = player.stats['Style'].Value
	local HUD = player.PlayerGui.HUD
	
	if style == "One" then
		colorGUI(HUD, Color3.fromRGB(3, 95, 109), Color3.fromRGB(27, 131, 159))
	elseif style == "Two" then
		colorGUI(HUD, Color3.fromRGB(179, 46, 19), Color3.fromRGB(221, 81, 26))
	elseif style == "Three" then
		colorGUI(HUD, Color3.fromRGB(122, 106, 24), Color3.fromRGB(218, 220, 52))
	end
	
	HUD.BottomBar['Style Name'].Text = style .. " Style"
	HUD.BottomBar['Level'].Text = "Level: " .. player.stats['Level'].Value
end

function colorGUI(HUD, colorBack, colorFill)
	HUD.BottomBar.MaxStamina.BackgroundColor3 = colorBack
	HUD.BottomBar.MaxStamina.Stamina.BackgroundColor3 = colorFill
end

return module

The LocalScript is rather small:

local guiModule = require(game.ServerScriptService:WaitForChild('GuiModule'))
guiModule.updateGui(game.Players.LocalPlayer)

And the Script running in the workspace

script.Parent.Touched:Connect(function(whomstd)
	if whomstd.Parent ~= nil then
		if game.Players:GetPlayerFromCharacter(whomstd.Parent) ~= nil then
			local player = game.Players:GetPlayerFromCharacter(whomstd.Parent)
			
			local guiModule = require(game.ServerScriptService:WaitForChild('GuiModule'))
			local guiModule = require(script.Parent.GuiModule)
		end
	end
end)

Theses scripts cause an infinite yield on the LocalScript

I see no mention of ReplicatedStorage. Did you try moving it there? Both the server and the client can access it if it’s there. Just know that they won’t receive the same instance, as each machine (each client and the server) receive a different instance on requiring the module.

Oh, interesting, I’ll definitely try it out!

You can’t access ServerScriptService, or ServerStorage, from LocalScripts. These are not replicated.

You should try moving the “GuiModule” into another location - ReplicatedStorage seems like the best choice.

1 Like

Okay, so I just tried, and it works exactly as expected, thank you both for helping me!