for i = 1, #c do if c[i].Name == "SwimForce" then c[i]:Destroy()
Question below
I'm working on a swimming script for my game and so I decided to look off of others work to get an idea on how to create one. I finally came across one that worked and I was looking over the script and then I came to this For loop. I don't understand what it does at all.
The whole basis of the script is to create a BodyPosition and place it into an R15 model character. The values for the BodyPosition is already predefined and all there is to do is place it into a character; which is handled by a While loop to check if the BodyPosition is currently in the Torso of the character and if so, what it'll do while it's in the character.
Here is the full script:
waterlevel = -3 -- Water level, in terms of height repeat wait() until game.Players.LocalPlayer.Character ~= nil local char = game.Players.LocalPlayer.Character function makeswim() if char:FindFirstChild("SwimForce") == nil then local bp = Instance.new("BodyPosition") bp.Name = "SwimForce" bp.maxForce = Vector3.new(0,85000,0) bp.position = Vector3.new(0,waterlevel + 3,0) bp.Parent = char.LowerTorso end end while true do if char.LowerTorso.Position.Y < waterlevel then if char.LowerTorso:FindFirstChild("SwimForce")==nil then makeswim() end else if char.LowerTorso:FindFirstChild("SwimForce") then local c = char.LowerTorso:GetChildren() for i = 1, #c do if c[i].Name == "SwimForce" then c[i]:Destroy() end end end end wait(0.1) end
The problem that I'm having is what does the For loop do? I didn't clearly understand why there was a hashtag in place for a number, for i = 1, #c do
, and why you couldn't do, if i.Name == "SwimForce" then i:destroy
, instead of, if c[i].Name == "SwimForce" then c[i]:Destroy()
.
Lastly, how is it that I'm able to move with a BodyPosition in my character when the script above is running, but whenever I place it into my character manually I can't?
Thanks for the help!
Its going thru all the parts of the character with #c being the number of parts found, then removing teh "swimforce" object, which in this case is a BodyPosition object named "swimforce".
If you're adding it in, while running make sure it a local script. and maybe comment out line 3.