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

Why moduled Player Data does not saves?

Hello, so i tried to make a player data be inside of a module, I can change it trough console, and it updates, but if close the game, its saves the default data, not new, and I don't know what is the problem.

Here's the code

Module:

local Data = {}
local Remotes = game.ReplicatedStorage:WaitForChild("Remotes")


Data.PlayerData = {}

function Data.new(Player)
	local PData  = {}

	PData.Food = 10
	PData.Fertilizer = 5

	PData.Tool = "Shovel"
	PData.Tools = {}
	
	--table.insert(PlayerData[Player.UserId], PData)
	Data.PlayerData[Player.UserId] = PData

	return PData
end

function Data:Get(Player)
	if game:GetService("RunService"):IsClient() then
		print("client")
		return Remotes.GetPlayerData:InvokeServer(Player)
	else
		return Data.PlayerData[Player.UserId]
	end
end

function Data:SetKey(Player, Key, Value)
	Data.PlayerData[Player.UserId][Key] = Value
end

function Data:Set(Player, PData)
	print(PData, "data to update")
	Data.PlayerData[Player.UserId] = PData
	print(Data.PlayerData[Player.UserId], "pdata after update")
end

return Data

Server Script:

local PlayerData = require(game.ReplicatedStorage:WaitForChild("Data"))
local Remotes = game.ReplicatedStorage:WaitForChild("Remotes")

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("WormfeederDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local Food = Instance.new("IntValue")
	Food.Name = "Food"
	Food.Parent = leaderstats
	
	local Fertilizer = Instance.new("IntValue")
	Fertilizer.Name = "Fertilizer"
	Fertilizer.Parent = leaderstats
	
	local Data
	
	local succes, errormsg = pcall(function()
		Data = DataStore:GetAsync(Player.UserId)
	end)
	
	
	if succes then
		print(Data, "is a player data")
		if Data then
			PlayerData.new(Player)
			PlayerData:Set(Player, Data)
			local PData = PlayerData:Get(Player)
			
			Food.Value = PData.Food
			Fertilizer.Value = PData.Fertilizer
		else
			print("New Player", Player.Name)
			PlayerData.new(Player)
			local PData = PlayerData:Get(Player)
			
			Food.Value = PData.Food
			Fertilizer.Value = PData.Fertilizer
		end
	else
		warn("An error occured while loading data:", errormsg)
		Player:Kick("An error occured while loading data! Please join later")
	end
	
	game:BindToClose(function()
		print(Player.Name, "is leaving!")
		
		local Succes, Errormsg = pcall(function()
			local SaveData = PlayerData:Get(Player)
			print("sd", SaveData)
			
			DataStore:SetAsync(Player.UserId, SaveData)
		end)
		
		if Succes then
			print("Data successfully saved")
		else
			warn("An error occured while saving data:", Errormsg)
		end
	end)
end)

Remotes.GetPlayerData.OnServerInvoke = function(player)
	return PlayerData:Get(player)
end

If someone can, please help since im testing for it for hours, and i don't know what's wrong. Thanks!

1 comment

0

dont use moudle script, its one-time

1 answer

I suggest just saving all code in ServerScriptService. Don't make a Module script. Make sure HTTP Requests are on. Make sure you set async before you get async. IF this doesn't work I suggest looking at this video:

https://www.youtube.com/watch?v=DkYupSBUpes

2 comments

0

but, the points is, that im also need to save a table of items, for example, and the module data is easier to use

0

So, is there no way to make it? I sawm people, which are have player data in module, so its probably possible

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