Need help fixing this messy "finish" script

  • What does the code do and what are you not satisfied with? --What it does is basically finish a “Downed” character. by finish i meant like stomping, a similar mechanic found in fighting games lik criminality or da hood. – Very buggy, player sometimes does not get rewarded.
  • How (specifically) do you want to improve the code? – Just make it more functional with less bugs
    Remember this is a serverscript found inside a tool, the event is fired by a keycode (E) which is in a localscript that is also found in a tool.
Events.Finish.OnServerEvent:Connect(function()
	local plrr = game.Players:GetPlayerFromCharacter(PlayerCharacter) -- PlayerCharacter is the player stomping/finishing. (Basically just tool owner.)
	local animation = Resources.Animations.Finish
	local humanoid = PlayerCharacter:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local animator = humanoid:FindFirstChildOfClass("Animator")
		if animator then 
			local animtrack = animator:LoadAnimation(animation)
			for _, player in pairs(Players:GetPlayers()) do
				local char = player.Character -- "char" is the player dying/finished.
				if char:FindFirstChild("HumanoidRootPart") and char:FindFirstChild("Values").Downed.Value == true then
					local Distance = (char.HumanoidRootPart.Position - PlayerCharacter.PrimaryPart.Position).Magnitude -- Making sure char (player getting stomped/downed) is near the tool owner (PlayerCharacter)
					if Distance < 7.5 then
					print("found distance and doing dmg")
					humanoid.WalkSpeed = 1
					animtrack:Play()
					task.wait(1.2)
					print("found  and doing dmg")
					char:FindFirstChildOfClass("Humanoid"):TakeDamage(35) -- Once again char is the player taking damage NOT PlayerCharacter (PlayerCharacter is the owner of the tool and player who fired the event.)
					humanoid.WalkSpeed = 16
					if char:FindFirstChildOfClass("Humanoid").Health <= 0 then -- "char" player who got stomped is "dead".
						plrr.leaderstats.TIX.Value += 25 -- Owner of tool is awarded
						plrr.Stats.KillStreak.Value += 1
					    end
					end
				end
			end
		end
	end
end)
1 Like

is it necessary to change the WalkSpeed of the player every index on the for loop?
Why not just put it outside the for loop

Events.Finish.OnServerEvent:Connect(function()
	-- PlayerCharacter is the player stomping/finishing
	-- (Basically just tool owner.)
	local LocalPlayer = game.Players:GetPlayerFromCharacter(PlayerCharacter)
	local Humanoid = PlayerCharacter:FindFirstChildOfClass("Humanoid")
	local Animation = Resources.Animations.Finish

	xpcall(function()
		local Animator = Humanoid:FindFirstChildOfClass("Animator")

		if Animator then
			local AnimtionTrack = Animator:LoadAnimation(Animation)

			for _, player in pairs(Players:GetPlayers()) do
				-- Add this becuase that will also damage the Owner
				if player.UserId == LocalPlayer.UserId then
					continue
				end

				-- "Character" is the player dying/finished.
				local Character = player.Character
				local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
				local OtherHumanoid = Character:FindFirstChildOfClass("Humanoid")

				if Character:FindFirstChild("Values").Downed.Value == true then
					-- Making sure Character (player getting stomped/downed)
					-- is near the tool owner (PlayerCharacter)
					local Distance = (HumanoidRootPart.Position - PlayerCharacter.PrimaryPart.Position).Magnitude

					if Distance < 7.5 then
						Humanoid.WalkSpeed = 1
						AnimtionTrack:Play()
						task.wait(1.2)

						-- Once again Character is the player taking damage NOT PlayerCharacter
						-- (PlayerCharacter is the owner of the tool and player who fired the event.)
						OtherHumanoid:TakeDamage(35)
						Humanoid.WalkSpeed = 16

						-- "Character" player who got stomped is "dead".
						if OtherHumanoid.Health <= 0 then
							-- Owner of tool is awarded
							LocalPlayer.leaderstats.TIX.Value += 25
							LocalPlayer.Stats.KillStreak.Value += 1
						end
					end
				end
			end
		end
	end, warn)
	-- You can use other functions besides warn
	-- or make one that messages you every time this fires
	-- like discord/trello api there are more the paid one's are better
end)
1 Like

This worked flawlessly, thank you! however i have one more question is it possible to add a debounce in here(on the remote event)? because the one i use rn for it is client-sided (which is bad).

Debounce of course as long as the script is separate from every player, or if not you could just save it on a table.