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

Wondering why script is not saving two data values?

I've been working on a basic simulator game, and was wondering what was wrong with my script, also i'm trying to make it save multiple data values:

Heres the first script:

local ds = game:GetService("DataStoreService")
local statsStore = ds:GetGlobalDataStore("PlayerStats")

function getPlayerData(plr)
	local success, data = false, nil
	
	repeat
		success = pcall(function()
			data = statsStore:GetAsync("Data"..plr.UserId)
		end)
		wait(2)
	until success
	
	plr.DataLoaded.Value = true
	return data
end

function savePlayerData(plr)
	if plr.DataLoaded.Value then
		statsStore:SetAsync("Data"..plr.UserId, plr.leaderstats.Lifts.Value, plr.Forky.Value)
		statsStore:SetAsync("Data"..plr.UserId, plr.Forky.Value)
	end
end

function givestats(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	local lifts = Instance.new("IntValue", leaderstats)
	lifts.Name = "Lifts"
	local loaded = Instance.new("BoolValue", plr)
	loaded.Name = "DataLoaded"
	local Fork = Instance.new("IntValue", plr)
	Fork.Name = "Forky"
	local data = getPlayerData(plr)
	if data == nil then
		data = 0
		lifts.Value = data
		Fork.Value = 1
	end
end

game.Players.PlayerAdded:Connect(givestats)
game.Players.PlayerRemoving:Connect(savePlayerData)

while wait(60) do
	for _, plr in pairs (game.Players:GetChildren()) do
		savePlayerData(plr)
 	end
end

Heres the other script:

local LiftingEvent = Instance.new("RemoteEvent", game.ReplicatedStorage)
LiftingEvent.Name = "Lift"

local LastLifted = {}

function PlayerLifted(plr)
	if LastLifted[plr.UserId] == nil then
		LastLifted[plr.UserId] = os.time()
	end
	if os.time() - LastLifted[plr.UserId] >= 1 then
		plr.leaderstats.Lifts.Value = plr.leaderstats.Lifts.Value + plr.Forky.Value * 3
		LastLifted[plr.UserId] = os.time()
	end
end

LiftingEvent.OnServerEvent:Connect(PlayerLifted)

1 answer

✓ Accepted answer

Well, you are overwriting the data. First, the lifts. Then, you rewrite the database lifts value to Forky value.

You need to make 2 separate databases. It is easy, make a ds2 with another name and use that to save the Forky values.

1 comment

0

Ah okay, thanks.

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