applies a bodyforce to push the player or objects back
it stops working when a ball hits my foot
I am going to assume that its because of the character changing states? probably to a climbing one. I just have no idea how to make sure it doesn’t get rid of the BodyForce
you can see it works for a second, but a ball hits the character and it stops.
local forcefield = script.Parent
-- Ensure the forcefield is a Part
if not forcefield:IsA("Part") then
warn("The script must be attached to a Part instance.")
return
end
-- Create a BodyForce instance to apply the constant force
local bodyForce = Instance.new("BodyForce")
bodyForce.Parent = forcefield
-- Define the constant force vector
local constantForce = Vector3.new(0, 0, -300) -- Example force vector, adjust as needed
-- Function to apply force to objects inside the forcefield
local function applyForceToObject(object)
if object:IsA("BasePart") then
local force = bodyForce:Clone()
force.Force = constantForce
force.Parent = object
end
end
-- Connect the Touched event to apply force when an object enters the forcefield
forcefield.Touched:Connect(function(hit)
applyForceToObject(hit)
end)
-- Connect the TouchEnded event to remove force when an object leaves the forcefield
forcefield.TouchEnded:Connect(function(hit)
if hit:IsA("BasePart") then
for _, child in hit:GetChildren() do
if child:IsA("BodyForce") and child.Force == constantForce then
child:Destroy()
end
end
end
end)
Try opening your explorer to debug what is actually happening with your objects.
I also recommend filtering through hitting actual characters and adding the BodyForce directly to the root of your character’s assembly instead of adding it to whatever part the ball hits.
i mean the area itself is where it pushes the character, it pushes anything that enters it like a sort of wind. thats why the character slows down for a second, the ball just removes the BodyForce from the character for some reason. the balls contain no scripts.
I’m not sure I follow what you’re saying - the script you provided refers to a forcefield which is the script’s parent. Are you saying that isn’t referring to the balls which are shooting towards the player?
Either way, you should still try to narrow down the actual problem by checking your explorer for where the BodyForce objects are going, and you can try printing as well. Additionally, you might not know this, but Touched and TouchEnded are generally perceived as unreliable methods of hit detection, especially on the server, if that is where you are processing all of this.
That’s a little confusing because when you walk into that assigned area, it doesn’t seem like you actually have any force against your character, more so that it starts when a ball hits your character and then abruptly stops soon after.
This is likely because of TouchEnded firing off incorrectly/functioning differently than what you would expect. I would add a print inside that code block where it removes the BodyForce and check when that happens, I’m assuming it’s only supposed to fire when you leave the blue area.