Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Create a function to generate the GUI
- local function createGUI()
- local gui = Instance.new("ScreenGui")
- gui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
- gui.DisplayOrder = 10
- -- Frame to hold the buttons
- local frame = Instance.new("Frame")
- frame.Size = UDim2.new(0, 200, 0, 100)
- frame.Position = UDim2.new(1, -210, 0, 10) -- Upper-right corner
- frame.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
- frame.BorderSizePixel = 2
- frame.Parent = gui
- -- Button to start the script
- local startButton = Instance.new("TextButton")
- startButton.Size = UDim2.new(0, 80, 0, 30)
- startButton.Position = UDim2.new(0.1, 0, 0.1, 0)
- startButton.Text = "Start"
- startButton.Parent = frame
- -- Button to stop the script
- local stopButton = Instance.new("TextButton")
- stopButton.Size = UDim2.new(0, 80, 0, 30)
- stopButton.Position = UDim2.new(0.1, 0, 0.5, 0)
- stopButton.Text = "Stop"
- stopButton.Parent = frame
- -- Variables
- local player = game.Players.LocalPlayer
- local replicatedStorage = game:GetService("ReplicatedStorage")
- local punchHitEvent = replicatedStorage:WaitForChild("PunchHitEvent")
- local scriptRunning = false -- Controls whether the script is active
- -- Function to find the closest player
- local function getClosestPlayer()
- local closestPlayer = nil
- local shortestDistance = math.huge
- for _, otherPlayer in pairs(game.Players:GetPlayers()) do
- if otherPlayer ~= player and otherPlayer.Character and otherPlayer.Character:FindFirstChild("HumanoidRootPart") then
- local distance = (player.Character.HumanoidRootPart.Position - otherPlayer.Character.HumanoidRootPart.Position).Magnitude
- if distance < shortestDistance then
- closestPlayer = otherPlayer
- shortestDistance = distance
- end
- end
- end
- return closestPlayer
- end
- -- Function to fire the PunchHitEvent on the closest player
- local function firePunchHitEvent()
- local closestPlayer = getClosestPlayer()
- if closestPlayer and closestPlayer.Character then
- local args = {
- [1] = closestPlayer.Character:FindFirstChild("Left Arm"),
- [2] = closestPlayer.Character.Humanoid,
- [3] = true
- }
- punchHitEvent:FireServer(unpack(args))
- end
- end
- -- Function to run the loop that fires the event continuously
- local function startScript()
- scriptRunning = true
- while scriptRunning do
- firePunchHitEvent()
- task.wait(0.001) -- Wait for 1 millisecond
- end
- end
- -- Button actions
- startButton.MouseButton1Click:Connect(function()
- if not scriptRunning then
- startScript()
- end
- end)
- stopButton.MouseButton1Click:Connect(function()
- scriptRunning = false
- end)
- end
- -- Connect the GUI creation to player character respawn
- game.Players.LocalPlayer.CharacterAdded:Connect(function()
- createGUI()
- end)
- -- Initial call to create the GUI
- createGUI()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement