Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Simplest Script I've Ever Made Does Not Work?

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)

1 comment

0

Correction: bottom script prints "sup" in studio, nothing happens in server

1 answer

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.

2 comments

0

btw im not exactly sure if you can save a json coded variable into datastores, but i may be wrong.

0

JSON is just a string. You can save strings to DataStores just fine.

This discussion is preserved as it was. New answers, comments, and votes are closed.