How do I make player speed increase when player gets 2 kills

Hey Robloxians,
I have made a npc which gives kill points everytime it is eliminated, how do I make that if a player gets 2 or more kills, that player’s speed will increase for 5 seconds and then it will be back to normal and the player kills reset?

My kill value is inside player in the folder called leaderstats and the value name is ZombieTempKills

That’s pretty easy to make, you can make a number value that will store how much kills player has, and then set it to 0 and increase speed once it reaches the amount of 2:

local AmountOfKills -- your number value
local Character -- define character here
local Increase = 5 -- define increase here
local defaultSpeed = 16 -- the default speed

AmountOfKills.Changed:Connect(function(amount)
   if amount == 2 then
       AmountOfKills.Value = 0
       Character.Humanoid.WalkSpeed += increase
       task.wait(5)
       Character.Humanoid.WalkSpeed = defaultSpeed
   end
end)

he said that if the player has 2 or more kills.

Yes I know, the script I made will increase player’s speed by 5 once the player kills 2 NPCs and then get back to normal after 5 seconds.

Consider “Character” the character in question

local WALK_SPEED_INCREASE = 5
local INCREASE_TIME = 5

local Humanoid = Character:WaitForChild("Humanoid")
local KillsValue = Character:WaitForChild("Kills") -- change the path if it's different

local function OnChanged(Value: number)
	if Value < 2 then return end

	KillsValue.Value = 0
	Humanoid.WalkSpeed += WALK_SPEED_INCREASE
	task.wait(INCREASE_TIME)
	Humanoid.WalkSpeed -= WALK_SPEED_INCREASE
end

KillsValue.Changed:Connect(OnChanged)

it didn’t work for some reason

this code doesn’t seem to work either

should I put this script in server script server or starterplayerscripts

It should, can you please show how did you implement it?

I suggest you to learn basics first before actually trying to make something on your own. The script could be placed anywhere where it will run, but preferably on the server side, so it won’t be exploitable.

try that:

local WALK_SPEED_INCREASE = 5
local INCREASE_TIME = 5

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local KillsValue = player:WaitForChild("leaderstats").ZombieTempKills

local function OnChanged(Value: number)
	if Value < 2 then return end

	KillsValue.Value = 0
	Humanoid.WalkSpeed += WALK_SPEED_INCREASE
	task.wait(INCREASE_TIME)
	Humanoid.WalkSpeed -= WALK_SPEED_INCREASE
end

KillsValue.Changed:Connect(OnChanged)

and put in starterplayerscripts

local Character = game.Players.LocalPlayer -- define character here
local AmountOfKills = Character.leaderstats.ZombieTempKills -- your number value
local Increase = 20 -- define increase here
local defaultSpeed = 16 -- the default speed

AmountOfKills.Changed:Connect(function(amount)
	if amount == 2 then
		AmountOfKills.Value = 0
		Character.Humanoid.WalkSpeed += Increase
		task.wait(5)
		Character.Humanoid.WalkSpeed = defaultSpeed
	end
end)

You defined the character wrong way. In your case, the character is a player, but player doesn’t have a Humanoid inside of it.

local Character = game.Players.LocalPlayer -- define character here
local AmountOfKills = Character.leaderstats.ZombieTempKills -- your number value
local Increase = 20 -- define increase here
local defaultSpeed = 16 -- the default speed

AmountOfKills.Changed:Connect(function(amount)
	if amount == 2 then
		AmountOfKills.Value = 0
		Character.Character.Humanoid.WalkSpeed += Increase
		task.wait(5)
		Character.Humanoid.WalkSpeed = defaultSpeed
	end
end)

That should work

Still it didnt worked. There are no errors in output too

It didnt work. I tried by parenting the script inside of serverscriptservice and starterplayerscripts both

I tried in both in serverscriptservice and starterplayerscripts

Is your script a local script? The script must be a local script and parented to a StarterPlayerScripts. Also the problem might be that leaderstats folder didn’t appear before the script ran, so try adding WaitForChilds.

u are trying to get with amount any value of the leaderstats try use that:

local player = game.Players.LocalPlayer -- define player here
local Character  = player.Character or player.CharacterAdded:Wait()  -- define character here
local AmountOfKills = player:WaitForChild("leaderstats").ZombieTempKills -- your number value
local Increase = 20 -- define increase here
local defaultSpeed = 16 -- the default speed

AmountOfKills.Changed:Connect(function(amount)
	if amount >= 2 then
		AmountOfKills.Value = 0
		Character:WaitForChild("Humanoid").WalkSpeed += Increase
		task.wait(5)
		Character:WaitForChild("Humanoid").WalkSpeed = defaultSpeed
	end
end)