Inconsistency of operation between studio and game published online

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!
Image2rstu

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.

Why are you trying to save shirt/pants/face anyways?, the Error is happening because it’s trying to access a non-existent Character as the player left.

2 Likes

what you say makes sense indeed, in this case do I also have to store the templates in a new instance, like for the money values to save them when the player disconnects? or should I use another method? what misleads me is that it works in the studio.

It works in the studio cause it’s basically instantaneous like no delay(I’m not entirely sure, but some stuff will always be different on studio), But yea you can use values to store them I guess, just use any method that you’re most comfortable with. You can even use the newly added Feature “Attributes” if you wanna know about it just use the search engine.

1 Like

thank you for your advice, it gives me a better view of the paths I should follow, I will be sure to post my results when it all works

edit:
Finally I opted for a completely different solution, I separated the saving into two parts, the scores are recorded when the player exits and the clothes are recorded manually with a GUI button, which allows the player to choose. and for me more flexibility for future developments.
Everything works perfectly
Thanks for your help