Hello developers,
These days I was bored and I decided to create a competitive game but the only problem is that I can’t create a leaderstats with kills. So I want to make a leaderstats with kills and whenever you kill somebody with a tool the kills would increase by +1. I have this code right here but it doesn’t work, the output hasn’t any error or something like that.
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr
local strength = Instance.new("IntValue")
strength.Name = "Strength"
strength.Parent = stats
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = stats
local star = Instance.new("IntValue")
star.Name = "Stars"
star.Parent = stats
local kill = Instance.new("IntValue")
kill.Name = "Kills"
kill.Parent = stats
plr.CharacterAdded:connect(function(char)
local humanoid = char:FindFirstChild("Humanoid")
local tag = humanoid:FindFirstChild("creator")
if tag then
local killer = tag.Value
if killer then
killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
end
end
end)
end)
Can someone please help me with this problem?
Thanks,
Daniel
Alright. Disable the DataStore script that you have and use the script below, which has both the leaderstats and DataStore in one script:
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)
i don’t think leaderstats for kill will work you should make it other way where you will add script to all characters that checks if they got killed and after that it will give kill to the killer I was already making it
here put script in StarterPlayer.StarterCharacterScripts
local Humanoid = script.Parent.Humanoid
function Dead()
local tag = Humanoid:FindFirstChild("creator")
print(tag)
if tag ~= nil then
print(tag.Value)
if tag.Value ~= nil then
local leaderstatsname = "your leaderstat name"
local leaderstats = tag.Value:FindFirstChild("leaderstats")
print(leaderstats)
if leaderstats ~= nil then
local value = leaderstats[leaderstatsname]
value.Value = value + 1
end
end
end
end
Humanoid.Died:Connect(Dead)
First you Disable the DataStore script, then Delete the leaderstats script, then create a new server script in serverscriptservice, and then paste the code above into the script.
Never handle leaderstats on the client. Exploiters can easily modify it to get infinite kills. Also, server scripts can’t be put in StarterCharacterScripts iirc