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 = 10 -- 10 studs range for the forcefield
- local forceMagnitude = 50 -- The amount of force applied to push the player away
- -- Function to apply a forcefield effect
- local function applyForcefield(object)
- -- Calculate direction away from the object, ignoring the Y axis (for horizontal movement)
- local horizontalDirection = (humanoidRootPart.Position - object.Position)
- horizontalDirection = Vector3.new(horizontalDirection.X, 0, horizontalDirection.Z).Unit -- Flatten Y-axis movement
- local force = horizontalDirection * forceMagnitude -- Apply force in the horizontal direction
- humanoidRootPart.Velocity = humanoidRootPart.Velocity + force -- Adjust the player's velocity
- end
- -- Function to check for dangerous objects within forcefield range
- local function checkForcefield()
- if debounce then return end
- debounce = true
- local dangerousObjects = {}
- -- Recursively find all dangerous objects in the workspace
- local function findDangerousObjects(parent)
- for _, child in pairs(parent:GetChildren()) do
- if child:IsA("BasePart") and (child.Name == "precast" or child.Name == "hitbox") then
- table.insert(dangerousObjects, child)
- end
- findDangerousObjects(child) -- Recursive call to check child objects
- end
- end
- findDangerousObjects(workspace)
- -- Check proximity to each dangerous object and apply the forcefield if within range
- for _, obj in pairs(dangerousObjects) do
- if obj and obj:IsA("BasePart") then
- local distance = (humanoidRootPart.Position - obj.Position).Magnitude
- -- If within the 10-stud forcefield range, apply the forcefield effect
- if distance <= forcefieldRange then
- applyForcefield(obj)
- break -- Exit after applying forcefield to one object
- end
- end
- end
- debounce = false
- task.wait() -- Pause to prevent spamming the function
- end
- -- Initial setup for the current character
- while true do
- checkForcefield()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement