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

Is there a reason why my datastore script isn't working in studio?

Hello!

I'm more or less looking for areas of improvement on my script, or potentially a solution as to why the datastore isn't working in studio, but works in game. It seems to be an issue with saving since data does load when in studio.

Near the end, where the DS:SetAsync is, seems to be the issue. I added a few prints yesterday and it seems to halt as the DS:SetAsync happens, as well as the print at the end not happening, so there may be an issue with my code. I was thinking maybe it was an issue with what was going in, the table to be more specific. Anyway, I'm open to suggestions. No errors are outputted.

local DS = game:GetService("DataStoreService"):GetDataStore("yourdatastore", "testing1")
game.Players.PlayerAdded:Connect(function(player)
    local variablePlayer = player.Name
    local folder = Instance.new("Folder",player)
    folder.Name = "inventoryfolder"
    local dataKey = "userkey_"..player.UserId
    local data
    local success,errormsg = pcall(function()
        data = DS:GetAsync(dataKey)
    end)

    if success then
        if data ~= nil then
                print("Player has data.")
                for name,value in pairs(data) do
                    local i2 = Instance.new("IntValue", folder)
                    i2.Name = name
                    i2.Value = value
                    print(name, value)
            end
        else
            print("Player is new.")
            
            local defaultvalue = Instance.new("IntValue", folder)
            defaultvalue.Name = "Default Value"
            defaultvalue.Value = 7 * math.random(4, 17)
        end    
    else
        warn("Error when retrieving data for "..player.Name..". Error: "..errormsg)
    end
    end)
game.Players.PlayerRemoving:Connect(function(player)
local inventory1 = {}

for i, v in pairs(player.inventoryfolder:GetChildren()) do
    inventory1[v.Name] = v.Value -- add the value to the table
    print(v.Name, v.Value)
end

    local success,errormsg = pcall(function()
    
    DS:SetAsync("userkey_"..player.UserId, inventory1)

    end)
    if success then
        print("Successfully saved for "..player.Name)
    else
        warn("Unable to save for "..player.Name..". Error: "..errormsg)
    end
end)

1 comment

0

I had a single value save. Maybe this is different? This script is based on the single value script.

1 answer

✓ Accepted answer

You need to enable datastores on studio, there's a tutorial on this link:

https://developer.roblox.com/en-us/articles/Data-store

11 comments

0

I've followed that already.

0

It should work...

0

Studio API services are on.

0

did you tried closing and reopenning the place on studio after that?

0

Yeah.

0

I'll try turning the services off then back on again.

0

No change.

0

Try going on your game page > ••• > Configure game and see if the "Enable studio access to API" option is enabled there

0

Yeah, it's enabled.

0

Its probally a bug on the place, try making a post on roblox devforum

0

Sorry, late reply. I don't have permission to post there.

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