Local script doesn't access module script correctly

  1. What do you want to achieve?
    I want to have a local script get data from a module script function.
  2. What is the issue?
    The module script functions don’t work for local scripts. They only work for server scripts.
  3. What solutions have you tried so far?
    I have tried changing the functions and different scripts, and I have also tried finding devforums about this issue but couldn’t find any that helped.

There is a server script that creates gears in the game and when a player’s mouse hovers on the gear, a selection box appears on the gear for the player if they are close enough (must get the position of the gear), but instead after I create a gear using GearInfo.AddGear(), it only shows up for server scripts, and my local script can’t access it using the functions.

Module script:

local GearAmounts = {
	["NormalGearPart"] = 0
}

local GearInfo = {

}

local GInfo = {
	
}

function GearInfo.AddGear(gearType)
	print("creating gear data")
	GearAmounts[gearType] += 1
	
	local newName = gearType .. tostring(GearAmounts[gearType])
	local newInfo = {
		["Health"] = 10,
		["Drop"] = "NormalGearItem",
		["DropAmt"] = 15,
		["Size"] = {},
		["Orientation"] = {},
		["Position"] = {}
	}
	
	GInfo[newName] = newInfo
	return newName
end

function GearInfo.GetData(gear, data)
	print("fetching gear data")
	if data == "All" then
		return GInfo[gear.Name]
	else
		print(GInfo)
		return GInfo[gear.Name][data]
	end
end

function GearInfo.GetAmounts(gearType)
	print("fetching gear amounts")
	return GearAmounts[gearType]
end

function GearInfo.UpdateData(gear, data, info)
	print("updating gear data")
	if data == "All" then
		GInfo[gear.Name] = info
	else
		GInfo[gear.Name][data] = info
	end
end

function GearInfo.RemoveGear(gear)
	print("removing gear data")
	GInfo[gear.Name] = nil
end

return GearInfo

Server script:

local function CreateGear()
	coroutine.resume(coroutine.create(function()

		local gear = Gears[math.random(1, #Gears)]:Clone()
		gear.Parent = gearFolder
		gear.Name = GearInfo.AddGear(gear.Name)

		GearInfo.UpdateData(gear, "Size", gear.Size)
		GearInfo.UpdateData(gear, "Orientation", gear.Orientation)
		GearInfo.UpdateData(gear, "Position", gear.Position)
		
		print(gear.Name)
	end))
end

player script:

-- Select a gear
mouse.Move:Connect(function()
	local target = mouse.Target
	if target then
		print(target.Name)
		if gearFolder:FindFirstChild(target.Name) then
			local gearPosition = GearInfo.GetData(target, "Position") -- when it calls on the module script it says trying to access nil, even though the gear exists in the table.
			local magnitude = (character.HumanoidRootPart.Position - gearPosition)
			if magnitude <= 10 then -- ignore stuff in here
				selectionBox.Adornee = target
				if pickActivated then
					PlrHitGear:FireServer(target, pickActivated)
				end
			end
		end
	end
end)

You don’t seem to require the module script in both of your serverscript and localscript to use here’s an example of basic module usage

for example

local GearInfo = require(LocationOf_GearInfo_Module) --// Location of module to use it
local function CreateGear()
	coroutine.resume(coroutine.create(function()

		local gear = Gears[math.random(1, #Gears)]:Clone()
		gear.Parent = gearFolder
		gear.Name = GearInfo.AddGear(gear.Name)

		GearInfo.UpdateData(gear, "Size", gear.Size)
		GearInfo.UpdateData(gear, "Orientation", gear.Orientation)
		GearInfo.UpdateData(gear, "Position", gear.Position)
		
		print(gear.Name)
	end))
end

yeah I didn’t paste it in here but I do require it
edit: I just pasted in the script parts which have to do mostly with the module script.

May i see the lines you required the modules with

ok, for both my server script and player local script I use this:

local GearInfo = require(ReplicatedStorage.GearInfo)
local ToolInfo = require(ReplicatedStorage.ToolInfo)
local TweenStorage = require(ReplicatedStorage.TweenStorage)
local PlayerStats = require(ReplicatedStorage.PlayerStats)

to require my module scripts.
ReplicatedStorage is a variable referencing game.ReplicatedStorage

Oh sorry,i misread and was just focusing on requiring you are trying to view contents on the module from client-side and server-side.

yea, I use functions from the module scripts to try to get data, but for some reason it doesn’t work for my local player script

Is there anything in your output log? any errors?

Yeah,

ReplicatedStorage.GearInfo:38: attempt to index nil with ‘Position’ - Client - GearInfo:38

is the error when trying GearInfo.GetData(target, “Position”) in the local player script

I don’t know how you declared the gear folder variable did you print it? or debugged to find its contents

are you talking about gearFolder variable in the server script?

But yes I did do a lot of printing to see if I can get the contents through the module script.
It returns an empty table every time through the local script, but returns the expected data when I do it through the server side script

I was concerned about it i verified with @T0ny that “once you load a script server-side it is on the server if you load it client-side it is on the client”. At the moment you just edited 2 different copies of the same module script the contents are not the same.

What i suggest is making remote-functions that will return the data you want that is on server-side to the client.

5 Likes