Hello developers, I have a script that’s supposed to give you coins and kills leaderstats value whenever you kill a player. I found a problem with the script though, So if you kill a player you don’t get coins or kills
here’s the kill for coins and kills script in serverscriptservice:
game.Players.PlayerAdded:Connect(function(Player)
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.Coins.Value += 15
leaderstats.Kills.Value += 1
end
end)
end)
end)
And here’s my leaderstats script:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Coins = Instance.new("NumberValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
local Kills = Instance.new("NumberValue")
Kills.Name = "Kills"
Kills.Value = 0
Kills.Parent = leaderstats
leaderstats.Parent = Player
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild("Humanoid", 3)
if Humanoid then
Humanoid.Died:Connect(function()
local creator = Humanoid:WaitForChild("Creator", 1)
if creator then
local leaderstats = creator.Value:FindFirstChild("leaderstats")
if leaderstats then
leaderstats.Coins.Value += 15
leaderstats.Kills.Value += 1
end
end
end)
end
end)
end)
Can you show me the script which kills the player? The creator value isn’t automatically created so a script must create it. I’m not sure if your damaging script creates the creator value though.
The only reason @NicoleSydor’s script doesn’t give any coin is because one of the statement isn’t equal to true. I suppose the problem is coming from the if creator then statement, so you probably have to fix the script that handle the “creator” thing.
At the time I posted my script I was on mobile and had automatically assumed that the topic creator used a script which created the creator value. I know that scripts don’t create the creator value on their own so I thought of creating a script for that, but I don’t have a good editor for mobile and I didn’t have any time. Thank you for explaining this though.
Alright, I have reformed your script to add the creator value inside of the Humanoid. The killer should be able to be identified in the other script now.
Here is your new weapon script…
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local Tool = script.Parent:FindFirstAncestorOfClass("Tool")
local Damage = 50
function tagHumanoid(humanoid, player)
if humanoid:IsA("Humanoid") then
if not humanoid:FindFirstChild("creator") then
local creator = Instance.new("ObjectValue")
creator.Name = "creator"
creator.Value = player
Debris:AddItem(creator, 2)
creator.Parent = humanoid
else
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
end
end
function onTouch(hit)
if cooldown == true return end
cooldown = false
if Tool then
local Model = Tool.Parent
local Player = Players:GetPlayerFromCharacter(Model)
if Player then
local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if Humanoid and Humanoid.Parent ~= Model then
tagHumanoid(Humanoid, Player)
Humanoid:TakeDamage(Damage)
end
end
end
end
script.Parent.Touched:Connect(onTouch)
It creates an ObjectValue called “creator” inside of the target’s Humanoid and it turns into a Debris item after 2 seconds or it basically destroys the ObjectValue. The script which is connected to the function where the Player’s Humanoid dies searches for the creator value through the Humanoid, and if the creator value is found, the script will reference the killer through the creator.Value and, in this case it will give them Coins and Kills. Roblox doesn’t automatically detect who kills you of course, so you would need to create a value or variable to reference who the killer is.
Also, I already have a function for the TakeDamage part, should i leave the function along or change it?
Also here is the Kill Function:
local function playerCheck(otherPart)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if humanoid and humanoid.Parent.Name ~= Bullet.Attacker.Value then
humanoid:TakeDamage(40)
end
end
Bullet.Touched:Connect(playerCheck)
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local Tool = script.Parent:FindFirstAncestorOfClass("Tool")
local Damage = 50
local cooldown = false
local duration = nil -- change cooldown time or keep nil to wait()
function tagHumanoid(humanoid, player)
if humanoid:IsA("Humanoid") then
if not humanoid:FindFirstChild("creator") then
local creator = Instance.new("ObjectValue")
creator.Name = "creator"
creator.Value = player
Debris:AddItem(creator, 2)
creator.Parent = humanoid
else
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
end
end
function onTouch(hit)
if cooldown == true return end
cooldown = true
if Tool then
local Model = Tool.Parent
local Player = Players:GetPlayerFromCharacter(Model)
if Player then
local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if Humanoid and Humanoid.Parent ~= Model then
tagHumanoid(Humanoid, Player)
Humanoid:TakeDamage(Damage)
if duration == nil then
wait()
cooldown = false
return
end
wait(duration)
cooldown = false
else
cooldown = false
end
else
cooldown = false
end
else
cooldown = false
end
end
script.Parent.Touched:Connect(onTouch)
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local Damage = 50
local cooldown = false
local duration = nil -- change cooldown time or keep nil to wait()
function tagHumanoid(humanoid, player)
if humanoid:IsA("Humanoid") then
if not humanoid:FindFirstChild("creator") then
local creator = Instance.new("ObjectValue")
creator.Name = "creator"
creator.Value = player
Debris:AddItem(creator, 2)
creator.Parent = humanoid
else
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
end
end
function onTouch(hit)
if cooldown == true return end
cooldown = true
local Tool = script.Parent.Parent
if Tool then
local Model = Tool.Parent
local Player = Players:GetPlayerFromCharacter(Model)
if Player then
local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if Humanoid and Humanoid.Parent ~= Model then
tagHumanoid(Humanoid, Player)
Humanoid:TakeDamage(Damage)
if duration == nil then
wait()
cooldown = false
return
end
wait(duration)
cooldown = false
else
cooldown = false
end
else
cooldown = false
end
else
cooldown = false
end
end
script.Parent.Touched:Connect(onTouch)