I want to know how I could shorten this code just so it’s easier to read
local CountName = "Deaths"
local DataStore = game:GetService("DataStoreService") -- DataStore service
local MyDataStore = DataStore:GetDataStore("Dead") -- DataStore that saves your progress
-------------------------------------------------------------------
local Players = game:GetService('Players')
local PlayerAdded = function(player)
local leaderstats = player:FindFirstChild('leaderstats') or Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local WOs = leaderstats:FindFirstChild(CountName) or Instance.new("IntValue", leaderstats)
WOs.Name = CountName
WOs.Value = 0
local CharacterAdded = function(character)
local d = true
character:WaitForChild("Humanoid").Died:connect(function()
if (d) then
d = false
WOs.Value += 1
end
end)
end
local Id = "Player_"..player.UserId --Where does your data saves
local Data
local success, errormessage = pcall(function() --Detects a error and success
Data = MyDataStore:GetAsync(Id) --Syncs your data with DataStore
end)
--If success, then its going save Levels's value
if success then
WOs.Value = Data
end
--Starts saving only when player left the game
game.Players.PlayerRemoving:Connect(function(player)
local Id = "Player_"..player.UserId
local Data = player.leaderstats.Cash.Value
local success, errormessage = pcall(function()
MyDataStore:SetAsync(Id, Data) --Id = location of saving, Data = what is it saving
end)
if success then --checks if it saved
print("dont forget to save noobie")--you can change the message to whatever u want
else --if its not saved
print("dumb u forgot to save")--you can change the message to whatever u want
warn(errormessage)
end
end)
CharacterAdded(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:connect(CharacterAdded)
end
for _, player in next, Players:GetPlayers() do
spawn(function()
PlayerAdded(player)
end)
end
Players.PlayerAdded:Connect(PlayerAdded)
I suggest trying this approach: Review the code and see if everything works as expected. If you have any questions, please feel free to ask me.
local Players = game:GetService("Players")
local MyDataStore = DataStoreService:GetDataStore("Dead")
local CountName = "Deaths"
local function onPlayerAdded(player)
local leaderstats = player:FindFirstChild("leaderstats") or Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local deaths = leaderstats:FindFirstChild(CountName) or Instance.new("IntValue", leaderstats)
deaths.Name = CountName
deaths.Value = 0
-- Attempt to load player data
local dataKey = "Player_" .. player.UserId
local success, data = pcall(function()
return MyDataStore:GetAsync(dataKey)
end)
if success and data then
deaths.Value = data
end
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
deaths.Value = deaths.Value + 1
end)
end
if player.Character then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
local function onPlayerRemoving(player)
local dataKey = "Player_" .. player.UserId
local deaths = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild(CountName)
if deaths then
local success, errorMessage = pcall(function()
MyDataStore:SetAsync(dataKey, deaths.Value)
end)
if not success then
warn("Failed to save data for " .. player.Name .. ": " .. errorMessage)
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
local a="Deaths" local _=game:GetService("DataStoreService") local c=_:GetDataStore("Dead") local _=game:GetService('Players') local a=function(f)local _=f:FindFirstChild('leaderstats')or Instance.new("Folder",f) _.Name="leaderstats" local e=_:FindFirstChild(a)or Instance.new("IntValue",_) e.Name=a e.Value=0 local d=function(_)local a=true _:WaitForChild("Humanoid").Died:connect(function()if(a)then a=false e.Value+=1 end end)end local _="Player_"..f.UserId local b local a,_=pcall(function()b=c:GetAsync(_)end) if a then e.Value=b end game.Players.PlayerRemoving:Connect(function(a)local _="Player_"..a.UserId local a=a.leaderstats.Cash.Value local a,_=pcall(function()c:SetAsync(_,a)end) if a then print("dont forget to save noobie")else print("dumb u forgot to save") warn(_)end end) d(f.Character or f.CharacterAdded:Wait()) f.CharacterAdded:connect(d)end for _,_ in next,_:GetPlayers()do spawn(function()a(_)end)end _.PlayerAdded:Connect(a)
I don’t think you necessarily need to shorten your code per se, but you definitely should modularize it. The issue is not that it’s too long, but rather it’s too messy. The PlayerAdded function has a lot of snippets that can be organized into their own separate, external functions.
As a general rule of thumb, each function should only serve one purpose. That PlayerAdded function is doing many many things, even creating its own sub-functions. If I were you, I’d considering refactoring the code so each separate “task” in the PlayerAdded function is isolated into its own function.