Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can i make the First joined game script work?

Asked by 4 years ago
Edited 4 years ago

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?

0
pretty sure theres a rule which states that the code has to be yours, not taken from a free model or from a friend. iOriena3 67 — 4y
0
oh well i dont know how to make a first joined script so i ask for help hen i got this and i tried it and it didnt work, after that i didnt get any response mahid786 41 — 4y
0
oh, fair enough I guess. iOriena3 67 — 4y
0
Ah, this is my old work. It's pretty broken; Response isn't set, but the request is successful, so it tries to get Join data from an empty table. Ziffixture 6913 — 4y
0
so then what do i do? mahid786 41 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
i had gotten this at line 27 (attempt to index nil with 'Joined') mahid786 41 — 4y
0
i did change the datastore to something else so i could test if it works fully mahid786 41 — 4y
Ad

Answer this question