A problem I see is that the second portion of your script will run whenever a Humanoid hits the part. Because the only requirements for
hit.Parent.Torso.BodyForce:Destroy()
Are that the player that hit it has a torso, it will run about 10 or more times each time the character steps on it.
(Test this by putting print ("hit") instead of hit.Parent.Torso.BodyForce:Destroy() in line 17 and it will print many times when stepped on)
this means that every time a character steps on the part, the script will try to delete 10+ BodyForces-- more than there are! This explains your error.
A fix for the error would be to check if there is a body force to delete before actually deleting it. You will notice that your error is fixed, but wait! Your pad still doesn't work properly! Now it may seem like the first part of the script isn't working! This is because as soon as the body force is added by the first part of your script the second notices the body force and instantly deletes it. To fix this, you could have the first script stop the second from working for say, five seconds right when the body force is added. It looks like you have a variable for the job already in your script: enabled. Just tell the second part of your script to not run when enabled is false.
Now when you step on your pad it should work again. But this script is still not perfect. Some minor adjustments still need to be made.
Finished script:
enabled = true
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and enabled and not hit.Parent.Torso:FindFirstChild("BodyForce") then
enabled = false
local BodyForce = Instance.new("BodyForce", hit.Parent.Torso)
BodyForce.force = Vector3.new (0,2000,0)
wait(5)
enabled = true
end
end)
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Torso") and hit.Parent.Torso:FindFirstChild("BodyForce") and enabled then
hit.Parent:FindFirstChild("Torso")
hit.Parent.Torso.BodyForce:Destroy()
enabled = false
wait (5)
enabled = true
end
end)
This will probably work. if it doesn't, don't downvote me just tell me and I will try and fix it. Please ignore my terrible indentation, just learning to create lua codeblocks on here :D
The other answer is better than mine I'm a new developer
2 comments
You didn't define Torso on line 17. Make it hit.Parent.TorsoBodyForce:Destroy() . Also the parent argument to Instance.new is deprecated.
I tried it. I gave me a new error I posted above.