Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local player = Players.LocalPlayer
- local character = player.Character or player.CharacterAdded:Wait()
- local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
- local humanoid = character:WaitForChild("Humanoid")
- local debounce = false
- local forcefieldRange = 20 -- 10 studs range for the forcefield
- local baseForceMagnitude = 200 -- Force to push player away from danger
- local debounceTime = 0.1 -- Time delay to prevent rapid dodging
- -- Function to apply a force that pushes the player away from dangerous objects
- local function applyForcefield(dangerousObject)
- local direction = (humanoidRootPart.Position - dangerousObject.Position).Unit
- local forceMagnitude = baseForceMagnitude
- -- Apply force via BodyVelocity
- local bodyVelocity = Instance.new("BodyVelocity")
- bodyVelocity.Velocity = direction * forceMagnitude
- bodyVelocity.MaxForce = Vector3.new(100000, 0, 100000) -- Only X and Z axis force
- bodyVelocity.P = 1000
- bodyVelocity.Parent = humanoidRootPart
- -- Remove the velocity effect after a short time
- game:GetService("Debris"):AddItem(bodyVelocity, 0.15)
- end
- -- Function to continuously detect and dodge dangerous objects
- local function detectAndDodge()
- if debounce then return end
- debounce = true
- -- Look for dangerous parts in the workspace
- for _, obj in pairs(workspace:GetDescendants()) do
- if obj:IsA("BasePart") and (obj.Name:lower():find("precast") or obj.Name:lower():find("hitbox")) then
- local distance = (humanoidRootPart.Position - obj.Position).Magnitude
- -- If within range, apply the dodge effect
- if distance <= forcefieldRange then
- applyForcefield(obj)
- break -- Exit after dodging one object
- end
- end
- end
- debounce = false
- task.wait(debounceTime)
- end
- -- Function to reconnect the dodge mechanism when the player respawns
- local function onCharacterAdded(newCharacter)
- character = newCharacter
- humanoidRootPart = character:WaitForChild("HumanoidRootPart")
- humanoid = character:WaitForChild("Humanoid")
- -- Start detecting and dodging again after respawn
- while true do
- detectAndDodge()
- task.wait(0.05) -- Frequent checks for dangerous objects
- end
- end
- -- Initial setup for the current character
- while true do
- detectAndDodge() -- Start detecting and dodging dangerous objects
- task.wait(0.05) -- Frequent checks
- end
- -- Reapply the dodge logic when the player respawns
- player.CharacterAdded:Connect(onCharacterAdded)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement