Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

can someone help me script it so that if a player kills themself they get kills subtracted?

hello, can someone help me script it so that if a player kills themself they get kills subtracted and that the least amount of kills a player can get is 0 kills instead of being able to get -x amount of kills

game:GetService("Players").PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

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

	player.CharacterAdded:Connect(function(char)

		char:WaitForChild("Humanoid").Died:Connect(function()
			local creator = char:WaitForChild("Humanoid"):FindFirstChild("creator")

			if creator and creator.ClassName == "ObjectValue" then
				local murderer = creator.Value

				murderer:WaitForChild("leaderstats"):WaitForChild("Kills").Value += 1

			end
		end)

		if kills.Value >= 10 then
			
			char:FindFirstChild("hat"):Destroy()
			
			game.Lighting.hat1:Clone().Parent = char
			
		end
	end)

end)

2 answers

✓ Accepted answer

To put a cap to the player's kills so it doesn't go lower than 0. You have to make a PropertyChangedSignal(), This makes it so whenever the value of your kills change. It will fire a function.

So what we do with this function is:

kills:GetPropertyChangedSignal("Value"):Connect(function() --If value changes, It fires a function.
			
	if kills.Value < 0 then --If the kills value is under 0, It fires the code underneath
		kills.Value = 0 --Changes kills value to 0
	end
			
end)

I have no idea how the murderer value works, but you'd need to create a value of Who the murderer is and check if the Murderer killed the player. If not, You remove 1 kill from the player.

So something like this:

	local MurderFolder = Instance.new("Folder") --Makes MurderFolder (To keep things neat)
	MurderFolder.Name = "MurderFolder"
	MurderFolder.Parent = player
	
	local DmgMurderer = Instance.new("BoolValue") --Makes Boolvalue, Checking if player got killed by Murderer.
	DmgMurderer.Name = "KilledByMurderer"
	DmgMurderer.Parent = MurderFolder
	
	player.CharacterAdded:Connect(function(char)

		char:WaitForChild("Humanoid").Died:Connect(function() --If player died, Fires Function.
			if DmgMurderer.Value ~= true then --If the murderer hasn't killed the player, fires code underneath.
				kills.Value = kills.Value - 1 --Removes a kill.
			end
		end)

	end)

How to make this work? You can find out if the player has been hit by the murderer's weapon and check if their health is 0, If so, you change DmgMurderer.Value to true.

This is all I thought of but yeah, hopefully this helps somehow.

This answer is quite simple: compare the murderer (ObjectValue) to the player:

player.CharacterAdded:Connect(function(char)

	char:WaitForChild("Humanoid").Died:Connect(function()
		local creator = char:WaitForChild("Humanoid"):FindFirstChild("creator")

		if creator and creator.ClassName == "ObjectValue" then
			local murderer = creator.Value

			if murderer == player then
				murderer:WaitForChild("leaderstats"):WaitForChild("Kills").Value -= 1 -- they killed themself
			else
				murderer:WaitForChild("leaderstats"):WaitForChild("Kills").Value += 1
			end

		end
	end)

	if kills.Value >= 10 then

		char:FindFirstChild("hat"):Destroy()

		game.Lighting.hat1:Clone().Parent = char

	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.