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.
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.
–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.