So im new to module scripts and i made a script that works and it has values but i want to save the values and idk how here is the script i made
Saving Script:
local DataStoreService = game:GetService("DataStoreService") --Gets the DataStoreService
local Rs = game:GetService("ReplicatedStorage")
local dataStore = DataStoreService:GetDataStore("Test1") --Gives the Datastore a name
local UpgradesModule = require(Rs:WaitForChild("Modules"):WaitForChild("ModuleScript"))
--//Function
local function saveData(player)
local tableToSave = {
UpgradesModule.Variables.Speed;
}
local success, errorMessage = pcall(dataStore.SetAsync, dataStore, player.UserId, tableToSave)
if success then
print("Data2 has been saved!")
else
print("Data2 has not been saved!")
end
end
game.Players.PlayerAdded:Connect(function(player)
local data = nil
local success, errorMessage = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
UpgradesModule.Variables.Speed = data[1]
else
print("The Player has no Data!")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
task.spawn(saveData, player)
end
end)