I can't get my Data Store V2 to work, It always returns nil - roblox

I read the Data Store V2 documentation on the Dev Hub and tried to make a similar code, but I can't get it to work. It doesn't have any errors other than Attempt to index nil with Version because it can't get the Data. It returns Succes = true, and CurrentNumber, KeyInfo = nil. There are no errors when saving the data and the Succes variable is always returned true. I enabled the security configuration to allow data stores, the game is public and I tested it both on studio and the actual game(on Roblox). The code is in a script in the Server Script Service Here's the code:
local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')
-- Data Stores:
-- Enables Data Stores V2
local Options = Instance.new('DataStoreOptions')
Options:SetExperimentalFeatures({["v2"] = true})
local NumberStore:DataStore = DataStoreService:GetDataStore("PlayerNumber", "global", Options)
local SetOptions = Instance.new('DataStoreSetOptions')
SetOptions:SetMetadata({['PlayerNumberType'] = 'Int'})
local function SaveNumberData(Player:Player)
local Number = 1
local PlayerKey = 'Player_1234'
local Sucess, Errormessage = pcall(function()
NumberStore:SetAsync(PlayerKey, Number, {Player.UserId}, SetOptions)
end)
if not Sucess then
print(Errormessage)
else
print('Data Saved')
end
end
local function LoadPlayerData(Player:Player)
local PlayerKey = 'Player_1234'
local Sucess, CurrentNumber, KeyInfo:DataStoreKeyInfo = pcall(function()
NumberStore:GetAsync(PlayerKey)
end)
if Sucess then
print(CurrentNumber)
print(KeyInfo.Version)
print(KeyInfo.CreatedTime)
print(KeyInfo.UpdatedTime)
print(KeyInfo:GetUserIds())
print(KeyInfo:GetMetadata())
else
print(CurrentNumber)
end
end
Players.PlayerRemoving:Connect(SaveNumberData)
Players.PlayerAdded:Connect(LoadPlayerData)

I found the answer, I forgot to put return before NumberStore:GetAsync(PlayerKey)

Related

Roblox Studio-DataStore request was added to queue

I am making Race clicker game
And this is my script of my code
and I am keep getting the error, "DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 2222391052"
And the leaderstats are not updating.
local dataStoreService = game:GetService("DataStoreService")
local leaderstatsDataStore = dataStoreService:GetDataStore("data")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local passions = Instance.new("IntValue", leaderstats)
passions.Name = "Passion"
local upgrades = Instance.new("IntValue", leaderstats)
upgrades.Name = "Hero Upgrade"
local MaxSpeed = Instance.new("IntValue",leaderstats)
MaxSpeed.Name = "Max Speed"
local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
if leaderstatsDataStore ~= nil then
passions.Value = leaderstatsData[1]
upgrades.Value = leaderstatsData[2]
MaxSpeed.Value = leaderstatsData[3]
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local values = {}
for _, child in pairs(player.leaderstats:GetChildren()) do
table.insert(values,child.Value)
end
pcall(function()
leaderstatsDataStore:SetAsync(player.UserId, values)
end)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local values = {}
for _, child in pairs(player.leaderstats:GetChildren()) do
table.insert(values,child.Value)
end
pcall(function()
leaderstatsDataStore:SetAsync(player.UserId, values)
end)
end
end)
Someone said that this is an error that happens if I send request more than 60 times a second.
But I can't really get what is sending so much requests.
So can anyone tell the changed code to me
That's not an error, that's a warning. (Errors are red, warnings are yellow.)
You are getting the warning because the server can't do multiple data store operations at once. Instead, it queues the operations and does them one by one. However, if the queue becomes too long, some requests get dropped. That's not happening in your case, so you don't have to care about it.

How to send errors from Neovim LSP to ALE

ALE has an API for sending errors to it from other sources. I'm using this like shown below and it works for the first error. More specifically, if I make one edit that results in an LSP error, the error will be displayed by ALE in the location list. If I make any further keystrokes, the location list is emptied again.
I can also trigger this behavior if I disable LSP, load ALE, manually call ShowResults and then press any other key in insert mode.
My hypothesis is that ALEs linting in insert mode (per default) kicks in. If LSP is disabled and there are no linters registered for the current file type, it doesn't find any errors (obviously, there's nothing that could report any) and so it empties my location list again. So the steps are: open buffer without LSP, call ShowResults, location list opens, press i, press any key, location list is empty
Now I thought that it was because I wasn't implementing the full ALE API. So I added a hook (2nd snippet). I can verify that this hook is called and that it generates valid messages. If I keep the location list open I can even see all the expected errors flickering across the loclist. I can navigate to those errors with :lolder, but I don't know why ALE always adds another, empty location list, after the LSP is done doing its job.
Or maybe I'm doing something wrong here.
vim.lsp.handlers["textDocument/publishDiagnostics"] = function(_, _, params, client_id, _, config)
local uri = params.uri
local bufnr = vim.uri_to_bufnr(uri)
if not bufnr then
return
end
local diagnostics = params.diagnostics
if not diagnostics or vim.tbl_isempty(diagnostics) then
vim.fn['ale#other_source#ShowResults'](bufnr, "nvim-lsp", {})
return
end
-- Important so we can pull diagnostics from this table when ALE asks for
-- them
vim.lsp.diagnostic.save(diagnostics, bufnr, client_id)
local messages = {}
for _, event in ipairs(diagnostics) do
-- :h ale-localist-format
local msg = {}
msg.text = event.message
msg.lnum = event.range.start.line
msg.end_lnum = event.range["end"].line
msg.col = event.range.start.character
msg.end_col = event.range["end"].character
msg.bufnr = bufnr
msg.nr = event.severity
table.insert(messages, msg)
end
vim.fn['ale#other_source#ShowResults'](bufnr, "nvim-lsp", messages)
end
Second snippet which is called from the 'on_attach' function of the Neovim LSP
function ALEHook(bufnr)
vim.fn['ale#other_source#StartChecking'](bufnr, "nvim-lsp")
local diagnostics = vim.lsp.diagnostic.get(bufnr, client.id)
if not diagnostics or vim.tbl_isempty(diagnostics) then
vim.fn['ale#other_source#ShowResults'](bufnr, "nvim-lsp", {})
return
end
local messages = {}
for _, event in ipairs(diagnostics) do
local msg = {}
msg.text = event.message
msg.lnum = event.range.start.line
msg.end_lnum = event.range["end"].line
msg.col = event.range.start.character
msg.end_col = event.range["end"].character
msg.bufnr = bufnr
msg.nr = event.severity
table.insert(messages, msg)
end
vim.fn['ale#other_source#ShowResults'](bufnr, "nvim-lsp", messages)
end
api.nvim_command('augroup ALEHookLSP')
api.nvim_command('autocmd!')
api.nvim_command('autocmd User ALEWantResults call v:lua.ALEHook(g:ale_want_results_buffer)')
api.nvim_command('augroup END')

Roblox: BindToClose works, PlayerRemoving does not

I am trying to store CFrame X, Y, and Z data into DataStore so that the player can start off where they left off. I am testing by trying to save hardcoded values.
The loading of data using PlayerAdded works fine.
For some reason, the SetAsync() in PlayerRemoving wasn't completing when the player leaves in game or in studio. The weird thing is that when I added BindToClose, the save function runs correctly when in Studio but not in game.
If anyone can tell me why, I'd be very grateful.
local function save(plr)
local data = {
CFrameX = -1232;
CFrameY = 1232;
CFrameZ = -1235;
}
local success, errormessage = pcall(function()
MandoSimStore:SetAsync(plr.UserId.."-Location_StorageData", data)
end)
if success then
print("Successfully saved data!")
else
warn("ERROR: "..errormessage)
end
end
local gameShutDown = false
game.Players.PlayerRemoving:Connect(function(plr)
wait(0.1)
if not gameShutDown then
save(plr)
end
end)
game:BindToClose(function()
gameShutDown = true
for _, plr in ipairs(game.Players:GetPlayers()) do
save(plr)
end
end)
Maybe try
wait(1)
It worked for me

BodyPosition not changing?

I've been sitting at my laptop for about an hour trying to figure out what's going on. I did this exact same thing yesterday and it worked. Please help.
Explaining the program:
The program takes the object that the mouse is pointing at, if you click the object gravitates toward the mouse.
local player = game.Players.LocalPlayer
local runservice = game:GetService("RunService")
local character = player.Character
local humanoid = character.Humanoid
local camera = workspace.Camera
local mouse = player:GetMouse()
local mousepos = mouse.hit.p
local grabbed = false
local function grabbedobj(obj) --this is the thing i need help on
local bodypos = obj.BodyPosition.Position
local pos = obj.Position
obj.Anchored = false
mouse.Button1Down:Connect(function()
grabbed = true
end)
mouse.Button1Up:Connect(function()
grabbed = false
end)
if grabbed == true then
print(obj.BodyPosition.Position)
bodypos = Vector3.new(mousepos.X, mousepos.Y, mousepos.Z) --really important
end
if grabbed == false then
bodypos = Vector3.new(pos.X, -25, pos.Z)
pos = Vector3.new(bodypos.X, pos.Y, bodypos.Z)
end
end
local function setup()
mousepos = mouse.hit.p
if mouse.Target ~= nil and mouse.Target.Parent == game.Workspace.IntObj then
grabbedobj(mouse.Target) --takes object that the mouse is pointing at
end
end
while wait(.1) do
setup()
end
Lets shuffle the code a little bit so it does what I think you want to do. You might have mixed up some things while trying to debug this. So here:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local grabbed = false
local obj
mouse.Button1Down:Connect(function()
if mouse.Target ~= nil and mouse.Target.Parent == game.Workspace.IntObj then
obj = mouse.Target
obj.Anchored = false
grabbed = true
end
end)
mouse.Button1Up:Connect(function()
if (grabbed) then
grabbed = false--BodyPosition
obj.BodyPosition.Position = Vector3.new(obj.Position.X, 0.1, obj.Position.Z)
obj.Anchored = true
end
end)
local function move()
if (grabbed) then
local mousepos = mouse.hit.p
obj.BodyPosition.Position = Vector3.new(mousepos.X, mousepos.Y, mousepos.Z)
print(obj.BodyPosition.Position)
end
end
while wait(.1) do
move()
end
When you click the mouse it checks if you click your part that is in IntObj. It will retain a reference to that part and then moves it with the mouse. When you release the button it will drop it at its location.
So this LocalScript should work but it has an issue updating the BodyPosition as you said in the title. The reason for that is that you can only update server parts from your client if you give the client permission from the server for it. You could write a remote function on the server and say:
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function(plr)
Workspace.IntObj.Part:SetNetworkOwner(plr)
end
If you call this from your local script's initialization, it will work. Note, that your part cannot be anchored for that.
Another way would be to just let the server do the updating. So instead of setting the
obj.BodyPosition.Position on the client you would call the remote function like this:
game.ReplicatedStorage.RemoteFunction:InvokeServer(Vector3.new(mousepos.X, mousepos.Y, mousepos.Z))
and then on the server you set the position:
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function(plr, bodyPos)
script.Parent.BodyPosition.Position = bodyPos
end
In that case, if you anchor/unanchor the part, do that on the server as well. Otherwise you'll get weird results.

Roblox- how to store large arrays in roblox datastores

i am trying to make a game where players create their own buildings and can then save them for other players to see and play on. However, roblox doesn't let me store all the data needed for the whole creation(there are several properties for each brick)
All i get is this error code:
104: Cannot store Array in DataStore
any help would be greatly appreciated!
I'm not sure if this is the best method, but it's my attempt. Below is an example of a table, you can use tables to store several values. I think you can use HttpService's JSONEncode function to convert tables into strings (which hopefully can be saved more efficiently)
JSONEncode (putting brick's data into a string, which you can save into the DataStore
local HttpService = game:GetService("HttpService")
-- this is an example of what we'll convert into a json string
local exampleBrick = {
["Size"] = Vector3.new(3,3,3),
["Position"] = Vector3.new(0,1.5,0),
["BrickColor"] = BrickColor.new("White")
["Material"] = "Concrete"
}
local brickJSON = HttpService:JSONEncode(exampleBrick)
print(brickJSON)
-- when printed, you'll get something like
-- { "Size": Vector3.new(3,3,3), "Position": Vector3.new(0,1.5,0), "BrickColor": BrickColor.new("White"), "Material": "Concrete"}
-- if you want to refer to this string in a script, surround it with two square brackets ([[) e.g. [[{"Size": Vector3.new(3,3,3)... }]]
JSONDecode (reading the string and converting it back into a brick)
local HttpService = game:GetService("HttpService")
local brickJSON = [[ {"Size": Vector3.new(3,3,3), "Position": Vector3.new(0,1.5,0), "BrickColor": BrickColor.new("White"), "Material": "Concrete"} ]]
function createBrick(tab)
local brick = Instance.new("Part")
brick.Parent = <insert parent here>
brick.Size = tab[1]
brick.Position= tab[2]
brick.BrickColor= tab[3]
brick.Material= tab[4]
end
local brickData = HttpService:JSONDecode(brickJSON)
createBrick(brickData) --this line actually spawns the brick
The function can also be wrapped in a pcall if you want to account for any possible datastore errors.
Encoding a whole model into a string
Say your player's 'building' is a model, you can use the above encode script to convert all parts inside a model into a json string to save.
local HttpService = game:GetService("HttpService")
local StuffWeWantToSave = {}
function getPartData(part)
return( {part.Size,part.Position,part.BrickColor,part.Material} )
end
local model = workspace.Building --change this to what the model is
local modelTable = model:Descendants()
for i,v in pairs(modelTable) do
if v:IsA("Part") or v:IsA("WedgePart") then
table.insert(StuffWeWantToSave, HttpService:JSONEncode(getPartData(modelTable[v])))
end
end
Decoding a string into a whole model
This will probably occur when the server is loading a player's data.
local HttpService = game:GetService("HttpService")
local SavedStuff = game:GetService("DataStoreService"):GetDataStore("blabla") --I don't know how you save your data, so you'll need to adjust this and the rest of the scripts (as long as you've saved the string somewhere in the player's DataStore)
function createBrick(tab)
local brick = Instance.new("Part")
brick.Parent = <insert parent here>
brick.Size = tab[1]
brick.Position= tab[2]
brick.BrickColor= tab[3]
brick.Material= tab[4]
end
local model = Instance.new("Model") --if you already have 'bases' for the players to load their stuff in, remove this instance.new
model.Parent = workspace
for i,v in pairs(SavedStuff) do
if v[1] ~= nil then
CreateBrick(v)
end
end
FilteringEnabled
If your game uses filteringenabled, make sure that only the server handles saving and loading data!! (you probably already knew that) If you want the player to save by clicking a gui button, make the gui button fire a RemoteFunction that sends their base's data to the server to convert it to a string.
BTW I'm not that good at scripting so I've probably made a mistake somehwere.. good luck though
Crabway's answer is correct in that the HttpService's JSONEncode and JSONDecode methods are the way to go about tackling this problem. As it says on the developer reference page for the DataStoreService, Data is ... saved as a string in data stores, regardless of its initial type. (https://developer.roblox.com/articles/Datastore-Errors.) This explains the error you received, as you cannot simply push a table to the data store; instead, you must first encode a table's data into a string using JSONEncode.
While I agree with much of Crabway's answer, I believe the function createBrick would not behave as intended. Consider the following trivial example:
httpService = game:GetService("HttpService")
t = {
hello = 1,
goodbye = 2
}
s = httpService:JSONEncode(t)
print(s)
> {"goodbye":2,"hello":1}
u = httpService:JSONDecode(s)
for k, v in pairs(u) do print(k, v) end
> hello 1
> goodbye 2
As you can see, the table returned by JSONDecode, like the original, uses strings as keys rather than numeric indices. Therefore, createBrick should be written something like this:
function createBrick(t)
local brick = Instance.new("Part")
brick.Size = t.Size
brick.Position = t.Position
brick.BrickColor = t.BrickColor
brick.Material = t.Material
-- FIXME: set any other necessary properties.
-- NOTE: try to set parent last for optimization reasons.
brick.Parent = t.Parent
return brick
end
As for encoding a model, calling GetChildren would produce a table of the model's children, which you could then loop through and encode the properties of everything within. Note that in Crabway's answer, he only accounts for Parts and WedgeParts. You should account for all parts using object:IsA("BasePart") and also check for unions with object:IsA("UnionOperation"). The following is a very basic example in which I do not store the encoded data; rather, I am just trying to show how to check the necessary cases.
function encodeModel(model)
local children = model:GetChildren()
for _, child in ipairs(children) do
if ((child:IsA("BasePart")) or (child:IsA("UnionOperation"))) then
-- FIXME: encode child
else if (child:IsA("Model")) then
-- FIXME: using recursion, loop through the sub-model's children.
end
end
return
end
For userdata, such as Vector3s or BrickColors, you will probably want to convert those to strings when you go to encode them with JSONEncode.
-- Example: part with "Brick red" BrickColor.
color = tostring(part.BrickColor)
print(string.format("%q", color))
> "Bright red"
I suggest what #Crabway said, use HttpService.
local httpService = game:GetService("HttpService")
print(httpService:JSONEncode({a = "b", b = "c"}) -- {"a":"b","b":"c"}
But if you have any UserData values such as Vector3s, CFrames, Color3s, BrickColors and Enum items, then use this library by Defaultio. It's actually pretty nice.
local library = require(workspace:WaitForChild("JSONWithUserdata"))
library:Encode({Vector3.new(0, 0, 0)})
If you want a little documentation, then look at the first comment in the script:
-- Defaultio
--[[
This module adds support for encoding userdata values to JSON strings.
It also supports lists which skip indices, such as {[1] = "a", [2] = "b", [4] = "c"}
Userdata support is implemented by replacing userdata types with a new table, with keys _T and _V:
_T = userdata type enum (index in the supportedUserdataTypes list)
_V = a value or table representing the value
Follow the examples bellow to add suppport for additional userdata types.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Usage example:
local myTable = {CFrame.new(), BrickColor.Random(), 4, "String", Enum.Material.CorrodedMetal}
local jsonModule = require(PATH_TO_MODULE)
local jsonString = jsonModule:Encode(myTable)
local decodedTable = jsonModule:Decode(jsonString)
--]]