Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Not anchoring past bodyposition's Y?

Asked by
Fatul 9
7 years ago
Edited 7 years ago

Not getting an error so i dunno what to do.

It's currently anchoring EVERY brick the very second 'e' is pressed, while I want the first one to reach past its bodyposition's position.Y to anchor and each brick subsequently. Problem probably lies in misuse of iterations.(which is beyond my knowledge)

EDIT: I solved it, but if anyone has a similar issue and wants a method, using a courotine helped a lot.

repeat wait(.1) until script.Parent.Name ~= "StarterCharacterScripts"
player = game.Players:FindFirstChild(script.Parent.Name)
mouse = player:GetMouse() 
--E = Telekineses
--R = Pyromancy
--T = Shape

center = player.Character.Torso
objects = game.Workspace:GetChildren()
--
power = 1000
--
current = 0
total = 1+(power/10)
--
floating = false

mouse.KeyDown:connect(function(key)
    if key == "e" then
        for i = 1, #objects do
            if objects[i].ClassName == "Part" then
                if objects[i].Anchored == false then
                    local magnitude = (center.Position - objects[i].Position).magnitude
                    local range = 1+(power/2)
                    if magnitude < range then
                        current = current+1
                        if current < total then
                            local bodypos = Instance.new("BodyPosition")
                            bodypos.Name = "Force"
                            bodypos.MaxForce = Vector3.new(0,25000,0)
                            bodypos.P = 5000
                            bodypos.Position = Vector3.new(objects[i].Position.X, objects[i].Position.Y+15, objects[i].Position.Z)
                            bodypos.D = 1000
                            bodypos.Parent = objects[i]
                            floating = true
                            if (objects[i].Force.Position.Y-1 / objects[i].Position.Y) >= 1 then
                                objects[i+1].Anchored = true
                            end
                        end
                    end 
                end
            end
        end
    end
end)

0
That first line is unnecessary. Scripts don't run inside of StarterCharacterScripts. On the second like you can write game.Players:GetPlayerFromCharacter(script.Parent) instead, or just not define the player since you only ever use it to access the character which you're already using to get the player. 1waffle1 2908 — 7y

Answer this question