Script not updating player's data

Hey all! I’m using the ProfileService module to save my player data. However, for whatever reason my script isn’t properly updating the player’s data.

GiveExp Script
local GiveExp = {}

local datastore = require(game.ServerScriptService.Modules.Data.DataGetter)

function GiveExp:giveExp(player, amount, source)
	
	local playerData = datastore:Get(player)
	local playerExp = playerData.Experience
	local playerLevel = playerData.Level
	local requiredExp = 20 + ((playerLevel * 2) ^ 1.9)
	
	playerExp = playerExp + amount
	
	if playerExp >= requiredExp then
		playerExp = 0
		playerLevel = playerLevel + 1
	end
	
end

return GiveExp
DataGetter
local ProfileService = require(script.Parent.ProfileService)
local ReplicaService = require(script.Parent.Parent.ReplicaService)

local ProfileStore = ProfileService.GetProfileStore(
	"Player",
	{
		--// CURRENCY
		Trimmings = 0;
		Shards = 0;
		
		
		--// STATS
		GrassCut = 0;
		Level = 0;
		Experience = 0;
		BossKills = 0;
		
		--// MISCELLANEOUS
		UpgradesOwned = {};
		Health = 100;
		Sprint = 5;
		
		}
	}
)

local Profiles = {}

game.Players.PlayerRemoving:Connect(function(whoLeft)
	if Profiles[whoLeft] then
		Profiles[whoLeft]:Release()
	end
end)

game.Players.PlayerAdded:Connect(function(whoJoined)
	local profile = ProfileStore:LoadProfileAsync("Player_"..whoJoined.UserId, "ForceLoad")

	if profile then
		
		--//Replicates data to client
		game.ServerStorage.Events.RequestSaveData.Event:Connect(function(player)
			if player == whoJoined then
				local StatsMenuReplica = ReplicaService.NewReplica({
					ClassToken = ReplicaService.NewClassToken("StatsMenuReplica_"..whoJoined.UserId),
					Data = {Trimmings = profile.Data.Trimmings, Level = profile.Data.Level, Experience = profile.Data.Experience, BossKills = profile.Data.BossKills, GrassCut = profile.Data.GrassCut, Shards = profile.Data.Shards, BestMower = profile.Data.BestMower, MowerImage = game.ServerStorage.Mowers[profile.Data.BestMower]:GetAttribute("Image")},
					Replication = whoJoined
				})
			end
		end)
		
		--//
		
		profile:ListenToRelease(function()
			Profiles[whoJoined] = nil
			whoJoined:Kick("Data was released.")
		end)
		
		if whoJoined:IsDescendantOf(game.Players) then
			Profiles[whoJoined] = profile
		else
			profile:Release()
		end
	else
		whoJoined:Kick("Error loading data.")
	end
end)

local DataGetter = {}

function DataGetter:Get(player)
	local profile = Profiles[player]
	
	if profile then
		return profile.Data
	end
end
	
return DataGetter

I have another script in the game which updates the player’s data, but that one is a string. I’m not sure why it’s not working with numbers, since I’m not very good with data stuff. Any help is appreciated, thanks!

If you mean the value isn’t saved after you update it, I believe it’s because you are updating the variable named playerLevel and playerExp instead of the player data. You would need to address the player’s data directly when updating it.

Personally, I would do something like this:

local cachedProfiles = -- some path

function giveExp(player)
   local profile = cachedProfiles[player]

   if profile then
      local playerLevel = profile.Data.Level
      local playerExp = profile.Data.Experience
      local requiredExp = 5
      
      if playerExp > requiredExp then
          profile.Data.Experience = 0
          profile.Data.Level += 1
     end
   end
end
1 Like

If you’re unsure on how to use ProfileService properly, you can view some tutorials! I have linked one below that really helped me understand how to use it effectively when I first discovered it.

Hope this helps!

ProfileService Basic Usage Tutorial

Yep, thanks this works. Crazy how something so simple can break a whole script.

1 Like