Why isnt my code working?

Im trying to make a game where when you kill someone your speed goes up and when you die you get slower.

local player = game:GetService("Players").LocalPlayer -- Assuming you're working with local player
local speed = 16 -- Default speed value
local deathSpeedDecrease = 4 -- Amount to decrease speed when the player dies
local killSpeedIncrease = 2 -- Amount to increase speed when the player gets a kill

local function handleDeath()
    speed = speed - deathSpeedDecrease
end

local function handleKill()
    speed = speed + killSpeedIncrease
end

local function updateSpeed()
    player.Character.Humanoid.WalkSpeed = speed -- Adjust the player's speed
end

-- Listen for player deaths and kills
player.CharacterAdded:Connect(function(character)
    character:WaitForChild("Humanoid").Died:Connect(handleDeath)
end)

-- Assuming you have a way to track player kills, listen for kills and call the kill function
game:GetService("ReplicatedStorage").PlayerKilledEvent.OnClientEvent:Connect(handleKill)

-- Continuously update the player's speed
while wait(0.1) do
    updateSpeed()
end

Whats wrong? The speed doesnt change when you kill/die. The weapon is a gun. I tested it.

1 Like
  1. Instead of doing x = x + y do x += y which will add the number
  2. You need to put the last loop in a task.spawn() function, like:
task.spawn(function()
while wait(0.1) do
    updateSpeed()
end
end)

And there’s no need for that, you can simply call updateSpeed() after handleKill/Death

1 Like

Can you write an example? Thanks

I believe the problem is that the Event doesnt run because there is no code that tracks the kill. If im correct, then your speed should still decrease upon death.

Post is wrongly written. Tell us what exactly doesn’t work. The increase, the decrease or both? Is the code server script or client script? Have you checked the console?
Debug your code by placing breakpoint or prints.
It’s waste of your and our time to investigate what’s wrong in your code if you didn’t test it properly.

1 Like

How would I make it increase? Thanks

I wrote the details to your questions in the description. Sorry (:

Whats the difference between the 2

He has a RemoteEvent, he doesn’t say if there’s code behind it but there should be I think

If I’m not wrong a bit of performance + they look better to me

Im pretty sure it its bc its a local script, you need to change the speed on the server side

There’s no difference between the two, they work the same way

They do?
I remember hearing somewhere they have a bit of better performances…

Even though it does look better

Well, first you should add in the gun’s script:

–an event for when the bullet hits something , for example if you use Raycast to determine where the bullet goes, you can just check raycast_result.Instance

–inside raycast_result.Instance, insert a value called “target”.
raycast_result.Instance should be the player that the bullet hits.
now, you can check target.Parent.Humanoid’s health, and if it’s lower than 1, you killed them, if that happens then you fire to a ServerScript and you change the player’s speed from there.

it’s never the best idea to have a “character_added:connect” event and put a “humanoid.died” event inside of it. remember that character_added fires whenever any character is added, and just adding another listener inside of it just wastes time when there’s easier and more optimized ways of doing this.