For some reason, getting data from a dictionary isn’t working. Here’s my script
productFunctions[1353158718] = function(receipt, player)
playerTable[player.Name] = os.time()
cooldownTable[player.Name] = 15
receiptProcessed:FireClient(player,tech)
print("tech90")
end
task.spawn(function()
print("inside task.spawn")
while true do
print("inside loop")
for _, plr in pairs(playerTable) do
print("inside for loop")
print(plr)
if os.time() - playerTable[plr] > cooldownTable[plr] then --this is the line causing the error
playerTable[plr] = nil
cooldownTable[plr] = nil
print(plr.." ran out of time")
end
end
task.wait(1)
end
end)
When the script runs, it says I’m attempting to perform arithmetic on number and nil. Am I trying to get the data incorrectly, or am I setting it incorrectly? How do I fix this?
As it processes each player in the playerTable, print their data, just to confirm that there is something in each entry:
for _, plr in pairs(playerTable) do
print("playerTable", playerTable[plr])
print("cooldownTable", cooldownTable[plr])
That way, you are confirming a), that the data is valid and b) that the data you have inserted is also valid.
Never underestimate the power of printing a value.
I believe you are trying to get the data incorrectly. My reasoning is because you are looping through the array, then using the value equivalent to the array to get the value of the array, instead of using the index. Here’s an example:
-- array: {name: age}
local array = {
Jerry = 18
}
What you’re doing:
for _, age in array do
-- (You're indexing the value using the value)
array[age] -- nil; there is no value assigned to array[18].
end
What you should be doing:
for name, _ in array do
-- Access the value using the index, not the value.
array[name] -- 18.
end
Sorry if I formatted this bad. I am on mobile, so it is harder for me.