What do you want to achieve? i want the game to save the number of studs walked
What is the issue? it isn’t my area of expertise
What solutions have you tried so far? everything, youtube, dev forums
local OldPosition = {}
local TotalStudsWalked = {}
game.Players.PlayerAdded:Connect(function(Player)
local Board = Instance.new("Folder")
Board.Name = "leaderstats"
Board.Parent = Player
local Traveled = Instance.new("IntValue", Board)
Traveled.Name = "Studs"
end)
game.Players.PlayerRemoving:Connect(function(Player)
end)
while wait do
for i,Player in pairs(game.Players:GetPlayers()) do
if Player.Character then
local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
if Humanoid.MoveDirection.Magnitude > 0 then
if OldPosition[Player.UserId] then
local newDistanceTravelled = (Humanoid.RootPart.Position - OldPosition[Player.UserId]).Magnitude
TotalStudsWalked[Player.UserId] = TotalStudsWalked[Player.UserId] + newDistanceTravelled
Player:WaitForChild("leaderstats").Studs.Value = Player.leaderstats.Studs.Value + (newDistanceTravelled / 1)
OldPosition[Player.UserId] = Humanoid.RootPart.Position
else
TotalStudsWalked[Player.UserId] = 0
OldPosition[Player.UserId] = Humanoid.RootPart.Position
end
end
end
end
wait(0.1)
end
Many people make the mistake by using too many datastores at once. Its better when creating one whole table for everything:
--Vars and Services
local DataStore = game:GetService("DataStoreService"):GetDataStore("MainDatastore")
--Functions
function formatData(player)
local data = {}
local YourFolder = player:WaitForChild("leaderstats")
local steps = {}
for i,piece in pairs(YourFolder:GetChildren()) do
table.insert(steps, piece.Value)
end
table.insert(data, steps)
return data
end
function saveData(player)
local Key = "Player_"..player.UserId
local stuffToSave = formatData(player)
local succes, err = pcall(function()
if stuffToSave then
DataStore:SetAsync(Key, stuffToSave)
end
end)
if err then
warn('WARNING: COULD NOT SAVE PLAYER DATA: '..err)
end
end
function loadData(player)
--CREATING FOLDERS
local Key = "Player_"..player.UserId
local Board = Instance.new("Folder")
Board.Name = "leaderstats"
Board.Parent = player
local Traveled = Instance.new("IntValue", Board)
Traveled.Name = "Studs"
------------------------------------------------------------
--GETTING PLAYER DATA
local sucess, err = pcall(function()
local data = DataStore:GetAsync(Key)
if data then
if data[1] then
Traveled.Value = data[1][1] -- The first section of the folder, first value: FreeSkips
else
Traveled.Value = 0
end
else -- if data doesn't exist
Traveled.Value = 0
end
end)
if err then
warn('WARNING: COULD NOT LOAD PLAYER DATA: '..err)
end
end
function onServerShutdown()
if game:GetService("RunService"):IsStudio() then
wait(2)
else
for _, player in pairs(game.Players:GetPlayers()) do
local finished = Instance.new("BindableEvent")
saveData(player) --It's easier to use the previous saveData function on each player than it is to write new code
finished.Event:Wait()
end
end
end
--Binding Events
game:BindToClose(onServerShutdown)
game.Players.PlayerAdded:Connect(loadData)
game.Players.PlayerRemoving:Connect(saveData)
--
local OldPosition = {}
local TotalStudsWalked = {}
game.Players.PlayerAdded:Connect(function(Player)
local Board = Player:WaitForChild("leaderstats")
local Traveled = Board:FindFirstChild("Studs")
end)
while wait(0.1) do
for i,Player in pairs(game.Players:GetPlayers()) do
if Player.Character then
local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
if Humanoid.MoveDirection.Magnitude > 0 then
if OldPosition[Player.UserId] then
local newDistanceTravelled = (Humanoid.RootPart.Position - OldPosition[Player.UserId]).Magnitude
TotalStudsWalked[Player.UserId] = TotalStudsWalked[Player.UserId] + newDistanceTravelled
Player:WaitForChild("leaderstats").Studs.Value = Player.leaderstats.Studs.Value + (newDistanceTravelled / 1)
OldPosition[Player.UserId] = Humanoid.RootPart.Position
else
TotalStudsWalked[Player.UserId] = 0
OldPosition[Player.UserId] = Humanoid.RootPart.Position
end
end
end
end
end
finished + fixed your script, everything should work as far as you progress and everything wil save. the function you made is at the end of the script so if it wasn’t finished that just change it
Hope this helps (took me some time to make)
local Game = game
local Players = Game:GetService("Players")
local RunService = Game:GetService("RunService")
local Data = {}
local function OnPlayerAdded(Player)
Data[Player] = Vector3.new(0, 0, 0)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
local Studs = Instance.new("NumberValue")
Studs.Name = "Studs"
Studs.Parent = Leaderstats
Leaderstats.Parent = Player
end
local function OnPlayerRemoving(Player)
Data[Player] = nil
end
local function OnHeartbeat()
for _, Player in ipairs(Players:GetPlayers()) do
local Character = Player.Character
if not Character then continue end
local Position = Character:GetPivot().Position
local Distance = (Data[Player] - Position).Magnitude
if Distance == 0 then continue end
Data[Player] = Position
local Leaderstats = Player:FindFirstChild("leaderstats")
if not Leaderstats then continue end
local Studs = Leaderstats:FindFirstChild("Studs")
if not Studs then continue end
Studs.Value += Distance
end
end
Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
RunService.Heartbeat:Connect(OnHeartbeat)