Server script stops working after the players death

Hello, I’ve recently stumbled upon an issue.

I have made a script that constantly checks the player’s HumanoidRootPart velocity and destroys the character after reaching a certain limit.

The script works perfectly fine the first time, but after the player dies it stops working further. It might be because the script doesn’t reset itself after the player dies, but I have no idea what to do to fix the issue.

I’ve tried making it a Local script in StarterCharacterScripts, but it just didn’t work for some reason.

Any help is appreciated.

local Players = game:GetService("Players")

local currentFlingCount = 0
local Flinglimit = 30

local debounce = true
local deathDebounce = true

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
		local flingThreshold = 85

		game:GetService("RunService").Heartbeat:Connect(function()
			
			if humanoidRootPart and humanoidRootPart.AssemblyLinearVelocity.Magnitude > flingThreshold and deathDebounce == true then
				debounce = true
				currentFlingCount += 1
				print(player.Name .. " is being flung")
				
			elseif humanoidRootPart.AssemblyLinearVelocity.Magnitude < flingThreshold and debounce == true then
				debounce = false
				print(currentFlingCount)
				currentFlingCount = 0
			end
			
			---------------------------------------------------------------------------------------------------
			
			if currentFlingCount < Flinglimit then
				--print("Threshold has not been exceeded")
			elseif currentFlingCount > Flinglimit and deathDebounce == true then
				deathDebounce = false
				print("Threshold has exceeded the fling limit")
			
				character:Destroy()
				print("Character destroyed")
				
				wait(game.Players.RespawnTime)
				
				player:LoadCharacter()
				print("Character loaded")
			end
			
		end)
	end)
end)
1 Like

The issue is that your debounce variables are not being cleared properly. Additionally, if there are multiple players in the game, the debounces will be shared between them instead of per player.

One fix would be to move

local debounce = true
local deathDebounce = true

local currentFlingCount = 0

to the start of the CharacterAdded function.


Also, you should disconnect the RunService connection when the player dies, to prevent a memory leak / performance issues. Because of the change required for that, I would actually recommend modifying the code further, and placing the script in StarterCharacterScripts (to simplify the connection disconnecting). The modified code below should work in StarterCharacterScripts:

local Players = game:GetService("Players")
local Flinglimit = 30
local flingThreshold = 85

local character = script.Parent
local player = Players:GetPlayerFromCharacter(character) 

local debounce = true
local deathDebounce = true
local currentFlingCount = 0

local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
game:GetService("RunService").Heartbeat:Connect(function()
	if humanoidRootPart and humanoidRootPart.AssemblyLinearVelocity.Magnitude > flingThreshold and deathDebounce == true then
		debounce = true
		currentFlingCount += 1
		print(player.Name .. " is being flung")

	elseif humanoidRootPart.AssemblyLinearVelocity.Magnitude < flingThreshold and debounce == true then
		debounce = false
		print(currentFlingCount)
		currentFlingCount = 0
	end

	---------------------------------------------------------------------------------------------------

	if currentFlingCount < Flinglimit then
		--print("Threshold has not been exceeded")
	elseif currentFlingCount > Flinglimit and deathDebounce == true then
		deathDebounce = false
		print("Threshold has exceeded the fling limit")

		character:Destroy()
		print("Character destroyed")


		wait(game.Players.RespawnTime)

		player:LoadCharacter()
		print("Character loaded")
	end

end)
2 Likes

Where is the Server script located? if it is in the character than it will destroy with character

the other reason I think it stops working is:

  • Your flags (deathDebounce, debounce, currentFlingCount) live outside CharacterAdded, so they never reset after respawn.
  • You create a new Heartbeat connection every respawn but never disconnect the old one.
  • Killing via character:Destroy() inside the heartbeat (and then LoadCharacter()) is brittle. Just set Humanoid health to 0 and let Roblox respawn.

here is a example script

-- ServerScriptService Script
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local FLING_THRESHOLD = 85
local FRAMES_LIMIT = 30

Players.PlayerAdded:Connect(function(player)
    local hbConn

    local function onCharacterAdded(character)
        local hrp = character:WaitForChild("HumanoidRootPart")
        local hum = character:WaitForChild("Humanoid")

        local framesOver = 0
        if hbConn then hbConn:Disconnect() hbConn = nil end

        hbConn = RunService.Heartbeat:Connect(function()
            if not character.Parent then return end
            local speed = hrp.AssemblyLinearVelocity.Magnitude

            if speed > FLING_THRESHOLD then
                framesOver += 1
            else
                framesOver = 0
            end

            if framesOver >= FRAMES_LIMIT then
                if hbConn then hbConn:Disconnect() hbConn = nil end
                hum.Health = 0 -- triggers normal death/respawn
            end
        end)
    end

    player.CharacterAdded:Connect(onCharacterAdded)
    player.CharacterRemoving:Connect(function()
        if hbConn then hbConn:Disconnect() hbConn = nil end
    end)

    if player.Character then
        onCharacterAdded(player.Character)
    end
end)

1 Like

to anyone who thinks the event not being disconnected is the problem, the only real issue is performance, since the connection will just run on a nil character when it gets destroyed.

to answer your question: if you still want this to be handled server-sided, there’s two things you should know. your flags, as the person above said, is being handled outside the characteradded connections, and you handle each character the same rather than handling it per-player.

to handle this in both ways, you need to store characters in a table, and handle it from there, storing your appropriate values.

--!optimize 2
-- sorry if this isn't readable, but this is the optimal way to handle ts (this)

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local flingLimit = 30
local flingThreshold = 85 -- don't declare threshold every time a character's added.
local characters = {}

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.WalkSpeed = 90 -- testing walkspeed, since rootpart velocity is equal to walkspeed
		print("character added")
		
		if not characters[character] then
			characters[character] = {
				debounce = true,
				deathDebounce = true,
				rootPart = character:WaitForChild("HumanoidRootPart"),
				connection = nil,
				flingCount = 0,
				char = character
			}
		else
			warn("character already exists")
			characters[character].connection:Disconnect() -- disconnect so no performance issues
			characters[character].connection = nil
		end
		
		local existingChar = characters[character]
		print(existingChar)
		
		local root:BasePart = existingChar.rootPart
		local flingCount = existingChar.flingCount
		
		if not root then warn("can't find a rootpart") end
		
		existingChar.connection = RunService.Heartbeat:Connect(function()
			local rootVelo = root.AssemblyLinearVelocity.Magnitude
			print(rootVelo)
			
			if rootVelo > flingThreshold and existingChar.deathDebounce == true then
				existingChar.debounce = true
				existingChar.flingCount += 1
				print(("%s is being flung"):format(character.Name)) -- just for readability, i don't like to concat
			elseif rootVelo < flingThreshold and existingChar.debounce == true then
				existingChar.debounce = false
				print(existingChar.flingCount)
				existingChar.flingCount = 0
			end
			
			--
			
			--if existingChar.flingCount < flingLimit then
				-- remove this whole check, the logic was omitted
			if existingChar.flingCount > flingLimit and existingChar.deathDebounce == true then
				existingChar.deathDebounce = false
				print("threshold exceeded",existingChar.flingCount)
				
				existingChar.char:Destroy()
				print("character was destroyed")
				
				task.wait(Players.RespawnTime) -- no reason to be using wait(), plus you used game.Players when you got the players service already?
				
				player:LoadCharacter()
			end
		end)
	end)
end)
	
1 Like

This is a version of an all-players debounce setup..

--ServerScript in ServerScriptService
local Players = game:GetService("Players")
local rs = game:GetService("RunService")
local FlingThreshold = 85
local FlingLimit = 30
local playerData = {}

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local hrp = char:WaitForChild("HumanoidRootPart")
		playerData[player] = {count = 0, alive = true}
		task.spawn(function()
			while char.Parent and playerData[player].alive do
				local data = playerData[player] rs.Stepped:Wait()
				if hrp.AssemblyLinearVelocity.Magnitude > FlingThreshold then
					data.count += 1 --print(data.count)
				else data.count = 0 --print(data.count)
				end
				
				if data.count >= FlingLimit then
					data.alive = false --print("Fling detected")
					char:Destroy() --outcome
					break
				end
			end
		end)
	end)
end)

--player:LoadCharacter()
--player:Kick("Fling detected")

You never replied so I’m refining. Also am now at my rig, so I can test.
Easy test is FlingThreshold = 15 and run.. What is your goal here.. to kick them?
This may be your best approach as you would also want it to not be hackable.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.