-
What do you want to achieve? Fix my leaderstats.
-
What is the issue? My leaderstats doesn’t save my stats, i already tried 2 scripts but nothing work.
-
What solutions have you tried so far? I tried to look at youtube and API but nothing.
1ST script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Slaps")
game.Players.PlayerAdded:Connect(function(Player)
local sucess, PlayerData = pcall(function()
return DataStore:GetAsync(Player.UserId)
end)
print(sucess)
if sucess then
Player:SetAttribute("Slaps", PlayerData or 0)
print(PlayerData)
else
warn("failed")
end
end)
--This one didn't work.
2ND Script (From my “solved” Topic)
--Script in ServerScriptService:
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
-- Wait for our DataStoreVersion object to spawn in, and get it's value
local dV = ServerStorage:WaitForChild("DataStoreVersion").Value
-- Wait for our DataStore2 module object to spawn in, and require it
local dataStore2 = require(ServerStorage:WaitForChild("DataStore2"))
-- Combine all our DataStores into one
dataStore2.Combine(
"MasterKey"..dV, -- Dont change this one
"Slaps"..dV -- Your slaps.
--"Slaps2"..dV -- Just a example of more data you want to save, could also be named coins ect. (Just remember to add a comma after the dV in the Slaps above
)
-- Define DefaultData for our DataStore's
local Default_Slaps = 0
--local Default_Slaps2 = 0
Players.PlayerAdded:Connect(function(Player)
-- Make a warn in output, to tell us that the Player's data started loading
warn("[SERVER]: Started loading [PLAYER: "..Player.Name.."'s] Datastore-data!")
Player:SetAttribute("DataLoadingFinished", false) -- Can be used by other scripts, to determine if our data is loaded, before we do change it their data
-- If you want, just change the Folder below to be your leaderstats folder
-- Create a Folder for our replicated PlayerData, and put it inside our Player
local Folder_PlayerDataFolder = Instance.new("Folder");Folder_PlayerDataFolder.Name = "PlayerData";Folder_PlayerDataFolder.Parent = Player
-- Create a IntValue to store the number of Slaps
local Value_Slaps = Instance.new("IntValue");Value_Slaps.Name = "Slaps";Value_Slaps.Parent = Folder_PlayerDataFolder -- Used to replicate our current data to the player or any player in the server
-- Create a IntValue to store the number of Slaps
--local Value_Slaps2 = Instance.new("IntValue");Value_Slaps2.Name = "Slaps2";Value_Slaps2.Parent = Folder_PlayerDataFolder
local DS_Slaps = dataStore2("Slaps"..dV,Player) -- Define our Slaps Datastore
--local DS_Slaps2 = dataStore2("Slaps2"..dV,Player)
local function Update_Slaps(Value) -- We create a function here, to always update the replicated data to be equal value to our datastore data. This function will run everytime the value of Slaps in our datastore updates
Value_Slaps.Value = Value
warn("[SERVER]: Update_Slaps ran...")
end
--[[local function Update_Slaps2(Value)
Value_Slaps2.Value = Value
warn("[SERVER]: Update_Slaps2 ran...")
end]]
Update_Slaps(DS_Slaps:Get(Default_Slaps)) -- We run the Update_Slaps function, to initialize the default replicated Slaps value in our PlayerData folder. ":Get()" gets our current data of Slaps. "Default_Slaps" is used if no data was found, hit to why it's the default value.
--Update_Slaps2(DS_Slaps2:Get(Default_Slaps2))
DS_Slaps:OnUpdate(Update_Slaps) -- Everytime our Slaps data in our DataStore updates, run the Update_Slaps function
--DS_Slaps2:OnUpdate(Update_Slaps2)
-- OnCharacterAdded
Player.CharacterAdded:Connect(function(Character) -- Since the character does not automatically spawn, we create a function to make it automatically spawn on death, after we've finished loading all the data. It's essentially the same thing the CharacterAutoLoads does.
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
task.wait(5) -- Delay before spawn, change to your liking.
Player:LoadCharacter()
end)
end)
warn("[SERVER]: Finished loading [PLAYER: "..Player.Name.."'s] Datastore-data!")
Player:SetAttribute("DataLoadingFinished", true)
Player:LoadCharacter() -- Also turn off CharacterAUtoLoads under "Players" in Explorer. Normally we wont have the player to spawn before all the data is loaded
end)
Whats wrong on these scripts?