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

For loops adds a amount 2 times instead of 1?

Asked by
hokyboy 270 Moderation Voter
4 years ago
--[[
Varibles
]]
local NPC = workspace:WaitForChild("Leader")
local NPCPosFolder = workspace:WaitForChild("NPCPositions")
local CurrentPos = 1
local NextPos = NPCPosFolder["Pos"..CurrentPos].Position
local Repstorage = game:GetService("ReplicatedStorage")
local Status = Repstorage:WaitForChild("Status")


--[[
StoryLine
]]

local Dialog  = {
    "Campleader : Heya fokes , Welcome to camp greenlake",
    "If you follow me i'll show you around"
}

local function FirstPlayerLoaded()

end






--[[
function for letting a npc walk
]]




--[[
Main StoryLineScript
]]

local function PlayStoryLine()

    Status.Value = Dialog[1]
    wait(3)
    Status.Value = Dialog[2]

    for NpcWalk = 1,2 do
        NPC.Humanoid:MoveTo(NextPos)
        NPC.Humanoid.MoveToFinished:wait()
        CurrentPos = CurrentPos + 1
        print(CurrentPos)
    end

end


PlayStoryLine()

So bassicly i have this for loop to make my script not so long and hard to manage but it adds current pos twice so it will end up being 3 and it does not move to the second location is has how do i fix this?

1
Just a reminder- you can do just "--" instead of "--[[ ]]" to create a comment. Obnoxious comments can seem very bad on your code. Sebgamingkid 147 — 4y
0
Why do you need the loop then? You can just remove it. imKirda 4491 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Try this:

--[[
Varibles
]]
local NPC = workspace:WaitForChild("Leader")
local NPCPosFolder = workspace:WaitForChild("NPCPositions")
local CurrentPos = 1
local NextPos = NPCPosFolder["Pos"..CurrentPos].Position
local Repstorage = game:GetService("ReplicatedStorage")
local Status = Repstorage:WaitForChild("Status")


--[[
StoryLine
]]

local Dialog  = {
    "Campleader : Heya fokes , Welcome to camp greenlake",
    "If you follow me i'll show you around"
}

local function FirstPlayerLoaded()

end






--[[
function for letting an npc walk
]]




--[[
Main StoryLineScript
]]

local function PlayStoryLine()

    Status.Value = Dialog[1]
    wait(3)
    Status.Value = Dialog[2]
   NpcWalk = 2 -- How Many Times To Loop
    repeat
        NPC.Humanoid:MoveTo(NextPos)
        NPC.Humanoid.MoveToFinished:Wait()
        CurrentPos = CurrentPos + 1
        print(CurrentPos)
    until CurrentPos >= NpcWalk

end


PlayStoryLine()

I think the problem was that you were looping it too fast and it didn't wait right.

Ad

Answer this question