Alright. Bingo. I was being stupid. Delete your leaderstats script and your datastore script. Delete the script in StarterCharacterScripts too.
Make a new regular script in ServerScriptService.
Then paste this code inside:
local Players = game:GetService("Players")
local TestService = game:GetService("TestService")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Strength = Instance.new("IntValue")
Strength.Name = "Strength"
Strength.Parent = leaderstats
Strength.Value = 0
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
Coins.Value = 0
local Stars = Instance.new("IntValue")
Stars.Name = "Stars"
Stars.Parent = leaderstats
Strength.Value = 0
local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Parent = leaderstats
Kills.Value = 0
local value1Data = Strength
local value2Data = Coins
local value3Data = Stars
local value4Data = Kills
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function(Died)
local creator = Character.Humanoid:FindFirstChild("creator")
local leaderstats = creator.Value:FindFirstChild("leaderstats")
if creator ~= nil and creator.Value~= nil then
leaderstats.Kills.Value += 1
end
end)
end)
local s, e = pcall(function()
value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
value2Data = DataStore:GetAsync(player.UserId.."-Value2") or 0
value3Data = DataStore:GetAsync(player.UserId.."-Value3") or 0
value4Data = DataStore:GetAsync(player.UserId.."-Value4") or 0
end)
if s then
Strength.Value = value1Data --setting data if its success
Coins.Value = value2Data
Stars.Value = value3Data
Kills.Value = value4Data
else
TestService:Error(e) --if not success then we error it to the console
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Strength.Value) --setting data
DataStore:SetAsync(player.UserId.."-Value2", player.leaderstats.Coins.Value)
DataStore:SetAsync(player.UserId.."-Value3", player.leaderstats.Stars.Value)
DataStore:SetAsync(player.UserId.."-Value4", player.leaderstats.Kills.Value)
end)
if not s then TestService:Error(e)
end
end)
game:BindToClose(function(player)
if not RunService:IsStudio() then
local s, e = pcall(function()
DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Strength.Value) --setting data
DataStore:SetAsync(player.UserId.."-Value2", player.leaderstats.Coins.Value)
DataStore:SetAsync(player.UserId.."-Value3", player.leaderstats.Stars.Value)
DataStore:SetAsync(player.UserId.."-Value4", player.leaderstats.Kills.Value)
end)
if not s then TestService:Error(e)
end
end)
end
Are you positive that both the DataStore script and your previous leaderstats scripts is deleted? And that the script @madonchik123 told you to add in StarterCharacterScripts is deleted as well?
Wait wait wait! I found that in you script at the final the ends were wrong so I fixed them and now the leaderstats is apearing but when I kill an NPC from tooblox the kills value doesn’t go up. But when I colelct a coin the coins value are going up. Is that because I use an NPC and not a real player??
local Players = game:GetService("Players")
local TestService = game:GetService("TestService")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Strength = Instance.new("IntValue")
Strength.Name = "Strength"
Strength.Parent = leaderstats
Strength.Value = 0
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
Coins.Value = 0
local Stars = Instance.new("IntValue")
Stars.Name = "Stars"
Stars.Parent = leaderstats
Stars.Value = 0
local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Parent = leaderstats
Kills.Value = 0
local value1Data = Strength
local value2Data = Coins
local value3Data = Stars
local value4Data = Kills
player.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
local creator = tag.Value
local leaderstats = player:WaitForChild("leaderstats")
leaderstats.Kills.Value = leaderstats.Kills.Value + 1
end
end)
end)
local s, e = pcall(function()
value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
value2Data = DataStore:GetAsync(player.UserId.."-Value2") or 0
value3Data = DataStore:GetAsync(player.UserId.."-Value3") or 0
value4Data = DataStore:GetAsync(player.UserId.."-Value4") or 0
end)
if s then
Strength.Value = value1Data --setting data if its success
Coins.Value = value2Data
Stars.Value = value3Data
Kills.Value = value4Data
else
TestService:Error(e) --if not success then we error it to the console
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Strength.Value) --setting data
DataStore:SetAsync(player.UserId.."-Value2", player.leaderstats.Coins.Value)
DataStore:SetAsync(player.UserId.."-Value3", player.leaderstats.Stars.Value)
DataStore:SetAsync(player.UserId.."-Value4", player.leaderstats.Kills.Value)
end)
if not s then TestService:Error(e)
end
end)
game:BindToClose(function(player)
if not RunService:IsStudio() then
local s, e = pcall(function()
DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Strength.Value) --setting data
DataStore:SetAsync(player.UserId.."-Value2", player.leaderstats.Coins.Value)
DataStore:SetAsync(player.UserId.."-Value3", player.leaderstats.Stars.Value)
DataStore:SetAsync(player.UserId.."-Value4", player.leaderstats.Kills.Value)
end)
if not s then TestService:Error(e)
end
end)
end)
Hi! Just wanted to point this out. It’s certainly not because he doesn’t set the value’s to 0
The value for a new IntValue is default set to 0, so no need to do that at least.