Does anyone know why the scirpt just spawn a part and deletes it imadeatly and it also doesnt go in to the right orientation and postion
-- Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = script.Parent.RemoteEvent
local RunService = game:GetService("RunService")
-- Damage amount
local DAMAGE_AMOUNT = 1
-- Knockback strength
local KNOCKBACK_STRENGTH = 10 -- Very high strength for extreme knockback
-- Hitbox offset in front of the player
local HITBOX_OFFSET = 7
-- Function to handle RemoteEvent
local function onRemoteEventFired(player)
-- Create a new part (hitbox)
wait(0.5)
local newPart = Instance.new("Part")
newPart.Size = Vector3.new(2.975, 2.605, 14.158) -- Change size as needed
newPart.Transparency = 0
newPart.CanCollide = false
newPart.Parent = workspace
-- Get the player's humanoid root part
local humanoidRootPart = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return end
-- Update the part position and orientation in front of the player on each frame
local connection
connection = RunService.RenderStepped:Connect(function()
if newPart.Parent then
-- Position the part in front of the player and match orientation
newPart.CFrame = humanoidRootPart.CFrame * CFrame.new(0, 0, -HITBOX_OFFSET)
else
connection:Disconnect()
end
end)
-- Debounce table to prevent double damage
local touchedPlayers = {}
-- Function to deal damage and apply knockback
local function applyEffects(character)
local humanoid = character:FindFirstChildOfClass("Humanoid")
local rootPart = character:FindFirstChild("HumanoidRootPart")
if humanoid and rootPart then
-- Deal damage
humanoid:TakeDamage(DAMAGE_AMOUNT)
-- Apply knockback
local direction = (rootPart.Position - newPart.Position).unit
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = direction * KNOCKBACK_STRENGTH
bodyVelocity.P = 100000 -- Extremely high pressure for instant knockback
bodyVelocity.MaxForce = Vector3.new(100000, 100000, 100000) -- Ensure the force is applied
bodyVelocity.Parent = rootPart
-- Remove the BodyVelocity after a short period to stop the knockback effect
game:GetService("Debris"):AddItem(bodyVelocity, 0.1)
end
end
-- Function to handle touch events
local function onTouch(otherPart)
local character = otherPart.Parent
if character:IsA("Model") and character:FindFirstChild("Humanoid") and character ~= player.Character then
if not touchedPlayers[character] then
touchedPlayers[character] = true
-- Start a loop to continuously deal damage and apply knockback
while touchedPlayers[character] do
applyEffects(character)
wait(0.2) -- Wait 0.2 seconds before applying damage/knockback again
end
end
end
end
-- Function to handle touch end events
local function onTouchEnded(otherPart)
local character = otherPart.Parent
if touchedPlayers[character] then
touchedPlayers[character] = nil -- Remove the character from the touched players list
end
end
-- Connect the touch events
newPart.Touched:Connect(onTouch)
newPart.TouchEnded:Connect(onTouchEnded)
-- Clean up the `touchedPlayers` table when the part is destroyed
newPart.AncestryChanged:Connect(function(child, parent)
if not parent then -- Part is being removed from the game
for character in pairs(touchedPlayers) do
touchedPlayers[character] = nil -- Clear out each character
end
end
end)
-- Destroy the part after 10 seconds
game:GetService("Debris"):AddItem(newPart, 10)
end
-- Connect the function to the RemoteEvent
RemoteEvent.OnServerEvent:Connect(onRemoteEventFired)