• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community
  • The forum is currently open to new registrations. The registration will close for a short period of time when reaching 500 active members, to upgrade the server resources.

Roblox Trouble with Roblox DataStore (1 Viewer)

I am attempting a simple task of updating a player's all-time win totals. Here is the code I used based on tutorials I have watched:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats

local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-wins")
end)

if success then
wins.Value = data
else
print("There was an error while getting your data")
warn(errormessage)
end
end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-wins",player.leaderstats.Wins.Value)
end)

if success then
print("Player Data successfully saved")
else
print("There was an error when saving data")
warn(errormessage)
end
end)

When a player wins a round in my game, I have been able to identify the winner via a variable called 'winning player.' So the command I have tried to update the player's win total is as follows:

game.Players.winningplayer.leaderstats.Wins.Value = game.Players.winningplayer.leaderstats.Wins.Value + 1

THE PROBLEM

Unfortunately when this happens I get an error saying: winningplayer is not a valid member of Player "Players"
Would really love help with this. Many thanks!
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
Is the field name 'winning player', with the space in between? If that is the case, you have to address it like this:

game.Players['winning player'].leaderstats.Wins.Value = ...
 

kevintdame

Newcomer
Joined
Dec 24, 2020
Messages
11
Reaction score
0
Is the field name 'winning player', with the space in between? If that is the case, you have to address it like this:

game.Players['winning player'].leaderstats.Wins.Value = ...
no there is no space in the variable in the script.
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
As the error code says, the table game.Players has no member (that is, no field) named that way.
 

kevintdame

Newcomer
Joined
Dec 24, 2020
Messages
11
Reaction score
0
As the error code says, the table game.Players has no member (that is, no field) named that way.
any suggestions for a better command to update the players win total? I know the winning player (as defined by the variable winningplayer) and I just want to update the 'Wins' value under the leaderstats folder for that player. But I am having trouble figuring out what the command / syntax should be. Based on tutorials I have watched I thought it would have been: game.Players.winningplayer.leaderstats.Wins.Value
 

kevintdame

Newcomer
Joined
Dec 24, 2020
Messages
11
Reaction score
0
Here are some of the tests I have run:

When I used the script below it returned the error: winningplayer is not a valid member of Player "Players"
game.Players.winningplayer.leaderstats.Wins.Value = game.Players.winningplayer.leaderstats.Wins.Value + 1

When I used the script below it returned the error: attempt to index nil with 'Wins'
winningplayer.leaderstats.Wins.Value = winningplayer.leaderstats.Wins.Value + 1

As a test I inserted my own player name (player123) and it updated my win total just fine.
game.Players.player123.leaderstats.Wins.Value = game.Players.player123.leaderstats.Wins.Value + 1
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
I don't know much about Roblox, but if I were you I'd stop doing tests and I'd investigate to understand better how the data is organized.

When you do something like this:

Lua:
game.Players.winningplayer.leaderstats.Wins.Value = game.Players.winningplayer.leaderstats.Wins.Value + 1

you are assuming that the table 'game' has a field called 'Players' which is also a table, that has a field called 'winningplayer',
also a table, that has a field called 'leaderstats', also a table, that has a field called 'Wins', also a table, that has a field called 'Value',
which is a number that you are attempting to increment.

Is your assumption correct? Depending on your tests, it seems that it is not.

To see which fields has a table you can either look up in a manual (maybe Roblox's? idk), or you can list them using Lua's pairs() function.
 

kevintdame

Newcomer
Joined
Dec 24, 2020
Messages
11
Reaction score
0
As a test I inserted my own player name (player123) and it updated my win total just fine.
game.Players.player123.leaderstats.Wins.Value = game.Players.player123.leaderstats.Wins.Value + 1

So doesn't this imply that the table 'game' has a field called 'Players' which is also a table, that has a field called (in this case my player's name), also a table, that has a field called 'leaderstats', also a table, that has a field called 'Wins', also a table, that has a field called 'Value',
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
As a test I inserted my own player name (player123) and it updated my win total just fine.
game.Players.player123.leaderstats.Wins.Value = game.Players.player123.leaderstats.Wins.Value + 1

So doesn't this imply that the table 'game' has a field called 'Players' which is also a table, that has a field called (in this case my player's name), also a table, that has a field called 'leaderstats', also a table, that has a field called 'Wins', also a table, that has a field called 'Value',

Now I get it. Try this:

game.Players[winningplayer].leaderstats.Wins.Value = game.Players[winningplayer].leaderstats.Wins.Value + 1

(and if it works, try to understand why game.Players[winningplayer] and game.Players.winningplayer are not the same thing)
 

kevintdame

Newcomer
Joined
Dec 24, 2020
Messages
11
Reaction score
0
Appreciate it! Is it because [winningplayer] is a position within a table whereas .winningplayer is a sub-folder?
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
Nope. It's because game.Players.winningplayer is equivalent to game.Players['winningplayer'].
That is, the name of the field is the string 'winningplayer'.

On the other hand, game.Players[winningplayer] is equivalent to game.Players['player123'], assuming that
winningplayer='player123'.

What you want to do is to access the field whose name is the value of the variable winningplayer, so the latter
is the way to go.
 

nathanmark

Newcomer
Joined
Mar 23, 2021
Messages
2
Reaction score
1
I am getting an error on roblox while playing. The code is 524, Can someone help me out?
 

damian

Newcomer
Joined
Jan 10, 2020
Messages
8
Reaction score
7
Location
Illinois
I am getting an error on roblox while playing. The code is 524, Can someone help me out?
This is an authorization error, you're either trying to play on a VIP server you don't have access to or are playing as a guest (the ability to do this was removed a couple of years ago). Try turning off any VPNs and make sure you're logged in. This can also be the server's fault, wait a couple minutes and try again.
 
Top