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

How to call a FOR LOOP to go up by one, ONLY when called?

Hello! Ive been practicing scripting a bit more and im trying to set a cap of an IntValue so that once you complete putting all the logs on the campfire you cant glitch out the system and keep the number going up. Now, this is similar to a FOR loop where you can go up by a set integer and set a starting value and an end value. But I'm pretty sure you cant do this in a FOR loop and am wondering how I would do this but I can call when to proceed.

Script: I'm trying to set the variable "campfireCount" to start at 0 and stop at 3 and going up 1 each time a player puts a log in the fire.

Important script:

	if humanoid and campGaurd == false then
		
		for campfireCounts = 0, 3, 1 do
			campfireCount.Value = campfireCount.Value + 1
		end
		
		if playerLogCount.Value >= 1 then
			playerLogCount.Value = playerLogCount.Value - 1
			campfireCount.Value = campfireCount.Value + 1
			campfirelog1.Transparency = 0
			campfirelog1.CanCollide = true
			print("log gave")
		end
		
		if campfireCount.value == 2 then
			campfirelog2.Transparency = 0
			campfirelog2.CanCollide = true
			
		elseif campfireCount.value == 3 then
			campfirelog3.Transparency = 0
			campfirelog3.CanCollide = true
		else 
			print("all logs placed")
		end

Full script:

--Is there player on me

local campfire = script.Parent
local campGaurd = false
local campfires = campfire.Parent
local campfirelog1 = campfires:FindFirstChild("Log1")
local campfirelog2 = campfires:FindFirstChild("Log2")
local campfirelog3 = campfires:FindFirstChild("Log3")





local function onTouch(partTouch)
	local character = partTouch.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	local playerStats = player:FindFirstChild("leaderstats")
	local playerLogCount = playerStats:FindFirstChild ("Has Log")
	local campfireCount = playerStats:FindFirstChild ("CampFireLogs")
	if humanoid and campGaurd == false then
		
		for campfireCounts = 0, 3, 1 do
			campfireCount.Value = campfireCount.Value + 1
		end
		
		if playerLogCount.Value >= 1 then
			playerLogCount.Value = playerLogCount.Value - 1
			campfireCount.Value = campfireCount.Value + 1
			campfirelog1.Transparency = 0
			campfirelog1.CanCollide = true
			print("log gave")
		end
		
		if campfireCount.value == 2 then
			campfirelog2.Transparency = 0
			campfirelog2.CanCollide = true
			
		elseif campfireCount.value == 3 then
			campfirelog3.Transparency = 0
			campfirelog3.CanCollide = true
		else 
			print("all logs placed")
		end
		
		
		
		campGaurd = true
		wait(5)
		campGaurd = false
	end
end

campfire.Touched:Connect(onTouch)

4 comments

0

I am not sure, but I think you can do the for i = 1,1 so that the loop executes only once. You can then put it into the event or function.

0

That is a horrible idea.

0

It has worked for me when I tried to rotate an object inside a function.

0

Why didn't you just call what was in your loop then? What is the reason to wrap something in a for loop that won't be looped? It's just a stupid idea.

2 answers

If you want a variable to go up by one (or any other amount in that matter) each time a function is called, you do not need a loop. To force maximum value of three you can do a simple check. Now I am not 100% sure what you are trying to achieve, but I assume you do not want to carry on other operations in the function, should the amount of logs is already at 3.

Replace:

for campfireCounts = 0, 3, 1 do
	campfireCount.Value = campfireCount.Value + 1
end

With:

if campfireCount.Value == 3 then return end
campfireCount.Value = campfireCount.Value + 1

On the other hand, if you want the function to continue past that point you can do it like this:

campfireCount.Value = campfireCount.Value + 1
if campfireCount.Value > 3 then 
	campfireCount.Value = 3
end

Hope this helps, have a nice scripting session.

So you've got the right idea, you just don't understand that an event won't be looped through with new input. It's more like a function being called than a thread that gets instantiated. So you need to write your code with that in mind:

local campfire = script.Parent
local campGaurd = false
local campfires = campfire.Parent
local logs = {} --a lot easier than copy/paste
for i,v in pairs(campfires:GetChildren()) do
	if string.sub(v.Name,0,3) == "Log" then
		logs[#logs+1] = v
	end
end

local db = false

local function onTouch(partTouch)
	if db then --the debounce
		return
	end
	local character = partTouch.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	local playerStats = player:FindFirstChild("leaderstats")
	local playerLogCount = playerStats:FindFirstChild ("Has Log")
	local campfireCount = playerStats:FindFirstChild ("CampFireLogs")
    if humanoid and campGaurd == false then
		db = true
		if campfireCount.Value == 3 then
			logs["Log"..campfireCount.Value].Transparency = 0
			logs["Log"..campfireCount.Value].CanCollide = true
			--places the last log
			print("all logs placed")
			campGaurd = true
        	wait(5)
        	campGaurd = false
			--just going to assume after this you want the logs to disappear as the fire has burned out, if not, just get rid of whats below this comment
			for i,v in pairs(logs) do
				v.Transparency = 1
				v.CanCollide = false
				campfireCount.Value = 0
			end
			print("fire extinguished")
		else
			--else place logs
			logs["Log"..campfireCount.Value].Transparency = 0
			logs["Log"..campfireCount.Value].CanCollide = true
			campfireCount.Value = campfireCount.Value + 1
			print("log gave")
		end
		wait(.5)
		db = false
    end
end

campfire.Touched:Connect(onTouch)

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