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

How to save a value in workspace with DataStore?

Guys, someone can help me? I want to save a value in a model. But my script dont work.

Script:

local DataStoreService = game:GetService("DataStoreService")
local msgstore = DataStoreService:GetDataStore("HouseStore") --Value that saves the message

local success, key = pcall(function()
    return msgstore:GetAsync(game.Workspace.House.Owner.Value)
end)


local key = game.Workspace.House.Owner.Value   

if success then
    print("Success!")
else
    msgstore:SetAsync(key, "ohnub")
end

2 comments

0

don't bite off more than you can chew

0

The first argument of :SetAsync() is the key you're going to be using. This is usually a unique key, like if you were storing every player's data, but it can be general. The second argument is the actual data to be stored. I assume that you have your arguments backwards at line 14. I guessed that you want to store the value in workspace with the key "ohnub". If so, flip them around

1 answer

First of all, GetAsync's argument is a key, not instance. Secondly, you have 2 variables 'key'. Thirdly, SetAsync will never work if success will always be true, don't put it there. I'll fix the code for you.

local DataStoreService = game:GetService("DataStoreService")
local msgstore = DataStoreService:GetDataStore("HouseStore") --Value that saves the message

game.Players.PlayerAdded:Connect(function(plr)
    local success, key = pcall(function()
        return msgstore:GetAsync("HouseStore-"..plr.UserId)
    end)

    if success then
        print("Success!)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local val = game.Workspace.House.Owner.Value
    mgstore:SetAsync("HouseStore-"..plr.UserId, "ohnub")
end)

I added playerAdded and remving, so whenever they leave it saves and whenever they join it loads.

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