and when I try to reference it, I get this result:
ServerScriptService.dayScript:33: invalid argument #1 to 'pairs' (table expected, got nil) - Server - dayScript:33
before I made them module accessible (local dayArray instead of day.dayArray) it worked perfectly fine.
Am I doing something wrong? Any help is greatly appreciated
You need to provide more of your script for anyone to help, at least the part where you return in the module script, the for loop in your other script, and where you require the module script. Ideally share both script fully, or if they are big, cut what you are pretty much sure is irrelevant
okay il use a different example, because that one is very long winded
Remotes.receiveInteraction.OnServerEvent:Connect(function(player)
local currInt = player.PlayerGui.IngameUI.currentInteraction.Value
if SS.inInteraction:FindFirstChild(player.Name) then
local interactionAdditives = Module.ContractArray[currInt]
Module.grant(player, interactionAdditives)
end
end)
this part is self explanatory. currInt is a string value that holds the main prompt. looks something like this: currInt = "You find a mermaid, and she offers you vast strength for a sacrifice."
This grabs the attached keyvalue, which is another array holding what is to be granted, looking like this:
day.ContractArray = {
["You find a mermaid, and she offers you vast strength for a sacrifice."] = {["ATK"] = "05%", ["MaxHP"] = "07%"},
--["A devil appears infront of you, holding out a sheet of paper."] = {""}
}
i get the error:
ServerScriptService.dayScript:58: attempt to perform arithmetic (mul) on number and nil
within this function:
function day.grant(player, items) -- {["ATK"] = X, ["MaxHP"] = X}
if type(items) == "table" then
for text, reward in pairs(items) do
local file = player:WaitForChild("gameStats"):WaitForChild("currentStats"):FindFirstChild(text)
if type(reward) == "string" then
local subbed = string.sub(reward, -1, 1)
local makeable = "1."..reward
file.Value = file.Value * tonumber(makeable)
else
file.Value = file.Value + reward
end
end
end
end
at this line: file.Value = file.Value * tonumber(makeable)
this function might be made a bit silly, im new to systems like this :')
This line seems problematic, you are subbing a string from the last point, to the first, so either it would return the string in reverse, or an empty string if it isn’t made for that? I tried it and I get an empty string. Then the use of tonumber() is not recommended for code like this. It can return nil if there are non number characters like “%”, you shouldn’t need it for things other than converting user inputs to numbers
Make sure to print file.Value to verify that it isn’t nil
Moreover,
Remotes.receiveInteraction.OnServerEvent:Connect(function(player)
local currInt = player.PlayerGui.IngameUI.currentInteraction.Value
if SS.inInteraction:FindFirstChild(player.Name) then
local interactionAdditives = Module.ContractArray[currInt]
Module.grant(player, interactionAdditives)
end
end)
If you are updating the currentInteraction Value Object from the client, I am pretty sure that doesn’t replicate to the server (very little things replicate from client → server since filtering enabled). What can then happen is, whatever value currentInteraction has by default (an empty string? idk if it can be nil), that will be used to get interactionAdditives from Module.ContractArray, but since it’s an unexpected value, that is likely not present in Module.ContractArray, interactionAdditives will be nil, and you’ll be passing nil to the day.grant() function, and that function will error at the pairs(items) as items would be nil.
I believe this is the previous error you had
local mod = require(script:WaitForChild("ModuleScript"))
local dayArray = mod.days()
for day, event in pairs(dayArray) do
print(day, event)
end
---
print()
local Day = "Day17"
local event = dayArray[Day]
if event then
print("The event for " .. Day .. " is " .. event)
else print(Day .. " not found!")
end
– I know it’s a dictionary, the outcome is basically the same.
– “Mod” could be anything you wish it to be. Picked mod to show that.
– The ModuleScript is inside the main script … this is an example.
– Just going off the 1st script posted by the OP.
– Why are you questioning my reply?
thank you for pointing out the string.sub issue - that was the problem.
im not very used to using string manipulation as ive never really had to do this kinda stuff before.
if you dont mind me asking, how do i remove the last character of a string? LOL
tyvm <3
to get the last character of a string, you want to do this
string.sub(str, -1, -1)
i is set to the last character, and j is also set to the last character, so it will return the last character. j can actually be omitted here because it is -1 by default. A negative number is for “indexing” from the end of the string
For whatever reason I thought you were OP and assumed some contradictions in your post were possible problems with the script. Sorry for any headache this caused, entirely my fault.