DataStore doesn't even run

Hello, I tried making my first datastore script for my game and I hoped that it will work, but when I launch the game both in studio/in-game the script doesn’t even start to run. I have studio API Services enabled but for some reason it still doesn’t work. I wanted to test it but why the heck it doesn’t want to run. Please help. Thanks

Edit: I am also aware that it will break because I am pushing it over the datastore limits and it will get queued but I am not sure if you can give me some instructions to prevent it, but if you can then please say it, thanks.

Here is my script:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("ThisWillErrorDataStore")

game.Player.PlayerAdded:Connect(function(player)
	
	--Leaderstats
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Fertilizer = Instance.new("IntValue")
	Fertilizer.Name = "Fertilizer"
	Fertilizer.Parent = leaderstats
	Fertilizer.Value = 0
	
	local Food = Instance.new("IntValue")
	Food.Name = "Food"
	Food.Parent = leaderstats
	Food.Value = 0
	
	--Multipliers/Stats
	local InfoFolder = Instance.new("Folder", player)
	InfoFolder.Name = "InfoFolder"
	
	local ConvertAmount = Instance.new("StringValue", InfoFolder)
	ConvertAmount.Name = "ConvertAmount"
	ConvertAmount.Value = "100"
	
	local ConvertRate = Instance.new("StringValue", InfoFolder)
	ConvertRate.Name = "ConvertRate"
	ConvertRate.Value = "100%"
	
	local CriticalPower = Instance.new("StringValue", InfoFolder)
	CriticalPower.Name = "CriticalPower"
	CriticalPower.Value = "100%"
	
	local CriticalChance = Instance.new("StringValue", InfoFolder)
	CriticalChance.Name = "CriticalChance"
	CriticalChance.Value = "3%"
	
	local Luck = Instance.new("StringValue", InfoFolder)
	Luck.Name = "Luck"
	Luck.Value = "0"
	
	local InstantConvert = Instance.new("StringValue", InfoFolder)
	InstantConvert.Name = "InstantConvert"
	InstantConvert.Value = "100"
	
	local Defense = Instance.new("StringValue", InfoFolder)
	Defense.Name = "Defense"
	Defense.Value = "0%"
	
	local Jump = Instance.new("StringValue", InfoFolder)
	Jump.Name = "JumpPower"
	Jump.Value = "56"
	
	local Walkspeed = Instance.new("StringValue", InfoFolder)
	Walkspeed.Name = "Walkspeed"
	Walkspeed.Value = "20"
	
	local AttackMultiplier = Instance.new("StringValue", InfoFolder)
	AttackMultiplier.Name = "AttackMultiplier"
	AttackMultiplier.Value = "100%"
	
	local Capacity = Instance.new("StringValue", InfoFolder)
	Capacity.Name= "Capacity"
	Capacity.Value = "0"
	
	local CapacityMultiplier = Instance.new("StringValue", InfoFolder)
	CapacityMultiplier.Name = "CapacityMultiplier"
	CapacityMultiplier.Value = "100%"
	
	local playerUserId = "Player_"..player.UserId
	
	--Loading Data
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	
	
	if success then
		if data then
			Fertilizer.Value = data.Fertilizer
			Food.Value = data.Food
			ConvertAmount.Value = data.ConvertAmount
			ConvertRate.Value = data.ConvertRate
			CriticalPower.Value = data.CriticalPower
			CriticalChance.Value = data.CriticalChance
			Luck.Value = data.Luck
			InstantConvert.Value = data.InstantConvert
			Defense.Value = data.Defense
			Walkspeed.Value = data.Walkspeed
			Jump.Value = data.Jump
			AttackMultiplier.Value = data.AttackMultiplier
			Capacity.Value = data.Capacity
			CapacityMultiplier.Value = data.CapacityMultiplier
		end
		
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data ={
		Food = player.leaderstats.Food;
		Fertilizer = player.leaderstats.Fertilizer;
		ConvertAmount = player.InfoFolder.ConvertAmount;
		ConvertRate = player.InfoFolder.ConvertRate;
		CriticalPower = player.InfoFolder.CriticalPower;
		CriticalChance = player.InfoFolder.CriticalChance;
		Luck = player.InfoFolder.Luck;
		InstantConvert = player.InfoFolder.InstantConvert;
		Defense = player.InfoFolder.Defense;
		Walkspeed = player.InfoFolder.Walkspeed;
		Jump = player.InfoFolder.Jump;
		AttackMultiplier = player.InfoFolder.AttackMultiplier;
		Capacity = player.InfoFolder.Capacity;
		CapacityMultiplier = player.InfoFolder.CapacityMultiplier;
	}
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
	
	if success then
		print("Data Successfully saved!")
	else
		print("Error Detected")
		warn(errormessage)
	end
end)
1 Like

Does the script make the leaderstats folder and values for the player when they join? If it does and no data loads in, then you probably don’t have any data saved for the player.

No, the script doesnt even get service and create the leaderstat folders

Is the script in ServerScriptService?

Yes it is, I wouldn’t put it in workspace.

Can you make it print something on the first line to make sure the script runs?

Yea sure, I’ll try once I am home.

You’re doing game.Player.PlayerAdded

you should do:

game.Players.PlayerAdded

The service is Players not Player

1 Like

OH youre right… Thanks for helping