I’m working on a system where a player can claim a register in a store. I’m storing the data on who’s claimed which register in a ModuleScript, and I noticed that sometimes even while a register is claimed it will say “UNCLAIMED” rather than the player. When I tried printing the dictionary every 5 seconds to see what’s going on, it started printing the dictionary twice every 5 seconds… I’m so clueless.
ModuleScript:
local RegisterModule = {}
RegisterModule.RegisterData = {}
for _,Register in pairs(game.Workspace.Registers:GetChildren()) do
RegisterModule.RegisterData[Register] = "UNCLAIMED"
end
while wait(5) do
print(RegisterModule.RegisterData)
end
return RegisterModule
Whenever you require a ModuleScript, it will execute all the code inside of it once, and because you have a while loop inside of the code, it will constantly print every 5 seconds. What seems to be the problem is that you have 2 scripts trying to grab data from the same module, leading to it printing out twice every 5 seconds.
Another thing worth mentioning is that because there is a while loop, the Module will not return anything because the interpreter will contantly be reading the loop as opposed to moving on and exiting the code.
I’m aware of the second part, but still confused about the first. Let me explain the problem a bit better,
When you claim a register, it prints the player who has claimed it to the console. It returns my username every time. While a register is claimed, you can use a proximityprompt on the nearby tipjar, and it’s supposed to print the same exact thing from the same exact table, but instead it prints “UNCLAIMED”.
Script:
local Registers = require(game.ReplicatedStorage.RegisterModule).RegisterData
-- What i run when a register is claimed (returns charlestonial)
Registers[Register] = Player
print(Registers[Register])
-- What i run when a player interacts with the tip jar (returns "UNCLAIMED")
for Register,Claimed in pairs(Registers) do
local ProximityPrompt = Register.TipJar:WaitForChild("MeshPart").ProximityPrompt
ProximityPrompt.Triggered:Connect(function(Player)
print(Claimed)
end)
end
Your code is stuck in a loop because of the while loop within the ModuleScript, the code does not move past the loop and continues the run it constantly, thus not returning anything, and always priting out the same result.
Scripts dont skip loops to move on to the rest of the code, instead they will be constantly reading it and constantly restarting it until you tell it to break out of the loop, and or put in another thread via task or coroutines.
The issue i’m talking about is occuring with the loop deleted. This is what the module looks like when the issue is occuring:
local RegisterModule = {}
RegisterModule.RegisterData = {}
for _,Register in pairs(game.Workspace.Registers:GetChildren()) do
RegisterModule.RegisterData[Register] = "UNCLAIMED"
end
return RegisterModule
You’re printing the result of the current loop. If you make a change while you’re iterating through a table, its not going to update the loop, and because you printing out the variable from that current iteration, it will always print “UNCLAIMED” as that’s what it was during the loop, so you just need to remove it.
Also, I recommend you use ProximityPromptService for this, as opposed to creating a million Events to serve one purpose.