generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Basic DataStore thigny need help

Previous Thread :: Next Thread 
Brick_Wizard is not online. Brick_Wizard
Joined: 15 Mar 2016
Total Posts: 2072
23 Apr 2016 01:46 AM
I am terrible when it comes to datastore, this script is supposed to save all the tools the player has into a table which is saved with a datastore, then when he joins game give it back to him,

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local PlayersData = DataStore:GetDataStore("ToolsSave")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Players.PlayerAdded:connect(function(Player)
local Key = "user_" ..Player.userId
PlayersData:GetAsync(Key, function(Value)
if Value then
for _, DataTool in pairs (Value) do
DataTool.Parent = Player.Backpack
end
end
end)
end)

Players.PlayerRemoving:connect(function(Player)
local PlayersGear = {}
for _, Tool in pairs (Player:WaitForCheck("Backpack"):GetChildren()) do
table.insert(PlayersGear, Tool.Name)
if Player and Player.Character then
for _, CharacterChild in pairs (Player.Character:GetChildren()) do
if CharacterChild:IsA "Tool" then
table.insert(PlayersGear, CharacterChild.Name)
end
end
end
end
local Key = "user_" ..Player.userId
PlayersData:UpdateAsync(Key, function(OldToolTable)
local NewToolTable = OldToolTable or 0
NewToolTable = PlayersGear
return NewToolTable
end)
end)


Report Abuse
nickcool9999 is not online. nickcool9999
Joined: 04 Dec 2007
Total Posts: 1528
23 Apr 2016 03:51 AM
GetAsync doesnt have 2 arguments. the Async part is put into a variable which you use to then manipulate it.

putting a second argument there just creates a function address that wont ever be called.

In the update one the function is called to overwrite the old value.
Report Abuse
Brick_Wizard is not online. Brick_Wizard
Joined: 15 Mar 2016
Total Posts: 2072
23 Apr 2016 04:28 AM
This doesn't work either (I changed it to print and checked it via F9 Dev console)

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local PlayersData = DataStore:GetDataStore("ToolsSave")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Players.PlayerAdded:connect(function(Player)
local Key = "user_" ..Player.userId
local Data = PlayersData:GetAsync(Key)
if Data then
for _, DataTool in pairs (Data) do
--DataTool.Parent = Player.Backpack
print(DataTool.Name)
end
else
local PlayersGear = {}
Data = PlayersData:SetAsync(Key, PlayersGear)
end
end)

Players.PlayerRemoving:connect(function(Player)
local PlayersGear = {}
for _, Tool in pairs (Player:WaitForCheck("Backpack"):GetChildren()) do
table.insert(PlayersGear, Tool.Name)
if Player and Player.Character then
for _, CharacterChild in pairs (Player.Character:GetChildren()) do
if CharacterChild:IsA "Tool" then
table.insert(PlayersGear, CharacterChild.Name)
end
end
end
end
local Key = "user_" ..Player.userId
PlayersData:SetAsync(Key, PlayersGear)
end)


Report Abuse
AdvancedObjects is not online. AdvancedObjects
Joined: 11 Dec 2012
Total Posts: 3354
23 Apr 2016 04:44 AM
#code
local players = game.Players
local datastore = game:GetService("DataStoreService")
local playersdata = datastore:GetDataStore("ToolsSave")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:connect(function(player)
local key = "user_" ..player.userId
if key then
local async = playersdata:GetAsync(key)
if async then
for i, v in pairs (async) do
print(v)
end
else
local pie = {}
playersdata:SetAsync(key, pie)
end
end
end)

game.Players.PlayerRemoving:connect(function(player)
local key = "user_" ..player.userId
local gear = {}
local async = playersdata:GetAsync(key)
local backpack = player.Backpack
for i, v in pairs (backpack:GetChildren()) do
if v and v.ClassName == "Tool" and async[v.Name] == nil then
table.insert(gear, v.Name)
end
end
playersdata:SetAsync(key, gear)
end)




Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image