Secure Knockback and Stun Combat System for Roblox
This prompt generates an advanced combat system for Roblox where valid hits apply physical knockback and temporary stun to the opponent. The solution prioritizes server authority, preventing clients from deciding damage, target, distance, push force, or effect duration.
Ideal for fighting games, PvP arenas, action RPGs, and melee weapon experiences. The prompt asks for your Explorer context before generating the code, allowing it to adapt RemoteEvents, folder names, R6/R15 rigs, animations, and existing combat rules.
The expected result is a complete, commented Luau Script ready to paste into ServerScriptService, including validation raycast, spam protection, safe concurrent state control, movement restoration, and practical testing instructions in Roblox Studio.
Act as a senior Roblox Luau developer, specialized in secure, high-performance, exploit-resistant multiplayer combat systems. Create a complete knockback and stun system for hitting an opponent, implemented primarily on the server so that no client can freely choose target, damage, force, duration, or the outcome of a hit. Before writing the code, analyze and use my game context below. If any essential information is missing, ask at most 5 objective questions before generating the solution. Do not invent Explorer paths, RemoteEvent names, or custom APIs if I have provided alternatives. Context to be filled in by me: - Relevant Explorer structure: [PASTE HERE] - Existing RemoteEvents/RemoteFunctions and purpose: [PASTE HERE] - How the attack is initiated today (Tool, click, key, custom system, etc.): [PASTE HERE] - Name/location of the weapon, hitbox, or attack origin: [PASTE HERE] - Rigs used (R6, R15, or both): [PASTE HERE] - Maximum attack range: [VALUE OR LEAVE FOR YOU TO DEFINE] - Cooldown between attacks: [VALUE] - Attack damage, if applicable: [VALUE OR NONE] - Base stun duration: [VALUE] - Horizontal force and vertical lift of the knockback: [VALUES] - Special rules, teams, NPCs, shields, blocking, immunity, or PvP: [PASTE HERE] Deliver as the main output exactly 1 complete server Script in Luau, ready to paste, unless my context clearly requires integration into existing files. The file type must be Script, and the default location must be ServerScriptService. The Script should connect to the attack intent RemoteEvent indicated in the context, preferably located in ReplicatedStorage. If the RemoteEvent does not exist, explicitly explain, before the code, which RemoteEvent to create, its name, type, and exact path in the Explorer. Do not generate a LocalScript as the main solution: the client may only request an attack attempt, while the server determines the real hit. Implement the logic with these technical requirements: 1. When receiving the attack request, validate on the server the Player, Character, Humanoid, HumanoidRootPart, life state, cooldown, allowed tools/equipment, and any received data. Treat every argument coming from the client as untrusted. 2. Never accept directly from the client a target Instance, impact position, damage, stun duration, or knockback vector/force. The server must identify the target using raycast or another server-authoritative method compatible with the provided context. 3. Use RaycastParams with correct filters to ignore the attacker’s character, the weapon when necessary, and elements defined in the context. Validate maximum distance, line of sight, valid Humanoid, HumanoidRootPart, and that attacker and victim are not the same character. Include team/PvP validation if applicable. 4. Apply modern, stable knockback to the victim’s HumanoidRootPart, preferably with AssemblyLinearVelocity or a calculated impulse when more appropriate. The push must use the horizontal direction from the attacker to the victim, with a safe fallback for degenerate vectors, and allow a configurable vertical component. 5. Create a robust stun by attribute, for example Stunned, with server-side time control. During stun, block movement and jumping while preserving and restoring previous WalkSpeed, JumpPower, JumpHeight, and AutoRotate values only when safe. Handle consecutive hits: a new stun must renew or extend the end time without letting old timers restore movement early. Do not use infinite loops or imprecise waits to control critical states. 6. Protect against spam using a per-UserId cooldown table, cleanup when PlayerRemoving occurs, and defensive handling for removed characters, deaths, and respawns. Do not cause memory leaks through unnecessary persistent connections or tables. 7. Add configurable constants at the start of the Script for range, cooldown, duration, knockback speed/force, lift, optional damage, and relevant names/paths. Clearly comment the points I should adjust. 8. If my system already has damage or hit detection on the server, integrate with the appropriate event/point without duplicating damage. If the context does not have a server-side detection method, implement server-authoritative raycast as a fallback and document its limitations. Provide a short explanation of the architecture and assumptions before the code. Then deliver the full code, with no pseudocode, no omitted sections, and no ellipses, inside a single markdown block identified exactly as ```lua. Write useful comments in the code in Brazilian Portuguese. Do not use obsolete APIs, do not use BodyVelocity, do not use loadstring, and do not give the client authority over combat. After the code block, include numbered, objective instructions for setting up the required objects in the Explorer and testing in Roblox Studio using Start Server with at least 2 Players. Also explain how to verify stun, knockback, cooldown, raycast, and exploit attempts via the client. If any decision depends on the context I left blank, clearly state the assumption adopted and concentrate those assumptions in the configurable constants.