Broken kill for coins and kills

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 coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderstats

local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Value = 0
kills.Parent = leaderstats

end)

Error:

Thanks!

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)
3 Likes

remove the " and creator.Value ~= nil" if the value starts out 0 then im pretty sure the value will always be nil.

I tried it but it still doesn’t give coins or kills.

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.

I found out that if I use the classic sword model it gives me coins and kills but If I use my own weapon it wouldn’t work here’s the script I use:

function onTouch(hit)

script.Disabled = true

local humanoid = hit.Parent:FindFirstChild("Humanoid")

humanoid.Health = humanoid.Health - 50

end
script.Parent.Touched:Connect(onTouch)

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.

1 Like

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)
1 Like

So what would this script do, would it insert a value into something so that the leaderstats can fine the “creator” value without getting an error?

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.

Oh ok makes sense now, i’ll have to try out the code!

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)

it gives me coins and kills but one shots the player even though its 50 damage

It doesn’t do any damage some reason.

Try this…

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)

It doesn’t do any damage still.

Edited…

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)

It still doesn’t do any damage

Is it because I put the script in my handle?