Hey Everyone,
I’m making a datastore script for my wins for my story game.
It’s supposed to display the amount of wins under the player’s name.
The players name displays fine, but the wins don’t.
Here is my script:
local DataStoreService = game:GetService('DataStoreService')
local WinsDataStore = DataStoreService:GetDataStore('Wins')
script.Parent.UserId:GetPropertyChangedSignal('Value'):Connect(function()
local PlayerName = game.Players:GetNameFromUserIdAsync(script.Parent.UserId.Value)
local Player = game.Players:GetPlayerByUserId(script.Parent.UserId.Value)
local Data = WinsDataStore:GetAsync(script.Parent.UserId.Value)
if Data then
script.Parent.Parent.Parent.Podium.MiddleBlock.SurfaceGui.PlayerName.Text = PlayerName
script.Parent.Parent.Parent.Podium.MiddleBlock.SurfaceGui.WinsAmount.Text = Data.Stats
end
end)
local Players = game:GetService('Players')
local DataStoreService = game:GetService('DataStoreService')
local WinsDataStore = DataStoreService:GetDataStore('Wins')
Players.PlayerAdded:Connect(function(Player)
local Stats = Instance.new('Folder')
Stats.Name = 'leaderstats'
Stats.Parent = Player
local Wins = Instance.new('IntValue')
Wins.Name = 'Wins'
Wins.Parent = Stats
local Data = WinsDataStore:GetAsync(Player.UserId)
if Data then
for name, value in pairs(Data.Stats) do
Stats[name].Value = value
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
local SaveData = {Stats = {}}
for _, stat in pairs(Player.leaderstats:GetChildren()) do
SaveData.Stats[stat.Name] = stat.Value
end
WinsDataStore:SetAsync(Player.UserId,SaveData)
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
local SaveData = {Stats = {}}
for _,stat in pairs(Player.leaderstats:GetChildren()) do
SaveData.Stats[stat.Name] = stat.Value
end
WinsDataStore:SetAsync(Player.UserId,SaveData)
end
wait(2)
end)
I’m bad at datastores(I made this with a video) This is my datastore script.
I also get the error:
[12:04:16.746 - Workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs.1.Script:14: invalid argument #3 (string expected, got table)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
local SaveData = {}
for _,stat in pairs(Player.leaderstats:GetChildren()) do
SaveData[stat.Name] = stat.Value
end
WinsDataStore:SetAsync(Player.UserId,SaveData)
end
wait(2)
end)
Another issue is, after setting my wins to 0, when I rejoin it still comes up as the old amount.
This is the edited script:
local Players = game:GetService('Players')
local DataStoreService = game:GetService('DataStoreService')
local WinsDataStore = DataStoreService:GetDataStore('Wins')
Players.PlayerAdded:Connect(function(Player)
local Stats = Instance.new('Folder')
Stats.Name = 'leaderstats'
Stats.Parent = Player
local Wins = Instance.new('IntValue')
Wins.Name = 'Wins'
Wins.Parent = Stats
local Data = WinsDataStore:GetAsync(Player.UserId)
if Data then
for name, value in pairs(Data.Stats) do
Stats[name].Value = value
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
for _, Player in pairs(game.Players:GetPlayers()) do
local SaveData = {}
for _,stat in pairs(Player.leaderstats:GetChildren()) do
SaveData[stat.Name] = stat.Value
end
WinsDataStore:SetAsync(Player.UserId,SaveData)
end
wait(2)
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
local SaveData = {}
for _,stat in pairs(Player.leaderstats:GetChildren()) do
SaveData[stat.Name] = stat.Value
end
WinsDataStore:SetAsync(Player.UserId,SaveData)
end
wait(2)
end)
@Quwanterz Nevermind, I just changed my script to:
local DataStoreService = game:GetService('DataStoreService')
local WinsDataStore = DataStoreService:GetDataStore('Wins')
script.Parent.UserId:GetPropertyChangedSignal('Value'):Connect(function()
local PlayerName = game.Players:GetNameFromUserIdAsync(script.Parent.UserId.Value)
local Player = game.Players:GetPlayerByUserId(script.Parent.UserId.Value)
local Data = WinsDataStore:GetAsync(script.Parent.UserId.Value)
if Data then
script.Parent.Parent.Parent.Podium.MiddleBlock.SurfaceGui.PlayerName.Text = PlayerName
script.Parent.Parent.Parent.Podium.MiddleBlock.SurfaceGui.WinsAmount.Text = Data.Stats.Wins
end
end)