Local script difficulty accessing module script?

hi i have 2 sets of code. My issue i here that event when module.setTrue() is triggered, my local script still keeps printing that the value of the variable in the module tim is false ( its doing this through the module.getTim() ). I have disabled the script and copied the code to a normal script, and it works fine. what’s the issue? :happy3:thanks :happy1:

( module.setTrue() is triggered in another script, but im very confident in it working as intended. local script is in playerscripts )

Local script code

local module =  require(game:GetService("ReplicatedStorage"):WaitForChild("ModuleScript"))

while true do
	wait(0.2)
	local result = module.getTim()
	if result == true then
		print("tim is true")
	end
	print(result)
end	

Module script code


local module = {}
module.tim = false

function module:setTrue()
	module.tim = true
	
end

function module:getTim()
	
	return module.tim
end

return module

You use a colon for your functions while you’re attempting to call the function with the dot operator. Not 100% sure if that will fix it but I believe it’s related.

Where is this other script?
If it’s not on the same client it won’t be updating the same instance of the required module.

could you explain that a bit more?

hi , the other script is in a click detector in a part in the workspace, so i dont know how i would fix this

Refer to this post. However, I think the issue is something else. But you could still read into it to learn something new.

The server and client does not share the same VM environment. When they both require the same ModuleScript, both create and cache their own copy.

To do replication, you would have to do it through Instances, attributes, or remotes.

1 Like

The script in the click detector I’m guessing is a server script whilst the playerscript one is local. So though they both require the same module the references held for them are unique to the contexts they are running on.

To handle this I would use a remote event to fire to all the relevant clients (and write a handler) which need that property to be updated in their copy of the module.

Edit: @OniiSamaUwU also has good advice

like Intance.New ? and where would i store it

hi, is there an article to explain what that is :sweat_smile: or if you dont mind could you explain what that entails?

should i just make like an int value in the local player e.g local x = intance.New("IntValue") ?

This would be a simple format for a remote handler on the client, which can be called from anywhere on the server.
It simply listens for the remote and runs an update function to change the property in the module.

--module
local mod ={}
mod.tim = false

mod.update = function(prop, val)
    if mod[prop] then
        mod[prop] = Val
    end
end

Pathtoremote.onclientevent: Connect (mod.update)

return mod

Writing on phone, autocorrect may have misspelt things!

1 Like

They mean that if you require the modulescript on the client, it’s not the same as when you require it on the server. They do not share the same values. Setting tim to true on the server would not set it to true in the modulescript you required on the client.

The solution to this would be to use a BoolValue for example that holds the value of Tim, and is shared across both the server and client. Then when you call setTrue you just set the value of the boolValue to true on the server, and it will automatically replicate to the client.

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