i want to make the script i have work so that when someone first joins my game a gui pop's up, i got this script from someone but it dont work. Here's the script:
local DataStoreService = game:GetService("DataStoreService") local ReplicatedStorage = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") local JoinData = DataStoreService:GetDataStore("JoinTest") local function CheckIfJoined(Player) print('Player Joined, Now Checking...') local Success, Response = pcall(JoinData.GetAsync, JoinData, Player.UserId) if (Success) then local Joined = select(1,unpack(Response)) print('Checking...') if not (Joined) then print('First Time') SetJoinData(Player) end else warn(Response) end end local function SetJoinData(Player) local JoinInformation = { Joined = true; Date = os.date('%x'); UnixTimeStamp = tick() } JoinData:SetAsync(Player.UserId, JoinInformation) end Players.PlayerAdded:Connect(CheckIfJoined)
it keeps giving me the error bad argument #1 (table expected, got nil)
can someone please help me and explain what it mean?
Deleted the previous reply as it was incorrect. I believe the issue is that you're trying to unpack Results, but if the player already has data, it will have a value of nil, which you cannot unpack. What I would recommend is just checking if it was successful, and if it was it means there must be data that already exists. Also, what are you trying to get with os.date()? You need to give the argument of what date (either "*t
" or "!*t"
) you want (UTC or local). You can find the os docs here I changed a lot your code, if you have any questions, ask. Keep in mind im very new to Lua, but this seems to work after testing it myself.
local DataStoreService = game:GetService("DataStoreService") -- Get all required services local ReplicatedStorage = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") local JoinData = DataStoreService:GetDataStore("JoinTest") local function SetJoinData(Player) -- Sets data to the player when they join local JoinInformation = { Joined = true; -- If the player has joined Date = os.date("*t"); -- Sets the date UnixTimeStamp = tick(); -- Sets the precise time stamp } JoinData:SetAsync(Player.UserId, JoinInformation) -- Set the players data to JoinInformation table print("SetJoinData has been set") end local function CheckIfJoined(Player) -- Check if the player has joined (called on player join) print('Player Joined, Now Checking...') local Success, Response = pcall(function() -- Try get data JoinData:GetAsync(Player.UserId) print("Got player "..Player.Name.." data successfully.") print(JoinData:GetAsync(Player.UserId).Joined) -- A test to see if we can retrieve the data end) if (Success) then -- If there were no errors retrieving the data, set data SetJoinData(Player) else warn(Response) -- Else, display the error end end Players.PlayerAdded:Connect(CheckIfJoined) -- When player connects, call CheckIfJoined
As to why I changed what I did, you cannot unpack a table that does not exist, and those lines of code were unnecessary. Also, the argument GetAsync() takes is what key is to the players specific data, not the datastore. As GetAsync() is a method - shown by :
- it takes self as the first argument, so you dont need to specify what datastore you are taking it from.
Hope this helps. If it did solve your problem, dont forget to upvote and set this as the answer
EDIT: Feel free to remove any print statements I added, it just shows if the code is performing correctly