Killstreak being registered too many times

So basically whenever in my abilities when i kill someone it calls the onkill event on my module and update their killstreak +1 right but the thing is lets say im firing multiple skills at once on a player or if i like hit them with the same skill twice with their dead body laying on the ground it registers it as multiple killstreak even though the onkill function should only be incrementing 1 per death not per onkill function fired so how i would fix this

local DataStoreService = game:GetService("DataStoreService")
local killstreakDataStore = DataStoreService:GetDataStore("PlayerKillstreakDataStore")
local SoundEffects = game.Workspace:WaitForChild("SoundEffects")
local KillSound = SoundEffects:FindFirstChild("KillSound")

if not KillSound then
	warn("KillSound not found in SoundEffects folder!")
	return
end

-- Function to initialize the killstreak for the player
function Killstreaks.InitializeKillstreak(player)
	task.wait(5.5)

	-- Load saved killstreak from DataStore
	local savedKillstreak = 0
	local success, errorMessage = pcall(function()
		savedKillstreak = killstreakDataStore:GetAsync(player.UserId) or 0
	end)

	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local killstreak = leaderstats:FindFirstChild("Killstreak")
		if not killstreak then
			killstreak = Instance.new("IntValue")
			killstreak.Name = "Killstreak"
			killstreak.Parent = leaderstats
		end
		killstreak.Value = savedKillstreak
	end
end

-- Function to handle the kill event, increase killstreak, and play the sound
function Killstreaks.OnKill(killer)
	local leaderstats = killer:FindFirstChild("leaderstats")
	local killstreak = leaderstats and leaderstats:FindFirstChild("Killstreak")

	if killstreak then
		killstreak.Value = killstreak.Value + 1

		-- Check if the character is already available
		if killer.Character then
			playKillSound(killer)
		else
			-- Wait for the character if it's not already available
			killer.CharacterAdded:Connect(function(character)
				playKillSound(killer)
			end)
		end

		-- Save killstreak to DataStore
		local success, errorMessage = pcall(function()
			killstreakDataStore:SetAsync(killer.UserId, killstreak.Value)
		end)
		if not success then
			warn("Failed to save killstreak for player " .. killer.Name .. ": " .. errorMessage)
		end
	end
end

-- Function to play the KillSound
function playKillSound(killer)
	local killSoundClone = KillSound:Clone()

	-- Parent the sound to the character, ensure it's part of the character's humanoid root part
	killSoundClone.Parent = killer.Character:WaitForChild("HumanoidRootPart")

	-- Play the sound
	killSoundClone:Play()

	-- Clean up the sound after it finishes playing
	killSoundClone.Ended:Connect(function()
		killSoundClone:Destroy()
	end)
end

-- Reset killstreak if needed
function Killstreaks.ResetKillstreak(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	local killstreak = leaderstats and leaderstats:FindFirstChild("Killstreak")
	if killstreak then
		killstreak.Value = 0
		-- Save the reset killstreak to DataStore
		local success, errorMessage = pcall(function()
			killstreakDataStore:SetAsync(player.UserId, killstreak.Value)
		end)
		if not success then
			warn("Failed to save reset killstreak for player " .. player.Name .. ": " .. errorMessage)
		end
	end
end

return Killstreaks ```
2 Likes

You can set an attribute (on the server) whenever someone claims the kill and check if that attribute exists or not when attempting to set the killstreak.

I definetly don’t recommend sending a remote to the server for setting the killstreak, this can easily be exploited without proper protection. Instead try setting this up on the server.

2 Likes

You should setup a kills tracker system and also you shouldn’t be using your datastores that often

1 Like