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

(Solved)Why do not save the values of level and experience in the DataStoreService?

Hello, I have several script 2 in ServerServiceScript and one in the player all are server script, where the player's position is saved, that works perfectly, then I have the level and experience that work if the player dies or is reset, but when I leave the game either in stop or in the menu button leave, the experience and level data are erased, from the same roblox application, but if the position is saved perfectly. What can be?

--script Menu in ServerServiceScript

local Players = game:GetService("Players")
local MenuEventSave = game.ReplicatedStorage:WaitForChild('MenuEventSave')
local MenuEventReset = game.ReplicatedStorage:WaitForChild('MenuEventReset')
local DataStoreService = game:GetService("DataStoreService")
local DataPosition = DataStoreService:GetDataStore("PlayerPosition")
local DataLevel = DataStoreService:GetDataStore("PlayerLevel")
local DataExp = DataStoreService:GetDataStore("PlayerExp")
local lastPosition
local tableToVector = {43.809, 112.453, 103.157}
MenuEventSave.OnServerEvent:Connect(function()
	for i, playerChild in pairs(Players:GetChildren()) do
		for i, player in pairs(Players:GetPlayers()) do
			lastPosition = player.Character.HumanoidRootPart.Position
			if lastPosition then
				DataPosition:SetAsync("Player_" .. player.UserId, {lastPosition.X,lastPosition.Y,lastPosition.Z})
				DataLevel:SetAsync("Player_" .. player.UserId, player.leaderstats.Level.Value)
				DataExp:SetAsync("Player_" .. player.UserId, player.leaderstats.EXP.Value)
				print("Set1")
			end
		end
	end
end)
MenuEventReset.OnServerEvent:Connect(function()
	for i, playerChild in pairs(Players:GetChildren()) do
		for i, player in pairs(Players:GetPlayers()) do
			lastPosition = player.Character.HumanoidRootPart.Position
			if lastPosition then
				DataPosition:SetAsync("Player_" .. player.UserId, tableToVector)
				DataLevel:SetAsync("Player_" .. player.UserId, 0)
				DataExp:SetAsync("Player_" .. player.UserId, 0)
				print("Set2")
			end
		end
	end
end)
-- script load and died save ServerServiceScript

local DataStoreService = game:GetService("DataStoreService")
local DataPosition = DataStoreService:GetDataStore("PlayerPosition")
local DataLevel = DataStoreService:GetDataStore("PlayerLevel")
local DataExp = DataStoreService:GetDataStore("PlayerExp")
local players = game:GetService("Players")
local lastPosition
local debounce = false
players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder", player)
	stats.Name = "leaderstats"
	local level = Instance.new("IntValue", stats)
	level.Name = "Level"
	level.Value = 0
	local exp = Instance.new("IntValue", stats)
	exp.Name = "EXP"
	exp.Value = 0
	local position = DataPosition:GetAsync("Player_" .. player.UserId)
    
    if position then
	   player.Character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(position[1],position[2],position[3])
	end
	
	DataLevel:GetAsync("Player_" .. player.UserId)
    DataExp:GetAsync("Player_" .. player.UserId)
	
	player.CharacterRemoving:Connect(function(character)
		lastPosition = character.HumanoidRootPart.Position
		if lastPosition then
		   	DataPosition:SetAsync("Player_" .. player.UserId, {lastPosition.X,lastPosition.Y,lastPosition.Z})
		    print("Died")
		end
		
		local success, err = pcall(function()
			DataLevel:SetAsync("Player_" .. player.UserId, player.leaderstats.Level.Value)
			DataExp:SetAsync("Player_" .. player.UserId, player.leaderstats.EXP.Value)
		end)
	 
		if success then
			print("Success!")
		end
		
		position = DataPosition:GetAsync("Player_" .. player.UserId)
		
		if position then
	    	player.Character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(position[1],position[2],position[3])
		end
		DataLevel:GetAsync("Player_" .. player.UserId)
    	DataExp:GetAsync("Player_" .. player.UserId)
	end)
end)

-- Script player level and experience ++

local Players = game:GetService("Players")
for i, player in pairs(Players:GetPlayers()) do
	for i, enemy in pairs(game.Workspace:GetChildren()) do
		while wait(0.01) do
			if enemy:IsA('Model') and enemy.Name == "Enemy" and not Players:GetPlayerFromCharacter(enemy) then 
				local humanoidEnemy = enemy:FindFirstChild("Humanoid")
				humanoidEnemy.Died:Connect(function()
					player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
					player.leaderstats.EXP.Value = player.leaderstats.EXP.Value + 5
					print("Kill")
				end)
			end
		break
		end
	end
end

5 comments

0

Even save the game from the save button does not save me when leaving nothing

0

You are using IntValues to store Level and EXP in contrast to Position where a tableToVector is used (if I'm not mistaken). Use the .Changed(function(updVal)) this method stores the "Updated" values for your EXP and Level on kills. It will also fix your DataStore issues, for DataLevel and DataExp as it was storing base values instead of updated ones.

0

Thamk you xxcoordinatorxx

0

But with what variable I use the function .Changed (function (updVal))

0

It goes at the bottom where you're updating leaderstats on kills.

2 answers

✓ Accepted answer

I have to make an answer for this,

player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1 -- SetAsync here
player.leaderstats.Level.Changed(function(updVal) player.leaderstats.Level.Value = updVal end) -- IncrementAsync here

player.leaderstats.EXP.Value = player.leaderstats.EXP.Value + 5 -- SetAsync
player.leaderstats.EXP.Changed(function(updVal) player.leaderstats.EXP.Value = updVal end) -- IncrementAsync

The issue could be with the DataStores and how you're accessing, storing, and retrieving data. Which I'll get into after I have done understanding your script.

5 comments

0

Thanks you xxcoordinatorxx

0

I was going to rewrite your entire script and I was 1/4 of the way. As long as I helped you, it's all that matters.

0

I have given it to the player what you have given me the last script that I have put but it continues the same, apparently it was missing Connect after Changed but it does not work. Am I doing it wrong?

0

I will rewrite your script tomorrow. You'll have to be patient until then.

0

You should explain why it doesn't work rather than "oh yeah uhhh script bad copy paste this"

local levelData
		local expData
		local success, err = pcall(function()
		levelData = DataLevel:IncrementAsync("Player_" .. player.UserId, 1)
		expData = DataLevel:IncrementAsync("Player_" .. player.UserId, 1)
		end)
 
		if success and levelData == 5 and expData == 5 then
		   	player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
			player.leaderstats.Level.Changed:Connect(function(updVal)
				player.leaderstats.Level.Value = updVal 
			end)
			player.leaderstats.EXP.Value = player.leaderstats.EXP.Value + 5
				player.leaderstats.EXP.Changed:Connect(function(updVal) 
				player.leaderstats.EXP.Value = updVal 
			end)
		end

3 comments

0

The way i was going to go by was Using an IncrementAsync to store values to the Datastore as soon as they are changed.

0

I am going to try

0

I've done this and it also does not work when I leave the game it's deleted

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