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

Simplest Script I've Ever Made Does Not Work?

Asked by
funyun 958 Moderation Voter
9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

Server script running in Workspace, and it's not disabled. I try it on a test server and in studio and it won't output anything.

print("sup")

I'm not sure how this affects it, but I'm new to data stores, and I tried making a server script running in Players that says whether or not my stuff loads when I join. It didn't work in studio or in the test server, so I moved it to Workspace. For whatever reason, it kinda worked and kinda didn't ("sup" and "Loading Player's data" printed, nothing else happened). Nothing worked in the test server. Here's the script.

print("sup")

local datastores = game:GetService("DataStoreService")
local users = datastores:GetDataStore("Users")
local http = game:GetService("HttpService")

local default = {
    ["AdminStatus"] = "Noob", --Administrative status, such as "Noob", "Mod", "Admin", etc
    ["Level"] = 1,
    ["Money"] = 50
}

local JCodedDefault = http:JSONEncode(default)


game.Players.PlayerAdded:connect(function(player)
    print("Loading "..player.Name.."'s data.")

    player:WaitForDataReady()

    local playerData = users:GetAsync("user_"..player.userId)
    if playerData then
        print(player.Name.."'s data was found.")
    else
        print(player.Name.."'s data was not found.")

        users:SetAsync("user_"..player.userId, JCodedDefault)
    end
end)
0
Correction: bottom script prints "sup" in studio, nothing happens in server funyun 958 — 9y

1 answer

Log in to vote
3
Answered by 9 years ago
print("sup")

local datastores = game:GetService("DataStoreService")
local users = datastores:GetDataStore("Users")
local http = game:GetService("HttpService")

local default = {
    ["AdminStatus"] = "Noob", --Administrative status, such as "Noob", "Mod", "Admin", etc
    ["Level"] = 1,
    ["Money"] = 50
}

local JCodedDefault = http:JSONEncode(default)


game.Players.PlayerAdded:connect(function(player)
    print("Loading "..player.Name.."'s data.")

    --player:WaitForDataReady() You DONT need to wait for data to be ready in datastores

    local playerData = users:GetAsync("user_"..player.userId)
    if playerData then
        print(player.Name.."'s data was found.")
    else
        print(player.Name.."'s data was not found.")

        users:SetAsync("user_"..player.userId, JCodedDefault)
    end
end)

In test servers you need to activate studio api for the place in order to use datastores.

0
btw im not exactly sure if you can save a json coded variable into datastores, but i may be wrong. FutureWebsiteOwner 270 — 9y
0
JSON is just a string. You can save strings to DataStores just fine. BlueTaslem 18071 — 9y
Ad

Answer this question