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

Help on how to use DataStores, retrieving and storing?

Asked by 8 years ago
Edited 8 years ago

I'm trying to make a saving database of someone's department in a group, I have a GUI which successfully changes the Department depending on the entry, but then here's my hard part, how to save it to a DataStore and retrieve it afterwards.

My attempt (Store)

local DataStore = game:GetService("DataStoreService"):GetDataStore("Departments")
local key = "user_" .. script.Parent.Parent.userId

script.Parent.Parent:WaitForChild("leaderstats").Department.Changed:connect(function()
    DataStore:UpdateAsync(key,script.Parent.Parent:WaitForChild("leaderstats").Department.Value)
--This is where it stops working, "Unable to cast value to function"
end)

(Retrieve)

local group = 1234 --GroupID
local DataStore = game:GetService("DataStoreService"):GetDataStore("Departments")

game.Players.PlayerAdded:connect(function(player)
    local leadrsts = Instance.new("IntValue",player)
    leadrsts .Name = "leaderstats"
    local rank = Instance.new("StringValue",leadrsts )
    rank.Name = "Rank"
    local department = Instance.new("StringValue",leadrsts )
    department.Name = "Department"  
    local key = "user_" .. player.userId    
    department.Value = DataStore:GetAsync(key)
end)

Any help?

They're both server scripts

1 answer

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

The second argument is supposed to be a function, which is maybe why it says error casting it to a function.

--looking at what you did, it looks like you want it to be updated to the Deparment.Value 
--idk if this would work though
local DataStore = game:GetService("DataStoreService"):GetDataStore("Departments")
local key = "user_" .. script.Parent.Parent.userId
local leaderstats = script.Parent.Parent:WaitForChild("leaderstats")
local Deparment = leaderstats.Department
Deparment.Changed:connect(function()
    DataStore:UpdateAsync(key, function(old_value)
        local new_value = Deparment.Value
        print(string.format("The old value is %s\n The new value is %s", tostring(old_value), tostring(new_value))) -- I tostringed them because I do not know if this if it is an IntValue or an ObjectValue etc.
        return new_value
    end)
end)


0
Unless is a deeper reason for this happening + the reason I gave TheLowOne 85 — 8y
Ad

Answer this question