Hello, I’m trying to make a way so I can get the value the player has. But, I ran into a problem. How do I get that one value without searching through the whole entire data store? I serarched up already, and the only “solution” was using HTTP requests. Is there any other way?
Thanks.
Edit: I guess the only solution is to loop through all ordered data store. Welp. Thanks for your help.
you should just be able to get the data through OrderedDatastore:GetAsync(players_key_here)
1 Like
Thanks! I’ll try that asap!!!
Hmm nothing saved. Here’s my script:
--Variables
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local cashData = DataStoreService:GetOrderedDataStore("CashData")
--Player Added Functino
function onPlayerAdded(player)
--leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
--Give Data
local PlayerUserID = "Player_"..player.UserId
local cashPlayerData = cashData:GetAsync(PlayerUserID)
if cashPlayerData then
Cash.Value = cashPlayerData
else
Cash.Value = 0
end
end
-- On Player Removing
function onPlayerRemoving(player)
local leaderstats = player:WaitForChild("leaderstats")
--Set Data
local sucess,err = pcall(function()
local PlayerUserID = 'Player_'..player.UserId
cashData:UpdateAsync(PlayerUserID, leaderstats:WaitForChild("Cash").Value)
end)
end
-- On Bind To Close
function onBindToClose()
for _, player in pairs(Players:GetChildren()) do
local leaderstats = player:WaitForChild("leaderstats")
--Set Data
local sucess,err = pcall(function()
local PlayerUserID = 'Player_'..player.UserId
cashData:UpdateAsync(PlayerUserID, leaderstats:WaitForChild("Cash").Value)
end)
end
end
-- Connect Functions
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onBindToClose)