How can I create a datastore for attribute

Its in a normal script in severscriptservice. and yes api services are on!

1 Like

Are you sure it’s not actually saving? Try adding print(Data) after the line where we use GetAsync, see what it prints. Also inside the PlayerRemoving function, try adding a print line after where it checks if PlayerData[plr.Name]

2 Likes

Weird now it sometimes works. I get all the prints including the data. but it sometimes doesn’t save and when that happens it doesn’t print leaving.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore('Data')
local PlayerData = {}
print("Local")
Players.PlayerAdded:Connect(function(plr)
	local Data = DataStore:GetAsync(plr.UserId)
	print(Data)
	if not PlayerData[plr.Name] then
		PlayerData[plr.Name] = {
			Data = {},
			Connections = {},
		}
		print("Playeradded")
	end

	local folder = Instance.new("Folder")
	folder.Name = "Car"
	folder.Parent = plr

	local CarUnknown = Instance.new("StringValue")
	CarUnknown.Name = "CarVals"
	CarUnknown.Parent = plr.Car
	print("New")
	if not Data then
		local DefaultData = {
			['CarName'] = '', 
			['LicensePlate'] = '',
			['Cost'] = "" ,
			-- Add more attributes
		}
		print("Data")
		for i,v in pairs(DefaultData) do
			CarUnknown:SetAttribute(i,v)
			PlayerData[plr.Name].Data[i] = v
			table.insert(PlayerData[plr.Name].Connections,
				CarUnknown:GetAttributeChangedSignal(i):Connect(function()
					PlayerData[plr.Name].Data[i] = CarUnknown:GetAttribute(i)
					print("For")
				end)
			)
		end
	else
		for i,v in pairs(Data) do
			CarUnknown:SetAttribute(i,v)
			PlayerData[plr.Name].Data[i] = v
			table.insert(PlayerData[plr.Name].Connections,
				CarUnknown:GetAttributeChangedSignal(i):Connect(function()
					PlayerData[plr.Name].Data[i] = CarUnknown:GetAttribute(i)
					print("I")
				end)
			)
		end
	end

	table.insert(PlayerData[plr.Name].Connections,
		CarUnknown.AttributeChanged:Connect(function(att)
			PlayerData[plr.Name].Data[att] = CarUnknown:GetAttribute(att)
			print("Table")
		end)
	)
end)

Players.PlayerRemoving:Connect(function(plr)
	if PlayerData[plr.Name] then
		print("if plr")
		DataStore:SetAsync(plr.UserId, PlayerData[plr.Name].Data)
		for i,v in ipairs(PlayerData[plr.Name].Connections) do
			v:Disconnect()
		end
		PlayerData[plr.Name].Connections = {}
		PlayerData[plr.Name] = nil
		print("Leaving")
	end
end)
1 Like

Try running a test in studio, switch to server view, and edit the attributes. Once you’ve done that, leave the test and test again. If the attributes load as you set them, it saves and loads properly. Do this a few times to ensure. I’m pretty confident it’s working as intended.

2 Likes

Hi, there, sorry for the late response.
I tested 3 times in test run and wrote in the value on server side. I had no data to begin with because I was using Player1 but it never saved.
Then I tested in the normal play test I still had the data from last time when it saved once.
I changed it in server view and then left I did this 3 times and it did not change. And still holds the old data when it worked once. The only thing that doesn’t print is leaving.
Btw every time I testing this to see if it works I have been doing the same thing.

1 Like

I just tested on my end and it works as intended. Make sure you click “Current: Client” and turn it to “Current: Server” when editing the attributes to test:

Client
image

Server
image

If you’re editing the attributes in Explorer while it’s still set on client view, they won’t save because what the client does doesn’t replicate to the server.

1 Like

I am doing it in current server I will try creating another place and do it there

1 Like

This is definitely working – do you happen to have another DataStore you named “Data” that is overwriting it? Do you get any errors in the log?

2 Likes

I took a screen recording on a blank place that I just created in the video you can see it only saves if I don’t put anything in the value which it shows at the end of the video.

2 Likes

Hmm… I wonder if you try “Team Test” if it’ll allow you to properly save the data in studio, otherwise, I would say to add the following lines when the player joins, publish to the actual game, join it and leave, then remove these lines and see if it loads in studio:

CarUnknown:SetAttribute('Car','Honda')
CarUnknown:SetAttribute('Price',5000)

The issue definitely appears to be something with your studio disconnecting before fully executing the PlayerRemoving lines.

3 Likes

Thank you so much for all your help!!
I found that if I do the current server and change the values that way in the studio it doesn’t save, but if I do it in a script it works!! I also tried it on a public server and it works!!

2 Likes

No worries! :slight_smile: Glad I was able to help.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.