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

For loops adds a amount 2 times instead of 1?

--[[
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?

2 comments

1

Just a reminder- you can do just "--" instead of "--[[ ]]" to create a comment. Obnoxious comments can seem very bad on your code.

0

Why do you need the loop then? You can just remove it.

1 answer

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.

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