Something's wrong with my data saving script

Hello, so today I tried to make a data saving script which I’m super bad at understanding things in the script. (I barely learn any scripting and I only know the basics)

  1. What do you want to achieve? Ways to fix my data saving script.

  2. What is the issue?

  3. What solutions have you tried so far? I have tried looking for solutions but I don’t find anything useful and doubt anybody even have the same problem as mine in the first place. I don’t even understand my own script.

Script:

local DSS = game:GetService("DataStoreService")
local mDS = DSS:GetDataStore("mDS")


game.Players.PlayerAdded:Connect(function(player)
	local PLRData = Instance.new("Folder", player)
	PLRData.Name = "PlayerData"
	
	local currentSpirit = Instance.new("StringValue", PLRData)
	currentSpirit.Name = "Spirit"
	
	local data
	local success, errormessage = pcall(function()
		data = mDS:GetAsync(player.UserId.."-spirit")
	end)
	
	if success then
		currentSpirit.Value = data
	else
		print("Error getting data. Please check errormessage")
		warn(errormessage) --This line broke
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		mDS:SetAsync(player.UserId.."-spirit",player.PlayerData.SpiritName.Value)
	end)
	
	if success then
		print("Data saved")
	else
		print("Failed! Please check errormessage")
		warn(errormessage)
	end
end)

Note that I’m trying to understand and getting the hang of scripting and this is my first step, every helping hands possible is appreciated. Thank you!

Problem is solved but no replies are suitable for the solution mark.

If you are using Team Create and you don’t own the game, Studio will reject it with a Forbidden error. I’m pretty sure there is a bug report on this already and I will edit this post once I find it.

The game will still work if you play it in a regular game though.

1 Like

Yes, I used Team Create and I am the owner of the game. I don’t know why and how this happens, I used Team Create because it autosaves in case my PC shut itself down. It always do that and I lose majority of scripts I did.

1 Like

Have you enabled API services in the Game Settings?

1 Like

It’s still the same error listed on the post. I have disabled Team Create too.

Solved.