enabled = true script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and enabled 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") then hit.Parent:FindFirstChild("Torso") hit.Parent.Torso.BodyForce:Destroy() end end)
Error: BodyForce is not a valid member of Part
My intentions are for the player to hit a button, lowering the gravity, then hitting it again setting it back to normal. Neither part works, the problem is in line 17, the one I recently changed. The first part worked before that
Thanks - innhale
Now usually, I make this huge fit about how code needs to be organized, but you seem to have gotten the organization aspect of asking a question down. So props to you for that. ;)
This is purely - from my perspective - a logistical error.
You should merge the two Touched events into just one function and then check to see if BodyForce exists.
local enabled = true script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.PrimaryPart:FindFirstChild("BodyForce") and enabled then hit.Parent.PrimaryPart.BodyForce:Destroy() enabled = false wait(2) enabled = true return elseif hit.Parent:FindFirstChild("Humanoid") and enabled then enabled = false local bf = Instance.new("BodyForce") bf.force = Vector3.new(0,2000,0) bf.Parent = hit.Parent.PrimaryPart wait(5) enabled = true end end)
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 -- modification 1 make sure you are not adding an extra Bdyforce 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 -- modification 2 make sure there is a body force to delete -- modification 3 make sure a body force was not just added or deleted by this script hit.Parent:FindFirstChild("Torso") -- why do you need this line? hit.Parent.Torso.BodyForce:Destroy() enabled = false wait (5) enabled = true --modification 4 add these three lines to make sure a body force isn't added as soon as it is deleted 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