Hey there, I’m Shark.
I have a leaderstats that saves, but I want to know how to add a kill for money script to it.
I’ve tried, but it doesn’t work, so I need help on how to do it.
Here’s the script :
local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
game.Players.PlayerAdded:Connect(function(player)
local moneyValue
local success, err = pcall(function()
moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
if success then
money.Value = moneyValue
else
print("Failed to load data")
end
end)
local function save(player)
local success, err = pcall(function()
moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
end)
if success then
print("Saved data")
else
print("Failed to load data")
end
end
local function autosave()
while wait(5) do
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
save(player)
end
end
end
remote.OnServerEvent:Connect(function(player, amount)
player.leaderstats.Money.Value += amount
end)
spawn(autosave)
^^This is the script for the saving leaderstats^^
The following script is the kill for money script.
Here it is :
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
money.Value = money.Value + 1
end
end
end)
end)
end)
So, I don’t know where to add the kill for money part into the script, and some help would be nice.
I basically need somewhere to add that part to the script, so when a player kills someone, they get money, and the money saves.
Unrelated to the OP but still should be pointed out:
You’re calling SetAsync way too frequently, which would result in a lot of DataStore queue entries to be called on
I’d recommend increasing the time for the autosave() to happen often
You could just encase the CharacterAdded event inside your PlayerAdded event so that it’ll also detect when to give the stats when someone dies:
game.Players.PlayerAdded:Connect(function(player)
local moneyValue
local success, err = pcall(function()
moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
if success then
money.Value = moneyValue
else
print("Failed to load data")
end
player.CharacterAdded:Connect(function(character)
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
local tag = Humanoid:FindFirstChild("creator")
if tag and tag.Value then
money.Value += 1
end
end)
end)
end)
Could you possibly send me the whole script, because there seems to be errors appearing, and I don’t know why, if you haven’t noticed already, I’m not the greatest at scripting.
local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
game.Players.PlayerAdded:Connect(function(player)
local moneyValue
local success, err = pcall(function()
moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
end)
local function save(player)
local success, err = pcall(function()
moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
end)
if success then
print("Saved data")
else
print("Failed to load data")
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
local money = Instance.new("IntValue")
Humanoid.Died:Connect(function()
local tag = Humanoid:FindFirstChild("creator")
if tag and tag.Value then
money.Value += 1
end
end)
end)
end)
local function autosave()
while wait(5) do
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
save(player)
end
end
end
remote.OnServerEvent:Connect(function(player, amount)
player.leaderstats.Money.Value += amount
end)
spawn(autosave)
Managed to do this, although I’m not sure if it would work…
local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
game.Players.PlayerAdded:Connect(function(player)
local moneyValue
local success, err = pcall(function()
moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
if success then
money.Value = moneyValue
else
print("Failed to load data")
end
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
local tag = Humanoid:FindFirstChild("creator")
if tag and tag.Value then
local killerstats = tag.Value:FindFirstChild("leaderstats")
if killerstats then
killerstats.money.Value += 1
end
end
end)
end)
end)
local function save(player)
local success, err = pcall(function()
moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
end)
if success then
print("Saved data")
else
print("Failed to load data")
end
end
local function autosave()
while wait(5) do
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
save(player)
end
end
end
remote.OnServerEvent:Connect(function(player, amount)
player.leaderstats.Money.Value += amount
end)
spawn(autosave)
local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
game.Players.PlayerAdded:Connect(function(player)
local moneyValue
local success, err = pcall(function()
moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
if success then
money.Value = moneyValue
else
print("Failed to load data")
end
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
print("OOF")
local tag = Humanoid:FindFirstChild("creator")
if tag and tag.Value then
local killerstats = tag.Value:FindFirstChild("leaderstats")
if killerstats then
print("This should work")
killerstats.Money.Value += 1
end
end
end)
end)
end)
local function save(player)
local success, err = pcall(function()
moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
end)
if success then
print("Saved data")
else
print("Failed to load data")
end
end
local function autosave()
while wait(5) do
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
save(player)
end
end
end
remote.OnServerEvent:Connect(function(player, amount)
player.leaderstats.Money.Value += amount
end)
spawn(autosave)
Yes, this works, but there’s another problem, the person who kills me gets the money, I need it the other way around, if I kill someone, I get the money, also it doesn’t save…
local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
game.Players.PlayerAdded:Connect(function(player)
local moneyValue
local success, err = pcall(function()
moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
if success then
money.Value = moneyValue
else
print("Failed to load data")
end
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
print("OOF")
local tag = Humanoid:FindFirstChild("creator")
if tag and tag.Value then
money.Value += 1
end
end)
end)
end)
local function save(player)
local success, err = pcall(function()
moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
end)
if success then
print("Saved data")
else
print("Failed to load data")
end
end
local function autosave()
while wait(60) do
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
save(player)
end
end
end
remote.OnServerEvent:Connect(function(player, amount)
player.leaderstats.Money.Value += amount
end)
spawn(autosave)
I wouldn’t rely on saving the data every time the Player’s leaderstat changes, as that’ll result in a ton of DataStore queue entries
Same as the last time, so basically, this script is in ServerScriptService, and it everytime someone kills me, I get the points and also, it still doesn’t save.
The creator tag will work when the Player who killed someone will give them the stats, if you’re wanting to only make yourself get the stats just implement another conditional check if the Player object’s Name is equal to your name
It still depends what how you’re wanting to implement this, if you only want yourself to gain the Money stats just add a check if the Humanoid that Died is equal to the Owner, then do this:
local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
game.Players.PlayerAdded:Connect(function(player)
local moneyValue
local success, err = pcall(function()
moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
if success then
money.Value = moneyValue
else
print("Failed to load data")
end
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
print("OOF")
if Character.Name == "ii_sharkboy" then
local tag = Humanoid:FindFirstChild("creator")
if tag and tag.Value then
save(player)
money.Value += 1
end
end
end)
end)
end)
local function save(player)
local success, err = pcall(function()
moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
end)
if success then
print("Saved data")
else
print("Failed to load data")
end
end
local function autosave()
while wait(60) do
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
save(player)
end
end
end
remote.OnServerEvent:Connect(function(player, amount)
player.leaderstats.Money.Value += amount
end)
spawn(autosave)
He wants it so when someone kills anyone they get money for it. For example, I killed you so now I get +1 coin.
I think something like this scripting tutorial would help you achieve what you need.
Some random script I found(it might error I didn’t check it):
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
currency1.Value = currency1.Value + 10 --This is the reward after the player died.
end
end
end)
end)
end)