Function firing but doesn't run the code in it

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hello i want to create a tycoon with merging similar to this game Zombie Merge Tycoon - Roblox and i wrote the script below that merges three avatars and if there’s like 5 it will merge 3 and keep the 2 old avatars it’s a normal script (not local script) and is located inside of a part in the workspace

  2. What is the issue? Include screenshots / videos if possible!
    The problem is that the script works perfectly but the code inside the function MultipleOfThree (that let the script know how many avatars he needs to destroy) doesn’t work like if it was ignored

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to replace the repeat loop with a while loop but didn’t work and tried to move the function inside the .Touched event

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

--Avatars
local NormalAvatar = 0

local BlueAvatar = 0

--Script
local Button = script.Parent

local debounce = false

local Positions = game.Workspace.Tycoons.Tycoon1.Positions:GetChildren()

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TweenService = game:GetService("TweenService")

local function MultipleOfThree(Avatar)  --Makes a numbre a multiple of three
	if Avatar > 3 and Avatar % 3 ~= 0 then
		repeat Avatar -= 1 until Avatar % 3 == 0
	end
end

Button.Touched:Connect(function(hit)
	if hit.Parent:WaitForChild("Humanoid") and debounce == false then
		debounce = true
		
		for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
			if v.Name == "NormalAvatar" then
				NormalAvatar += 1
			elseif v.Name == "BlueAvatar" then
				BlueAvatar += 1
			end
		end
		
		MultipleOfThree(NormalAvatar)
		MultipleOfThree(BlueAvatar)
		
		print(NormalAvatar)
		print(BlueAvatar)
		
		if NormalAvatar >= 3 then
			for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
				if NormalAvatar == 0 then continue end
				
				if v.Name == "NormalAvatar" then
					v:Destroy()
					
					NormalAvatar -= 1

					if i % 3 == 0 then
						local BlueAvatar = ReplicatedStorage.Avatars.BlueAvatar:Clone()
						BlueAvatar.Parent = game.Workspace.Tycoons.Tycoon1.Avatars
						
						for i ,v in pairs(BlueAvatar:GetDescendants()) do
							if v:IsA("Part") or v:IsA("MeshPart") then
								if v.Name ~= "HumanoidRootPart" then
									v.Transparency = 1

									local Tween = TweenService:Create(v,TweenInfo.new(.5,Enum.EasingStyle.Linear,Enum.EasingDirection.In),{Transparency = 0 })
									Tween:Play()
								end
							end
						end
						
					end
					
				end	
			end	
		elseif BlueAvatar >= 3 then
			for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
				if BlueAvatar == 0 then continue end

				if v.Name == "BlueAvatar" then
					v:Destroy()

					BlueAvatar -= 1

					if i % 3 == 0 then
						local FireAvatar = ReplicatedStorage.Avatars.FireAvatar:Clone()
						FireAvatar.Parent = game.Workspace.Tycoons.Tycoon1.Avatars

						for i ,v in pairs(FireAvatar:GetDescendants()) do
							if v:IsA("Part") or v:IsA("MeshPart") then
								if v.Name ~= "HumanoidRootPart" then
									v.Transparency = 1

									local Tween = TweenService:Create(v,TweenInfo.new(.5,Enum.EasingStyle.Linear,Enum.EasingDirection.In),{Transparency = 0 })
									Tween:Play()
								end
							end
						end

					end

				end	
			end
			
		end
		
		for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
			v.HumanoidRootPart.CFrame = Positions[i].CFrame
		end
		
		NormalAvatar = 0
		BlueAvatar = 0
		
		wait(1)
		debounce = false
	end	
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

I dont know should it be like this, but inside .Touched function there is “MultipleDeTrios” instead of “MultipleOfThree”

It’s because i change the name of the function when i wrote the post it should be MultipleOfThree

1 Like

Tell me, how is it getting ignored? Like, what it does that it shouldn’t do

What the function should do is to substract the ammount of avatars until it’s a multiple of three but the problem is that it doesn’t do anything to that number just like if it was ignored (btw the code in the function works but when i put it in the function it doesn’t work)

1 Like

Try:

local function MultipleOfThree(Avatar)  --Makes a numbre a multiple of three
	if Avatar > 3 and Avatar % 3 ~= 0 then
		repeat Avatar -= 1 until Avatar % 3 == 0
	end
        return Avatar
end

and

NormalAvatar = MultipleOfThree(NormalAvatar)
BlueAvatar = MultipleOfThree(BlueAvatar)

Adding return to function, because if not it, changing Normal and Blue Avatar variables won’t work, it will return nil

Thanks :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.