Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can someone please look over this script and tell me where I when wrong?

So bacicaly, this script is suppost to find specific blocks called "Force" and give them a BodyFoce. The Body force is suppost to push the block one way and then wait 2 seconds and then push the other way and repeat.

local Model = script.Parent
while --Repeating script?
for _,v in pairs(Model:GetChilren()) do
    if v.Name == "Force" then --Finding all parts called Force
       local Movement = Instance.new ("BodyForce") --Adding BodyForce
       Movement.Force = (100,0,0) --Pushing the block one way
wait (2) --waiting 2 seconds
		Movement.Force = (-100,0,0)--Pushing the block another way
wait (2)

    end
end

This script is going to be used to add force to the end points of a cape to pull the majority of the cape in a direction to simulate wind. I'm trying to add a looping script to points in the cape instead of trying to "animate" it with slightly changing the points position every second. The Verlet Integration method is to complex for me to understand. I'm really just a builder who knows 1% of scripting knowledge.

1 answer

✓ Accepted answer

I've noticed a few problems with your script. The first is that the syntax for a while loop is while <condition> do, where <condition> is the condition for the loop to run. For an infinite loop, we use true as the condition. I also noticed that you have not ended your while loop.

The second problem is that you have misspelled GetChildren.

The third is that you never specify the parent for your BodyForce, meaning it won't do anything. Conveniently, Instance.new allows you to specify a second argument for the parent:

local Movement = Instance.new ("BodyForce", v)

Another problem is that your script will do the effect for each part in turn rather than at once, which may prove to be problematic. You can get around this using the spawn function to run the code on each part simultaneously:

    if v.Name == "Force" then --Finding all parts called Force
       spawn(function()
           local Movement = Instance.new ("BodyForce", v) --Adding BodyForce
          Movement.Force = (100,0,0) --Pushing the block one way
          wait (2) --waiting 2 seconds
          Movement.Force = (-100,0,0)--Pushing the block another way
          wait (2)
       end)
    end

I also noticed that you create a new BodyForce each time the script loops. You'll find that they begin to interfere with each other, so the best way to fix this is to destroy it after each loop:

          local Movement = Instance.new ("BodyForce") --Adding BodyForce
          Movement.Force = (100,0,0) --Pushing the block one way
          wait (2) --waiting 2 seconds
          Movement.Force = (-100,0,0)--Pushing the block another way
          wait (2)
          Movement:Destroy()

The full script is here:

local Model = script.Parent
while true do --Repeating script?
	for _,v in pairs(Model:GetChildren()) do
		if v.Name == "Force" then --Finding all parts called Force
			spawn(function()
				local Movement = Instance.new ("BodyForce", v) --Adding BodyForce
				Movement.Force = (100,0,0) --Pushing the block one way
				wait (2) --waiting 2 seconds
				Movement.Force = (-100,0,0)--Pushing the block another way
				wait (2)
				Movement:Destroy()
			end)
		end
	end
end

Also, something that will make your life much easier while scripting is the Output window. This will allow you to see if your script has errored, where the problem is and what the problem is. It also allows you to see anything that is printed. Hopefully this information will help you.

1 comment

0

Thanks alot!

This discussion is preserved as it was. New answers, comments, and votes are closed.