Further enhance this script, and add more ANTI slap features: ` local Players =…
- User
Further enhance this script, and add more ANTI slap features:
code - Assistant
Further enhance this script, and add more ANTI slap features:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local VirtualInputManager = game:GetService("VirtualInputManager")
local LightsaberRemotes = ReplicatedStorage:WaitForChild("LightsaberRemotes")
local BlockRemote = LightsaberRemotes:WaitForChild("Block")
local UnblockRemote = LightsaberRemotes:WaitForChild("Unblock")
-- Settings
local blockDistance = 20 -- Maximum distance to initiate a block
local autoCounterEnabled = true -- Enable automatic counter-slapping
local autoSlapDistance = 10 -- Distance to perform automatic slap counter
local reactionTimeThreshold = 0.15 -- Threshold for reaction time in seconds
local blockDuration = 0.3 -- Duration to hold the block
-- Slap Animations
local slapAnimations = {
["Slap"] = 12625891544,
["SlappedLegs"] = 12671715182,
["SlappedArms"] = 12625896358
}
local lastBlockTime = 0
local isBlocking = false
local function getClosestPlayer()
local closestPlayer = nil
local shortestDistance = math.huge
local localPlayer = Players.LocalPlayer
local localCharacter = localPlayer.Character
if localCharacter and localCharacter:FindFirstChild("HumanoidRootPart") then
local localPosition = localCharacter.HumanoidRootPart.Position
for _, player in ipairs(Players:GetPlayers()) do
if player ~= localPlayer and player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local playerPosition = player.Character.HumanoidRootPart.Position
local distance = (playerPosition - localPosition).Magnitude
if distance < shortestDistance then
closestPlayer = player
shortestDistance = distance
end
end
end
end
return closestPlayer, shortestDistance
end
local function handleSlapAnimation(opponent, animTrack)
local localPlayer = Players.LocalPlayer
local character = localPlayer.Character
if not character then return end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return end
local opponentRootPart = opponent.Character and opponent.Character:FindFirstChild("HumanoidRootPart")
if not opponentRootPart then return end
local distance = (humanoidRootPart.Position - opponentRootPart.Position).Magnitude
if distance <= blockDistance and not isBlocking then
local currentTime = tick()
local timeSinceLastBlock = currentTime - lastBlockTime
if timeSinceLastBlock >= reactionTimeThreshold then
isBlocking = true
BlockRemote:FireServer()
-- Set block duration
task.delay(blockDuration, function()
if isBlocking then
UnblockRemote:FireServer()
isBlocking = false
end
end)
lastBlockTime = currentTime
-- Automatic counter-slap
if autoCounterEnabled and distance <= autoSlapDistance then
task.delay(blockDuration * 0.5, function()
VirtualInputManager:SendKeyEvent(true, Enum.KeyCode.E, false, game)
task.wait(0.1)
VirtualInputManager:SendKeyEvent(false, Enum.KeyCode.E, false, game)
end)
end
end
end
end
local function onOpponentAnimationPlayed(opponent, animTrack)
for animName, animId in pairs(slapAnimations) do
if animTrack.Animation.AnimationId == "rbxassetid://" .. animId then
handleSlapAnimation(opponent, animTrack)
break
end
end
end
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local opponent = Players:GetPlayerFromCharacter(character)
if not opponent or opponent == Players.LocalPlayer then return end
humanoid.AnimationPlayed:Connect(function(animTrack)
onOpponentAnimationPlayed(opponent, animTrack)
end)
end
for _, player in ipairs(Players:GetPlayers()) do
if player.Character then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(onCharacterAdded)
end)
RunService.Heartbeat:Connect(function()
local closestPlayer, distance = getClosestPlayer()
if closestPlayer and distance <= blockDistance then
-- You can add additional logic here if needed
end
end)
Certainly! Enhancing your anti-slap script involves adding more robust features to detect and respond to slaps effectively. Below is an improved version of your script with the following enhancements:
Here’s the enhanced script:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local VirtualInputManager = game:GetService("VirtualInputManager")
local TweenService = game:GetService("TweenService")
local StarterGui = game:GetService("StarterGui")
local LightsaberRemotes = ReplicatedStorage:WaitForChild("LightsaberRemotes")
local BlockRemote = LightsaberRemotes:WaitForChild("Block")
local UnblockRemote = LightsaberRemotes:WaitForChild("Unblock")
-- Settings
local Settings = {
BlockDistance = 20, -- Maximum distance to initiate a block
AutoCounterEnabled = true, -- Enable automatic counter-slapping
AutoSlapDistance = 10, -- Distance to perform automatic slap counter
ReactionTimeThreshold = 0.15, -- Threshold for reaction time in seconds
BlockDuration = 0.3, -- Duration to hold the block
Cooldown = 1.0, -- Cooldown time after blocking
SlapAnimations = { -- Expanded slap animations
["Slap"] = 12625891544,
["SlappedLegs"] = 12671715182,
["SlappedArms"] = 12625896358,
["SlappedHead"] = 12625897000, -- Example additional animation
["DoubleSlap"] = 12625897500 -- Another example
}
}
local lastBlockTime = 0
local isBlocking = false
-- Create a ScreenGui for visual feedback
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "AntiSlapIndicator"
screenGui.Parent = Players.LocalPlayer:WaitForChild("PlayerGui")
local blockIndicator = Instance.new("Frame")
blockIndicator.Size = UDim2.new(0, 100, 0, 50)
blockIndicator.Position = UDim2.new(0.5, -50, 0, 10)
blockIndicator.BackgroundColor3 = Color3.new(1, 0, 0)
blockIndicator.Visible = false
blockIndicator.Parent = screenGui
local blockText = Instance.new("TextLabel")
blockText.Size = UDim2.new(1, 0, 1, 0)
blockText.BackgroundTransparency = 1
blockText.Text = "Blocking"
blockText.TextColor3 = Color3.new(1, 1, 1)
blockText.TextScaled = true
blockText.Parent = blockIndicator
-- Optional: Add a tween for smooth appearance/disappearance
local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local showTween = TweenService:Create(blockIndicator, tweenInfo, {BackgroundColor3 = Color3.new(1, 0, 0), Visible = true})
local hideTween = TweenService:Create(blockIndicator, tweenInfo, {BackgroundColor3 = Color3.new(1, 0, 0), Visible = false})
local function showBlockIndicator()
if not blockIndicator.Visible then
blockIndicator.Visible = true
showTween:Play()
end
end
local function hideBlockIndicator()
if blockIndicator.Visible then
hideTween:Play()
task.delay(tweenInfo.Time, function()
blockIndicator.Visible = false
end)
end
end
local function getClosestPlayers()
local players = {}
local localPlayer = Players.LocalPlayer
local localCharacter = localPlayer.Character
if localCharacter and localCharacter:FindFirstChild("HumanoidRootPart") then
local localPosition = localCharacter.HumanoidRootPart.Position
for _, player in ipairs(Players:GetPlayers()) do
if player ~= localPlayer and player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local playerPosition = player.Character.HumanoidRootPart.Position
local distance = (playerPosition - localPosition).Magnitude
if distance <= Settings.BlockDistance then
table.insert(players, {Player = player, Distance = distance})
end
end
end
end
table.sort(players, function(a, b) return a.Distance < b.Distance end)
return players
end
local function handleSlapAnimation(opponent, animTrack)
local localPlayer = Players.LocalPlayer
local character = localPlayer.Character
if not character then return end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return end
local opponentRootPart = opponent.Character and opponent.Character:FindFirstChild("HumanoidRootPart")
if not opponentRootPart then return end
local distance = (humanoidRootPart.Position - opponentRootPart.Position).Magnitude
if distance <= Settings.BlockDistance and not isBlocking then
local currentTime = tick()
local timeSinceLastBlock = currentTime - lastBlockTime
if timeSinceLastBlock >= Settings.Cooldown then
isBlocking = true
lastBlockTime = currentTime
BlockRemote:FireServer()
-- Show visual indicator
showBlockIndicator()
print("Blocking initiated against:", opponent.Name)
-- Set block duration
task.delay(Settings.BlockDuration, function()
if isBlocking then
UnblockRemote:FireServer()
isBlocking = false
hideBlockIndicator()
print("Blocking ended.")
end
end)
-- Automatic counter-slap
if Settings.AutoCounterEnabled and distance <= Settings.AutoSlapDistance then
task.delay(Settings.BlockDuration * 0.5, function()
VirtualInputManager:SendKeyEvent(true, Enum.KeyCode.E, false, game)
task.wait(0.1)
VirtualInputManager:SendKeyEvent(false, Enum.KeyCode.E, false, game)
print("Automatic counter-slap performed.")
end)
end
else
print("Block on cooldown. Time left:", Settings.Cooldown - timeSinceLastBlock)
end
end
end
local function onOpponentAnimationPlayed(opponent, animTrack)
for animName, animId in pairs(Settings.SlapAnimations) do
if animTrack.Animation and animTrack.Animation.AnimationId == "rbxassetid://" .. animId then
print("Detected slap animation:", animName, "from:", opponent.Name)
handleSlapAnimation(opponent, animTrack)
break
end
end
end
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local opponent = Players:GetPlayerFromCharacter(character)
if not opponent or opponent == Players.LocalPlayer then return end
humanoid.AnimationPlayed:Connect(function(animTrack)
onOpponentAnimationPlayed(opponent, animTrack)
end)
end
local function setupPlayer(player)
if player.Character then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
-- Initialize for existing players
for _, player in ipairs(Players:GetPlayers()) do
setupPlayer(player)
end
-- Handle new players joining
Players.PlayerAdded:Connect(setupPlayer)
-- Optional: Continuously monitor closest players (can be used for additional logic)
RunService.Heartbeat:Connect(function()
local closestPlayers = getClosestPlayers()
-- You can add additional logic here if needed
-- Example: Prioritize blocking the closest player
end)
-- Optional: Display cooldown timer (requires additional GUI setup)
-- This section can be expanded based on your needs
Expanded Slap Animation Detection:
Settings.SlapAnimations table to recognize additional slap animations, making the script more robust against varied slap actions.Visual Feedback:
ScreenGui with a Frame and TextLabel to visually indicate when blocking is active. This helps players be aware of their blocking status.TweenService for smooth transitions when showing and hiding the block indicator.Cooldown Mechanism:
Settings.Cooldown parameter to prevent the block mechanism from being spammed. This ensures that after blocking once, there is a short period before the next block can be initiated.Multiple Opponent Handling:
getClosestPlayers function now returns a sorted list of all players within the BlockDistance, allowing for future extensions where you might prioritize or handle multiple slaps differently.Configurable Settings:
Settings table for easier management and potential future expansions.Debugging and Logging:
print statements throughout the script to provide real-time feedback in the output. This is invaluable for debugging and understanding the script’s behavior during gameplay.Optimized Reaction Handling:
Prioritizing Closest Opponent: While the current script handles blocking based on proximity, you can extend it to prioritize blocking the closest opponent or the one who is actively slapping.
Enhanced GUI: Consider adding more detailed GUI elements, such as cooldown timers or notifications when a slap is detected but not blocked due to cooldown.
Customization Options: Allow users to customize settings through a GUI for a more personalized experience.
Performance Optimization: If you encounter performance issues with many players, consider optimizing the Heartbeat connection or the way you handle player lists.
Security Considerations: Ensure that blocking and counter-slapping actions are securely handled on the server-side to prevent exploitations.
This enhanced script should provide a more reliable and user-friendly anti-slap mechanism in your Roblox game. Feel free to adjust the settings and expand upon the features to best suit your specific needs!
Not yet included in a published release.