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
 

Re: datastore

Previous Thread :: Next Thread 
AustinNally is not online. AustinNally
Joined: 18 Dec 2012
Total Posts: 163
10 Jul 2016 09:15 PM
how come if I change the key in my datastore, it breaks and won't save?


Report Abuse
ClockworkSquirrel is not online. ClockworkSquirrel
Joined: 06 Jul 2009
Total Posts: 1531
10 Jul 2016 09:17 PM
We kind of need any errors from the output (if in-game, press F9), and some code snippets.
Report Abuse
AustinNally is not online. AustinNally
Joined: 18 Dec 2012
Total Posts: 163
10 Jul 2016 09:17 PM
That's the thing... There are no errors.


Report Abuse
ClockworkSquirrel is not online. ClockworkSquirrel
Joined: 06 Jul 2009
Total Posts: 1531
10 Jul 2016 09:26 PM
Still wouldn't hurt to post a few code snippets.
Report Abuse
AustinNally is not online. AustinNally
Joined: 18 Dec 2012
Total Posts: 163
10 Jul 2016 09:30 PM
local DataStore = game:GetService('DataStoreService'):GetDataStore('PlayerStatistics')
local Players = game:GetService('Players')

-- Datastore - Saving / Loading

local UpdateDataTime = 60

Players.PlayerAdded:connect(function(Player)

-- Create a leaderboard
local Leaderboard = Instance.new('Folder', Player)
Leaderboard.Name = 'leaderstats'

-- Create a value
local Stage = Instance.new('IntValue', Leaderboard)
Stage.Name = 'Stage'
Stage.Value = 1

repeat wait() until Stage.Value == 1

local Key = 'TestingDataStore-' .. Player.UserId

local SavedValues = DataStore:GetAsync(Key)

if SavedValues then
Stage.Value = SavedValues
else
DataStore:SetAsync(Key, Stage.Value)
end

end)

Players.PlayerRemoving:connect(function(Player)
-- Save Data on Exit

local Key = 'TestingDataStore-' .. Player.UserId
local ValuesToSave = (Player.leaderstats.Stage.Value)
DataStore:SetAsync(Key, ValuesToSave)
end)


Report Abuse
AustinNally is not online. AustinNally
Joined: 18 Dec 2012
Total Posts: 163
10 Jul 2016 09:31 PM
If I add or remove anything to the string in my Key it breaks. I have no idea why.


Report Abuse
AustinNally is not online. AustinNally
Joined: 18 Dec 2012
Total Posts: 163
10 Jul 2016 09:38 PM
[ Content Deleted ]
Report Abuse
ClockworkSquirrel is not online. ClockworkSquirrel
Joined: 06 Jul 2009
Total Posts: 1531
10 Jul 2016 09:39 PM
I will add this to my favourites and come back to it in a few hours on my laptop, as I am going to sleep now, since it is 03:38 here. If someone has helped you figure it out in the meantime, great.

However, as I scanned through your code quickly, I noticed you were using the Player's User ID. Capitalisation does matter, and the User ID property of a Player actually has a lowercase "u," which means they property should be .userId.

e.g. game.Players.LocalPlayer.userId

If this doesn't fix your problem, as I said, I'll be back to check in a few hours when I wake up and am not on my phone.
Report Abuse
ClockworkSquirrel is not online. ClockworkSquirrel
Joined: 06 Jul 2009
Total Posts: 1531
10 Jul 2016 09:41 PM
Also, store the key variable at the top of your script with your other variables, instead of inside the events. This way, you only need to change one copy of the variable, rather than having to change it twice.
Report Abuse
ClockworkSquirrel is not online. ClockworkSquirrel
Joined: 06 Jul 2009
Total Posts: 1531
10 Jul 2016 09:43 PM
In fact, I'll come back and clean up/optimise your code for you, since this could be a lot simpler.
Report Abuse
Mattyys is not online. Mattyys
Joined: 21 Aug 2013
Total Posts: 57
10 Jul 2016 09:46 PM
The reason I put it in the function is because this is a serverscript in ServerScriptService


Report Abuse
ClockworkSquirrel is not online. ClockworkSquirrel
Joined: 06 Jul 2009
Total Posts: 1531
10 Jul 2016 09:51 PM
That's irrelevant. It will still work if you move the "key" variable outside the functions.
Report Abuse
AustinNally is not online. AustinNally
Joined: 18 Dec 2012
Total Posts: 163
10 Jul 2016 09:54 PM
How is it irrelevant? I'm using Player.userId in the Key. I am defining 'Player' in the function.


Report Abuse
ClockworkSquirrel is not online. ClockworkSquirrel
Joined: 06 Jul 2009
Total Posts: 1531
10 Jul 2016 09:56 PM
Okay, that's just me being tired and stupid. It's relevant, yes. 😂
Report Abuse
iDerpyDogez is not online. iDerpyDogez
Joined: 05 Feb 2012
Total Posts: 1781
10 Jul 2016 10:10 PM
@clock im pretty sure "userId" is deprecated, UserId is the new one


#code --[[ hi friends i like turtles ]]--
Report Abuse
ClockworkSquirrel is not online. ClockworkSquirrel
Joined: 06 Jul 2009
Total Posts: 1531
11 Jul 2016 06:02 AM
Try this:

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerStatistics")
local Players, UpdateDataTime = game:GetService("Players"), 60
local Key = "TestingDataStore_"

Players.PlayerAdded:connect(function(Player)
local Leaderboard = Instance.new("Folder", Player)
Leaderboard.Name = "leaderstats"

local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Parent = Leaderboard

local SavedValues = DataStore:GetAsync(Key..Player.UserId)
Stage.Value = SavedValues or 1
end)

Players.PlayerRemoving:connect(function(Player)
DataStore:SetAsync(Key..Player.UserId, Player.leaderstats.Stage.Value)
end)
Report Abuse
AustinNally is not online. AustinNally
Joined: 18 Dec 2012
Total Posts: 163
11 Jul 2016 11:13 AM
Actually 'userId' and 'UserId' both work.


Now, I found another problem. However, it has nothing to do with DataStore.
It's actually DevProducts.

For some reason everytime I join the game it adds 5 to Stage.Value

(I have purchased this in the past just to test it out.)

Here's the script:

game:GetService('MarketplaceService').ProcessReceipt = function(ReceiptInfo)
for _, Player in pairs(game.Players:GetChildren()) do
if Player.UserId == ReceiptInfo.PlayerId then

if ReceiptInfo.ProductId == 36103824 then -- Skip stage
Player.leaderstats.Stage.Value = Player.leaderstats.Stage.Value + 1
Player:LoadCharacter()
end

end
end
end


Report Abuse
AustinNally is not online. AustinNally
Joined: 18 Dec 2012
Total Posts: 163
11 Jul 2016 11:31 AM
Fixed it... Forgot to return.

Question about DataStore though, how come it doesn't save a lower number?


Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
11 Jul 2016 11:45 AM
I shortened your code and made it more efficient so it would be easier to manage/read. If there's still an error it'll be much easier to find and debug it. Lemme know if there are any problems :)

In addition to that I made it easier to make more leaderstats that you can save in the future.

If there is anything you don't understand I'll be happy to explain the logic.


local DSS = game:GetService('DataStoreService')
local Stage = DSS:GetDataStore('Stage') --This can be reused for as many stats as you want. And example is commented out
--local Stat2 = DSS:GetDataStore('Stat_Name2')
local Players = game.Players

-- Datastore - Saving / Loading

Players.PlayerAdded:connect(function(Player)
local Leaderboard = Instance.new('Folder', Player)
Leaderboard.Name = 'leaderstats'

--This segment of code can be reused for as many stats as you want, as long as you follow what was said earlier on in this code. An example of a second stat is commented out
local Stage = Instance.new('IntValue', Leaderboard)
Stage.Name = 'Stage'
Stage.Value = Stage:GetAsync(Player.userId) or 1

--local Stage = Instance.new('IntValue', Leaderboard)
--Stage.Name = 'A second stat'
--Stage.Value = Stat2:GetAsync(Player.userId) or 1
end)

Players.PlayerRemoving:connect(function(Player)
local ValueToSave = Player.leaderstats.Stage.Value
Stage:SetAsync(Player.userId, ValueToSave)
end)

game.OnClose = function()wait(30)end
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
11 Jul 2016 11:48 AM
Dangit I forgot to show you you would save more stats as well, I only mentioned the loading. Well I'm sure you'll figure that out if you need it, it's not rocket science.


lol I just realized with all the comments it doesn't look shortened at all. If you only care about 1 stat here's the actual code rofl:


local DSS = game:GetService('DataStoreService')
local Stage = DSS:GetDataStore('Stage')
local Players = game.Players

Players.PlayerAdded:connect(function(Player)
local Leaderboard = Instance.new('Folder', Player)
Leaderboard.Name = 'leaderstats'

local Stage = Instance.new('IntValue', Leaderboard)
Stage.Name = 'Stage'
Stage.Value = Stage:GetAsync(Player.userId) or 1
end)

Players.PlayerRemoving:connect(function(Player)
local ValueToSave = Player.leaderstats.Stage.Value
Stage:SetAsync(Player.userId, ValueToSave)
end)

game.OnClose = function()wait(30)end
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
11 Jul 2016 11:51 AM
Clock makes a good point, my PlayerRemoving event can also be shortened:

local DSS = game:GetService('DataStoreService')
local Stage = DSS:GetDataStore('Stage') --You can combine this with the upper line if you don't plan to have more than one stat btw
local Players = game.Players

Players.PlayerAdded:connect(function(Player)
local Leaderboard = Instance.new('Folder', Player)
Leaderboard.Name = 'leaderstats'

local Stage = Instance.new('IntValue', Leaderboard)
Stage.Name = 'Stage'
Stage.Value = Stage:GetAsync(Player.userId) or 1
end)

Players.PlayerRemoving:connect(function(Player)
Stage:SetAsync(Player.UserId, Player.leaderstats.Stage.Value)
end)

game.OnClose = function()wait(30)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