I am new to DataStore
, I've read the wiki and a few example script's but I am not sure if I am using DataStore
right, can someone tell me if I am and if I'm not can you tell me what I am doing wrong about it?
01 | wait( 1 ) |
02 |
03 | local player = game.Players.LocalPlayer |
04 | local stats = player:WaitForChild( "leaderstats" ) |
05 | local cash = stats:WaitForChild( "Cash" ) |
06 | local service = game:GetService( "DataStoreService" ):GetDataStore( "GameValues" ) |
07 |
08 | cash.Changed:connect( function () |
09 | service:SetAsync( "Cash" ,cash.Value) |
10 | print ( "Updated cash to " ..cash.Value) |
11 | end ) |
1. It's because you have to use a key. Like, for example
1 | local key = "Playerkey" ..player.userID |
Each key has to be different so you can access the datastore, i believe.
So basically, you have to do
1 | service:SetAsync(key,cash.Value) |
2. Guessing by local player = game.Players.LocalPlayer, It's a Localscript.
You can't use DataStoreService in a localscript, only in a serverscript.
If you have any questions, i'll be happy to reply!