Hello,
For several weeks now I have faced a problem that I can not solve, I try to understand but despite my best efforts I can not move forward.
I did a lot of testing, tested several scripts, scoured this forum for hours, searched the internet etc … but nothing.
I created a script which is supposed to record data in the datastore such as coin, experience and rank values as well as “shirt” and “pants” clothing but the problem is as follows:
This script works perfectly during my tests on Roblox studio, but once published online it no longer works!
At first I thought it was a problem caused by the datastore but I thought of connecting another player to observe if I had an error on this subject, but it turns out that the datastore is not at all questioned.
I am new to script but want to learn, please be forgiving if you find my script to be simplistic and awkward.
Why such an inconsistency in behavior between these two operating modes?
how could i fix this or work around the problem ?
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData02aa")
local function onPlayerJoin(player) -- Runs when players join
local character = player.Character or player.CharacterAdded:Wait()
local leaderstats = Instance.new("Folder",player) --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
local money = Instance.new("IntValue",leaderstats) --Sets up value for leaderstats
money.Name = "Money"
local exp = Instance.new("IntValue",leaderstats) --Sets up value for leaderstats
exp.Name = "Experience"
local rank = Instance.new('StringValue',leaderstats)
rank.Name = "Rank"
local foundShirt = player.Character:FindFirstChild("Shirt") -- Tries to find Shirt
if not foundShirt then -- if there is no shirt
warn("No shirt found, creating for "..player.Name)
local newShirt = Instance.new("Shirt",player.Character)
newShirt.Name = "Shirt"
end
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if not data then -- Data store is working, but no current data for this player
money.Value = 0
exp.Value = 0
rank.Value = "-"
else
money.Value = data['Money']
exp.Value = data['Experience']
rank.Value = data['Rank']
player.Character.Shirt.ShirtTemplate = data['Shirt']
player.Character.Pants.PantsTemplate = data['Pants']
player.Character.Head.face.Texture = data['Face']
end
print(data)
end
local function SaveData(player) --Runs when players exit
local player_data = {
['Money']=player.leaderstats.Money.Value,
['Experience']=player.leaderstats.Experience.Value,
['Rank']=player.leaderstats.Rank.Value,
['Shirt']=player.Character:WaitForChild("Shirt").ShirtTemplate,--player.Character.Shirt.ShirtTemplate,
['Pants']=player.Character:WaitForChild("Pants").PantsTemplate,--player.Character.Pants.PantsTemplate,
['Face']=player.Character.Head.face.Texture
}
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_data)--Saves player data
end)
print(player_data)
if not success then
warn('Could not save data! :(')
else
warn('Data SAVED :)')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player)
end
end)
Any suggestions would be welcome, thank you in advance.