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

What's wrong with my logic here? (Vector 3, CFrame addition)

Asked by
Psudar 882 Moderation Voter
5 years ago
Edited 5 years ago

I've made this loop to add some basePart positions together so that I can get these chairs equally spaced from each-other no matter how many chairs there are. The only problem is, the first clone seems to have double the amount of space than the rest (about 10 studs, when it should be 5). I'll show a picture and the code. It's probably something really simple that I'm overlooking.

for i = 1, AmountOfChairs do --AmountOfChairs = 6
    if not workspace:FindFirstChild("firstChair") then --Only fires when theres no chair yet
        firstChair = servStorage:WaitForChild("Chair"):Clone()
            firstChair:SetPrimaryPartCFrame(CFrame.new(0, 1.5, 0))
            firstChair.Name = "firstChair"
            firstChair.Parent = workspace
        firstChairPos = firstChair.PrimaryPart.Position --Saving the original position for later use 
    end

        local newChair = firstChair:Clone()
    if not workspace:FindFirstChild("clonedChair") then --if theres not a clone then do this 
            newChairPos = firstChairPos + Vector3.new(5, 0, 0)
    end
            newChairPos = newChairPos + Vector3.new(5, 0, 0)  --add to itself + 5 on xVal
            newChair:SetPrimaryPartCFrame(CFrame.new(newChairPos))
            newChair.Name = "clonedChair" --the chair in the second if-statement
            newChair.Parent = workspace
end

Also feel free to help me out with the weird if-statements, after all, it may the root of my problem.

Edit: New site format doesn't allow for pictures to be added yet, Rip. This is way easier to read with some visual feedback.

https://cdn.discordapp.com/attachments/470922111746310154/584757796570923034/Screen_Shot_2019-06-02_at_9.31.56_AM.png

1
Have you tried defining the variable `newchairpos` outside of the for loop, do this just by doing `local newChairPos`, that might fix it so you can remix the thing you had to do because otherwise it gave a nil value thing abnotaddable 920 — 5y
0
The problem was because you can't add its Value to itself, if the actual variable had no value yet. So, I created an if-statement that gives it a new value before its added to itself, by checking if it existed yet or not. Hopefully that makes sense oof. The value added to newChairPos is just the `firstChairPos + vector3.new(5, 0, 0)` Psudar 882 — 5y

1 answer

Log in to vote
1
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
5 years ago

On line 12 you add 5 to the X axis. This causes you to basicly add 10 the first time, seeing that line 12 and 14 both add 5. Remove the + Vector3.new(5,0,0) from line 12, and your problem should be fixed.

0
Big thanks Psudar 882 — 5y
Ad

Answer this question