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

How can I make it so the function only runs a set amount of times?

local bang = script.Parent.Explosion

function ontouch(part)
	
	if part.Parent.Name ~= "Bombs" or "Explosions" then
		if part.CanCollide ~= false then
	
			script.Disabled = true
			
			local neon = game.ServerStorage.NeonPart
			local plastic = game.ServerStorage.PlasticPart
			local clnNeon = neon:Clone()
			local clnPlastic = plastic:Clone()
			clnNeon.Parent = script.Parent
			clnPlastic.Parent = script.Parent
			clnNeon.Position = script.Parent.Position
			clnPlastic.Position = script.Parent.Position
			
			local normalbomb = Instance.new("Explosion")
			normalbomb.BlastPressure = 800000
			normalbomb.BlastRadius = 10
			normalbomb.Parent = script.Parent
			normalbomb.Position = script.Parent.Position
			
			bang.Pitch = (math.random(5,10)*.1)
			bang:Play()
		
			wait(.1)
			script.Disabled = false
		
		end
	end
		
end -- By this point, I want it to check how many times the function has occurred, if more than 10, break the function. Unsure if any of this is possible.

	script.Parent.Transparency = 1
	script.Parent.CanCollide = false

	local z = 0
	repeat
		local bitz = game.ServerStorage.BombBit
		local bitzCln = bitz:Clone()
		bitzCln.Parent = workspace.Bitz
		bitzCln.Position = script.Parent.Position
		z = z + 1
	until z == 4

	wait(3)

	if not script.Parent == nil then
		script.Parent:Destroy()
	end

script.Parent.Touched:Connect(ontouch)

Help would be appreciated, sorry for any inconvenience.

2 comments

0

Please describe more to what you want to do, and how you plan to do it. We cannot answer a request, and we cannot answer something that doesn't tell us what needs solving.

0

The code block from line 36 to 52,is what i want to be activated once the ball i have hits something 10 times, without this the ball bounces forever.

1 answer

local timesran = 0 -- Define a variable to use as a counter
local maxtimesran = 10 -- Define a variable to use as the counters max value (When timesran reaches this number, the script will stop)

-- For simplicity I will just make a generic example script, but the steps can be added into any existing script with ease.
repeat -- Start the loop
timesran = timesran + 1 -- Every time the loop is ran, the counters value goes up by one

--Your script here

wait() -- Wait so script doesn't freeze
if timesran >= maxtimesran then -- If the counters value is greater than or equal to the max count then break the script
break
end
until timesran >= maxtimesran

Edited for explanation.

3 comments

0

Yes, this works great, but please include descriptive answers telling why this works and how you do it, that way more people understand. Even if he understands, there are many people that will view this in the future.

0

Maybe they did not say anything because i wasn't very clear, sorry about that. I was heading off at the time.

0

Edited for explanation, hopefully it helps you.

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