Hi, I just changed some things in the code and it doesn’t work properly, what’s wrong?
I don’t want a leaderstats folder or anything like that, just that the folder is called playerFolder.
Server Script Service:
local DataStore = game:GetService("DataStoreService"):GetDataStore("myData")
game.Players.PlayerAdded:Connect(function(player)
local playerFolder = Instance.new("Folder", player)
playerFolder.Name = "playerFolder"
local Gold = Instance.new("IntValue", playerFolder) -- blah blah make your stats
Gold.Name = "myGold"
-- Load data
local data
local key = "Player_".. player.UserId
local success, errormessage = pcall(function()
data = DataStore:GetAsync(key)
end)
if success then
Gold.Value = data
elseif data == nil then
Gold.Value = 0
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "Player_".. player.UserId
local data = player.playerFolder.Gold.Value
DataStore:SetAsync(key, data)
end)
The problem is that when I go to roblox player and run the below command f9, it doesn’t show me my int value and neither can it do + = 1 in the int value
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Store = DataStoreService:GetDataStore("data")
game:GetService("Players").PlayerAdded:Connect(function(Player)
local Folder = Instance.new("Folder",Player)
Folder.Name = "PlayerFolder"
local Gold = Instance.new("IntValue",Folder)
Gold.Name = "Gold"
local success, currentPoints = pcall(function()
return Store:GetAsync(Player.UserId)
end)
if success then
Gold.Value = currentPoints
end
Gold.Changed:connect(function()
pcall(function()
Store:SetAsync(Player.UserId, Gold.Value)
end)
end)
end)
Sadly PlayerRemoved sometimes doesn’t always function so you have to use BindToClose which delays the close time, I hate that. What I do is I check for changes in the value.
-- SAVEDATA
local DataStore = game:GetService("DataStoreService"):GetDataStore("myData")
game.Players.PlayerAdded:Connect(function(player)
local playerFolder = Instance.new("Folder", player)
playerFolder.Name = "playerFolder"
local Gold = Instance.new("IntValue", playerFolder) -- blah blah make your stats
-- Load data
local data
local key = "Player_".. player.UserId
local success, errormessage = pcall(function()
data = DataStore:GetAsync(key)
end)
if success then
Gold.Value = data
elseif data == nil then
Gold.Value = 0
print("gold has been saved.")
else
print("error saving data, error saving data!!!")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "Player_".. player.UserId
local data = player.playerFolder.Gold.Value
DataStore:SetAsync(key, data)
end)
You should check firstly whats in Datastore’s folder of player.
Anyways, i think the issue is below the " local Gold " you didn’t gave it a name
( Actually to save that instance )
– SAVEDATA
local DataStore = game:GetService(“DataStoreService”)
local PlayerData = DataStore:GetDataStore(“playerGold”)
game.Players.PlayerAdded:Connect(function(player)
local playerFolder = Instance.new("Folder", player)
playerFolder.Name = "Data"
local Gold = Instance.new("IntValue", playerFolder) -- blah blah make your stats
Gold.Name = "Gold"
-- Load data
local data
local key = "Player_".. player.UserId
local success, errormessage = pcall(function()
data = DataStore:GetAsync(key)
end)
if success then
Gold.Value = data
elseif data == nil then
Gold.Value = 0
print("gold has been saved.")
else
print("error saving data, error saving data!!!")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = “Player_”… player.UserId
local data = player.playerFolder.Gold.Value
DataStore:SetAsync(key, data)
In roblox studio it makes me print “gold has been saved.” But in roblox player I get this error, I have the API activated, I don’t know what is wrong …