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

Datastore Player had data twice and not saving?

ok so i have 2 separate datastore scripts in sss and the maxhealth datastore is printing player had data twice and the speed prints gave player data twice while i have 3 other datastores that are the same script as these just edited but they print and function just fine while maxhealth is not saving and speed is

maxhealth script

local Data = game:GetService("DataStoreService"):GetDataStore("MaxHealth")
game.Players.PlayerAdded:Connect(function(plr)
	local MaxHealth = Instance.new("Folder")
	MaxHealth.Name = "MaxHealth"
	MaxHealth.Parent = plr
	local First = Instance.new("BoolValue")
	First.Name = "First"
	First.Parent = MaxHealth
	local Second = Instance.new("BoolValue")
	Second.Name = "Second"
	Second.Parent = MaxHealth
	local Third = Instance.new("BoolValue")
	Third.Name = "Third"
	Third.Parent = MaxHealth
	local Fourth = Instance.new("BoolValue")
	Fourth.Name = "Fourth"
	Fourth.Parent = MaxHealth
	local Fifth = Instance.new("BoolValue")
	Fifth.Name = "Fifth"
	Fifth.Parent = MaxHealth
	
	print("Gave "..plr.Name.." MaxHealth Data")
	local SavedItems = Data:GetAsync(plr.UserId)
	if SavedItems then
		First.Value = SavedItems.First or false
		Second.Value = SavedItems.Second or false
		Third.Value = SavedItems.Third or false
		Fourth.Value = SavedItems.Fourth or false
		Fifth.Value = SavedItems.Fifth or false
		print(plr.Name.." had MaxHealth data")
	else
		First.Value = false
		Second.Value = false
		Third.Value = false
		Fourth.Value = false
		Fifth.Value = false
		print(plr.Name.." didn't have MaxHealth data")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local Saving = {
		["First"] = (plr.MaxHealth).First.Value;
		["Second"] = (plr.MaxHealth).Second.Value;
		["Third"] = (plr.MaxHealth).Third.Value;
		["Fourth"] = (plr.MaxHealth).Fourth.Value;
		["Fifth"] = (plr.MaxHealth).Fourth.Value;}
	Data:SetAsync(plr.UserId, Saving)
	print("Saved "..plr.Name.."'s MaxHealth Data")
end)

speed script

local Data = game:GetService("DataStoreService"):GetDataStore("Speed")
game.Players.PlayerAdded:Connect(function(plr)
	local Speed = Instance.new("Folder")
	Speed.Name = "Speed"
	Speed.Parent = plr
	local Five = Instance.new("BoolValue")
	Five.Name = "Five"
	Five.Parent = Speed
	local Ten = Instance.new("BoolValue")
	Ten.Name = "Ten"
	Ten.Parent = Speed
	local Fifteen = Instance.new("BoolValue")
	Fifteen.Name = "Fifteen"
	Fifteen.Parent = Speed
	local Twenty = Instance.new("BoolValue")
	Twenty.Name = "Twenty"
	Twenty.Parent = Speed
	
	print("Gave "..plr.Name.." Speed Data")
	local SavedItems = Data:GetAsync(plr.UserId)
	if SavedItems then
		Five.Value = SavedItems.Five or false
		Ten.Value = SavedItems.Ten or false
		Fifteen.Value = SavedItems.Fifteen or false
		Twenty.Value = SavedItems.Twenty or false
		print(plr.Name.." had Speed data")
	else
		Five.Value = false
		Ten.Value = false
		Fifteen.Value = false
		Twenty.Value = false
		print(plr.Name.." didn't have Speed data")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local Saving = {
		["Five"] = (plr.Speed).Five.Value;
		["Ten"] = (plr.Speed).Ten.Value;
		["Fifteen"] = (plr.Speed).Fifteen.Value;
		["Twenty"] = (plr.Speed).Twenty.Value;}
	Data:SetAsync(plr.UserId, Saving)
	print("Saved "..plr.Name.."'s Speed Data")
end)

1 answer

✓ Accepted answer

I suggest using DataStore2 as used in the DevForums. https://devforum.roblox.com/t/how-to-use-datastore2-data-store-caching-and-data-loss-prevention/136317

This module prevents data loss and manages DataStore for you. I have converted your script to use this.

Also, you should combine these scripts into one DataSaver. As they change, I have DataStore2 handle the values.

Here is your code that works and uses DataStore2 which I recommend. Your issue was you were saving at the same time and would have run into problems later on.

I have also provided lua comments about what I did to change your code. Reminder, you should put this in 1 script, not 2.

EDIT: Here is a tutorial on this also. https://www.youtube.com/watch?v=hBfMfB0BwGA

local DS2 = require(1936396537) --DataStore2 from the devforum. https://devforum.roblox.com/t/how-to-use-datastore2-data-store-caching-and-data-loss-prevention/136317

game.Players.PlayerAdded:Connect(function(plr)
	local speedData = DS2('Speed', plr)--Get the speed datastore
	local healthData = DS2('Health', plr)--Get the health datastore
	--Speed
    local Speed = Instance.new("Folder")
    Speed.Name = "Speed"
    Speed.Parent = plr
    local Five = Instance.new("BoolValue")
    Five.Name = "Five"
    Five.Parent = Speed
    local Ten = Instance.new("BoolValue")
    Ten.Name = "Ten"
    Ten.Parent = Speed
    local Fifteen = Instance.new("BoolValue")
    Fifteen.Name = "Fifteen"
    Fifteen.Parent = Speed
    local Twenty = Instance.new("BoolValue")
    Twenty.Name = "Twenty"
    Twenty.Parent = Speed
	for i,v in pairs (Speed:GetChildren()) do
		v.Changed:connect(function() --When the value is updated
			local Saving = {
        		["Five"] = (plr.Speed).Five.Value;
        		["Ten"] = (plr.Speed).Ten.Value;
        		["Fifteen"] = (plr.Speed).Fifteen.Value;
        		["Twenty"] = (plr.Speed).Twenty.Value;
			}
			speedData:Set(Saving)--Save in speed data with the saving table
			print'Saved speedData.'
		end)
	end
    print("Gave "..plr.Name.." Speed Data")
    local SavedItems = speedData:Get(nil)--Get with a default value of nil.
    if SavedItems then
        Five.Value = SavedItems.Five or false
        Ten.Value = SavedItems.Ten or false
        Fifteen.Value = SavedItems.Fifteen or false
        Twenty.Value = SavedItems.Twenty or false
        print(plr.Name.." had Speed data")
    else
        Five.Value = false
        Ten.Value = false
        Fifteen.Value = false
        Twenty.Value = false
        print(plr.Name.." didn't have Speed data")
    end
	--MaxHealth
	local MaxHealth = Instance.new("Folder")
    MaxHealth.Name = "MaxHealth"
    MaxHealth.Parent = plr
    local First = Instance.new("BoolValue")
    First.Name = "First"
    First.Parent = MaxHealth
    local Second = Instance.new("BoolValue")
    Second.Name = "Second"
    Second.Parent = MaxHealth
    local Third = Instance.new("BoolValue")
    Third.Name = "Third"
    Third.Parent = MaxHealth
    local Fourth = Instance.new("BoolValue")
    Fourth.Name = "Fourth"
    Fourth.Parent = MaxHealth
    local Fifth = Instance.new("BoolValue")
    Fifth.Name = "Fifth"
    Fifth.Parent = MaxHealth
	for i,v in pairs (MaxHealth:GetChildren()) do
		v.Changed:connect(function() --When the value is updated
			local Saving = {
        		["First"] = (plr.MaxHealth).First.Value;
        		["Second"] = (plr.MaxHealth).Second.Value;
        		["Third"] = (plr.MaxHealth).Third.Value;
        		["Fourth"] = (plr.MaxHealth).Fourth.Value;
        		["Fifth"] = (plr.MaxHealth).Fourth.Value;
			}
			healthData:Set(Saving)--Save in health data with the saving table
			print'Saved healthData.'
		end)
	end
    print("Gave "..plr.Name.." MaxHealth Data")
    SavedItems = healthData:Get(nil)--Get with a default value of nil.
    if SavedItems then
        First.Value = SavedItems.First or false
        Second.Value = SavedItems.Second or false
        Third.Value = SavedItems.Third or false
        Fourth.Value = SavedItems.Fourth or false
        Fifth.Value = SavedItems.Fifth or false
        print(plr.Name.." had MaxHealth data")
    else
        First.Value = false
        Second.Value = false
        Third.Value = false
        Fourth.Value = false
        Fifth.Value = false
        print(plr.Name.." didn't have MaxHealth data")
    end
end)

6 comments

0

oh alright thank you very much i tried implementing datastore 2 into my game before from alvinblox's video but it didnt work

0

but while having it all in one script if i wipe a certain set from the datastore2 like maxhealth because say of some exploiter problem how would i go about that?

0

You should secure your code for it not to be exploitable.

0

alright and one more thing the maxhealth is still not saving even with datastore2 but the speed is

0

the way i have it set up is when a player clicks a textbutton it fires a remote event and then the server takes away a set value from their experience data and sets their value to true on the maxhealth

0

That means the value is not changing if it doesn't save.

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