I don't really know how to script data storage, so I watched a video about it and came up with this. The script is almost identical to the one in the video excepting the stringvalues. Everyone commenting the video claim it works but for me it doesn't save. I have API enabled in the game I'm scripting it. Is it because of the stringvalues?

local Data = game:GetService("DataStoreService"):GetDataStore("Loadout_Saving")

game.Players.PlayerAdded:Connect(function(plr)
	local Folder = Instance.new("Folder",game.ServerStorage:FindFirstChild("CurrentLoadout"))
	Folder.Name = plr.Name
	local Primary = Instance.new("StringValue",Folder)
	Primary.Name = "Primary"
	local Secondary = Instance.new("StringValue",Folder)
	Secondary.Name = "Secondary"
	local SavedItems = Data:GetAsync(plr.userId)
	if SavedItems then
		Primary.Value = SavedItems.Primary or "None"
		print(Primary.Value)
		Secondary.Value = SavedItems.Secondary or "None"
		print(Secondary.Value)
	else
		Primary.Value = "None"
		Secondary.Value = "None"
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local Saving = {["Primary"] = game.ServerStorage.CurrentLoadout:FindFirstChild(plr.Name).Primary.Value;
		  			["Secondary"] = game.ServerStorage.CurrentLoadout:FindFirstChild(plr.Name).Secondary.Value
	}
	Data:SetAsync(plr.userID,Saving)
	
end)

10 comments

0

Did you test it in an online server? Data doesn't save in studio

0

The 2nd arguement of Instance.new is deprecated.

0

^it isnt really deprecated just advised against due to performance issues

0

since when was instance.new deprecated?

0

he meant setting the parent through the 2nd parameter (Instance.new(instance name, parent))

0

Yes, I tested it in an online server and it didn't work. Checked while online with a print command whilde in developer console.

0

for some reason player removing doesn't work for me. Try putting the set async in a loop

0

Data do save in studio.

0

I didn't try in studio, I'll try the set async in a loop when I can and update.

0

@radusavin it IS deprecated. Look it up yourself.