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

how to fix this datastore issue with a remote event?

So i made a datastore but it does not save only when i change it via console So i tought a remote event would be the problem solver BUT it does not work! My data does not save i got no output errors or warnings Please help

Remote Event Fire It does Print!

local Remote = game.ReplicatedStorage.RemoteEvent

Remote.OnServerEvent:Connect(function(player)
	player:FindFirstChild("Backpack").Value.Hair.Value = player.leaderstats.Hair.Value
	print("leaderstats changed")
end)

My datastore

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("StoredData")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local hair = Instance.new("IntValue")
	hair.Name = "Hair" 
	hair.Parent = leaderstats
	
	local haircolor = Instance.new("IntValue")
	haircolor.Name = "HairColor" 
	haircolor.Parent = leaderstats
	
	local Skin = Instance.new("IntValue")
	Skin.Name = "SkinColor" 
	Skin.Parent = leaderstats
	
	local Pants = Instance.new("IntValue")
	Pants.Name = "Pants"
	Pants.Parent = leaderstats
	
	local Shirt = Instance.new("IntValue")
	Shirt.Name = "Shirt"
	Shirt.Parent = leaderstats
	
	local HairData
	local HairColorData
	local SkinData
	local PantsData
	local ShirtData
	local success , errormessage = pcall(function()
		HairData = myDataStore:GetAsync(player.UserId.."-Hair")
		HairColorData = myDataStore:GetAsync(player.UserId.."-HairColor")
		SkinData = myDataStore:GetAsync(player.UserId.."-SkinColor")
		PantsData = myDataStore:GetAsync(player.UserId.."-Pants")
		ShirtData = myDataStore:GetAsync(player.UserId.."-Shirt")
	end)
	if success then
		hair.Value = HairData
		haircolor.Value = HairColorData
		Skin.Value = SkinData
		Pants.Value = PantsData
		Shirt.Value = ShirtData
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success ,errormessage = pcall(function()
	myDataStore:SetAsync(player.UserId.."-Hair",player.leaderstats.Hair.Value)
	myDataStore:SetAsync(player.UserId.."-HairColor",player.leaderstats.HairColor.Value)
	myDataStore:SetAsync(player.UserId.."-SkinColor",player.leaderstats.SkinColor.Value)
	myDataStore:SetAsync(player.UserId.."-Pants",player.leaderstats.Pants.Value)
	myDataStore:SetAsync(player.UserId.."-Shirt",player.leaderstats.Shirt.Value)
	end)
	if success then 
		print("Data Saved")
	else
		warn(errormessage)
	end
end)

3 comments

0

Does it print “Data Saved”?

0

No it does not

0

I'm going to write an answer but it's going to take 30 minutes, wait a minute...

1 answer

✓ Accepted answer

Problem: Okay, so the problem might be that your datastore doesn't handle it because you're saving too much values just in one datastore. Fix : Try making all the values in a table and saving that. How to do that? : Use JSONEncode and JSONDecode to encode tables and save them and then getting them from datastore and decoding it to normal table and changing the values from the table to the actual values.

local dss = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("testdatastore")
local httpservice = game:GetService("HttpService")

game.Players.PlayerAdded:Connect(function(player)
    leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    hair = Instance.new("IntValue",leaderstats)
    hair.Name = "Hair" 

    haircolor = Instance.new("IntValue",leaderstats)
    haircolor.Name = "HairColor" 

    Skin = Instance.new("IntValue",leaderstats)
    Skin.Name = "SkinColor" 

    Pants = Instance.new("IntValue",leaderstats)
    Pants.Name = "Pants"

    Shirt = Instance.new("IntValue",leaderstats)
    Shirt.Name = "Shirt"
    local key = player.Name.."-"..player.UserId
pcall(function()
    encodedData = datastore:GetAsync(key)
end)
    local data = httpservice:JSONDecode(encodedData)
-- Set the values from the decoding
	hair.Value = data[1]
	haircolor.Value = data[2]
	Skin.Value = data[3]
	Pants.Value = data[4]
	Shirt.Value = data[5]
 	print("All the values were loaded")
end)

game.Players.PlayerRemoving:Connect(function(player)
    local key = player.Name.."-"..player.UserId
    local datatosave = {
	hair.Value,
	haircolor.Value,
	Skin.Value,
	Pants.Value,
	Shirt.Value
}
   local encodeData = HttpService:JSONEncode(data)
   DataStore:SetAsync(key,encodeData)
end)

I hope this fixed your problem!

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