Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My datastore script does not work for unknown reason, I get no errors?

local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local kills = Instance.new("IntValue")
	kills.Parent = leaderstats
	kills.Name = "Kills"
	
	local Experience = Instance.new("StringValue")
	Experience.Parent = leaderstats
	Experience.Name = "Experience"

	
	local SurvivalTime = Instance.new("IntValue")
	SurvivalTime.Parent = leaderstats
	SurvivalTime.Name = "Survived"
	
	local Money = Instance.new("IntValue")
	Money.Parent = leaderstats
 	Money.Name = "BackDollars"	
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")

			humanoid.Died:Connect(function(died)
				local tag = humanoid:FindFirstChild("creator")
				local killer = tag.Value
				if tag and killer	then
					killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
					killer.leaderstats:FindFirstChild("BackDollars").Value += 1
				end
			end)
	end)
	local playerUserId = "Player_"..player.UserId
	
	local data = playerData:GetAsync(playerUserId)
	if data then
		kills.Value = data["Kills"]
		Experience.Value = data["Experience"]
		SurvivalTime.Value = data["Survived"]
		Money.Value = data["BackDollars"]
	else
		kills.Value = 0
		Experience.Value = "Newcomer"
		SurvivalTime.Value = 0
		Money.Value = 0
		print("datastore")
	end
end)

local function create_table(player)
	local player_stats = {}
	for i, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function SaveData(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = "Player_"..player.UserId
		playerData:SetAsync(playerUserId, player_stats)
		
	end)
	
	if not success then
		warn("Could not save data")
	end
end
function SaveAllData()
	for _,player in pairs(game.Players:GetPlayers()) do
		SaveData(player)
	end
end
game:BindToClose(function()
	SaveAllData()
end)

while true do
	SaveAllData()
	wait(60)
end
--game.Players.PlayerRemoving:Connect(onPlayerExit)



2 answers

✓ Accepted answer

Hello, if you're testing in the studio test mode, this may not work. So try to publish and then get into the game.

3 comments

0

Will try, thank you

0

Sadly still does not work, I will try to figure something out

0

IT GOT FIXED, idk how but It started working normally in game

hey you! have you ever heard of enes? if you are in trouble, better call enes!

This discussion is preserved as it was. New answers, comments, and votes are closed.