Hello there this is my first topic here, so I hope It goes well
My issue is that I get a datastore from youtube and dev hub and it works with ppl I get from but with me no!
I tried more than 10 scripts!
But any of the scripts didnât work with me, and it works with people I get from
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue") --Sets up value for leaderstats
gold.Name = "Gold"
gold.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- Data exists for this player
print("Data loaded")
gold.Value = data
else
-- Data store is working, but no current data for this player
print(player.Name..(" Has no Data!"))
gold.Value = 0
end
end
local function onPlayerExit(player) --Runs when players exit
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
print('Data Saved!')
playerData:SetAsync(playerUserId, player.leaderstats.Gold.Value) --Saves player data
end)
if not success then
print("Data Could Not Save!")
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
You should add more pcalls to your script. Try this:
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue") --Sets up value for leaderstats
gold.Name = "Gold"
gold.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data
local success, errmessage = pcall(function()
data = playerData:GetAsync(playerUserId) --Checks if player has stored data
end)
if success then -- The datastore works
if data then -- Data exists for this player
print("Data loaded")
gold.Value = data
else -- The player has no data
print(player.Name..(" has no Data!"))
gold.Value = 0
end
else -- The datastore failed
print(errmessage)
end
end
local function onPlayerExit(player) --Runs when players exit
local playerUserId = "Player_" .. player.UserId
local success, err = pcall(function()
playerData:SetAsync(playerUserId, player.leaderstats.Gold.Value) --Saves player data
end)
if not success then
print("Data Could Not Save!")
warn('Could not save data!')
else
print('Data Saved!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
It is not saved since, in a normal test, you are only in studio and surely the server shuts down before it is saved, you must use BindToClose, try this.
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
game:GetService("Players").PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Parent = leaderstats
local success, err = pcall(function()
gold.Value = playerData:GetAsync("Player_"..player.UserId) or 0
end)
if not success then print("Datastore error - GetAsync:", err) end
end)
function Save(player)
local success, err = pcall(function()
playerData:SetAsync("Player_"..player.UserId, player.leaderstats.Gold.Value)
end)
if not success then print("Datastore error - SetAsync:", err) end
end
game:GetService("Players").PlayerRemoving:Connect(Save)
game:BindToClose(function()
for _,player in pairs(game:GetService("Players"):GetPlayers()) do
coroutine.wrap(Save)(player)
end
wait(2)
end)
Saving doesnât often work in studio because it doesnât give it enough time. Saving only works when the game is published and to test it go onto the website and try it like a regular game, if that then doesnât work you have an issue in your code.
When Iâm testing saving in studio to give it enough time you can kick yourself, the command for that is:
It still didnât work, also can u tell me where to put it, so I make sure I put it in the right place
game:GetService("Players").PlayerRemoving:Connect(Save)
game:BindToClose(function()
for _,player in pairs(game:GetService("Players"):GetPlayers()) do
coroutine.wrap(Save)(player)
game.Players.["0MAR280"]:Kick()
end
wait(2)
end)
That isnât quite what I mean. Have you tried using it from the website? What I was saying was testing I use the Command Bar to type a kick command in that gives it enough saving time.
Lol, itâs because it doesnât like your name, I canât remember how to fix that to get past it but one option could be while previewing in explorer click âPlayersâ and then click your player and change itâs name to something else such as âMARâ then do