This function won't ever get called. No matter what I do

Im trying to call the function (SpawnMole()) but it wont ever call! It literally will not!!

local Players = game:GetService("Players")

local player = nil


game.Players.PlayerAdded:Connect(function(plr)

	player = plr

end)

local button = script.Parent.Leaderboard.Leaderboard.Leaderboard.SurfaceGui.Frame.Start


local tool = game.ServerStorage.Hammer


wait(3)

local score = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("Score")

local highScore = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("Highscore")
local gui = player:WaitForChild("PlayerGui"):WaitForChild("Mole"):WaitForChild("Frame")
local mole = player:WaitForChild("PlayerGui"):WaitForChild("Mole")

local timer = gui:WaitForChild("Timer")

local scoreGui = gui:WaitForChild("Score")

local minigameStarted = script.MinigameStarted
local toolrn = nil


button.MouseButton1Click:Connect(function()
	print("Starting mole minigame for player", player.Name)

	if minigameStarted.Value == true then
		print("Minigame already started for player", player.Name)
		return
	end

	minigameStarted.Value = true

	button.Visible = false
	mole.Enabled = true


	-- give the player the tool
	local toolClone = tool:Clone()
	toolClone.Parent = player.Backpack
	toolrn = player.Backpack:WaitForChild("Hammer")

	-- reset score to 0
	score.Value = 0
	print("Reset score to 0 for player", player.Name)
end)

-- start the gameplay






scoreGui.Text = "Score: " .. score.Value

local function updateScore()
	if minigameStarted.Value == true then
		score.Value = score.Value + 1
		scoreGui.Text = "Score: " .. score.Value
	end
end

local timeLeft = 60

local function updateTime()
	if minigameStarted.Value == true then
		timeLeft = timeLeft - 1
		timer.Text = "Time left: " .. timeLeft
		if timeLeft == 0 then
			toolrn:Destroy()
			mole.Enabled = false
			minigameStarted.Value = false
			button.Visible = true
			timeLeft = 60
			timer.Text = "Time left: " .. timeLeft
			if score.Value > highScore.Value then
				highScore.Value = score.Value
				print("Updated highscore to", highScore.Value, "for player", player.Name)
			end
		end
	end
end


while timeLeft > 0 do

	updateTime()
	wait(1)
end
local holeUp = script.Parent.TpMole -- Replace with actual path to holes

local holeDown = script.Parent.TpMole -- Replace with actual path to holes

local moleRegular = script.Parent.Moles.Mole

local moleGold = script.Parent.Moles.GoldMole

local moleDiamond = script.Parent.Moles.DiamondMole

local moleAngry = script.Parent.Moles.AngryMole

local moleBomb = script.Parent.Moles.BombMole

local moleTypes = {moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleGold, moleGold, moleGold, moleAngry, moleAngry, moleBomb, moleDiamond} -- adjust moleTypes to have fewer rare mole types

local TweenService = game:GetService("TweenService")

local function spawnMoles()

	if minigameStarted.Value == true then

		local holes = {1, 2, 3, 4, 5, 6, 7, 8}

		local numHoles = math.random(1, 5) -- change to a random number between 2 and 3

		for i = 1, numHoles do

			local index = math.random(1, #holes)

			local hole = holes[index]

			table.remove(holes, index)

			local moleType = moleTypes[math.random(1, #moleTypes)]

			local mole = moleType:Clone()
			local moleModel = mole:FindFirstChildOfClass("Model")
			mole.PrimaryPart = mole.Primary

			mole.Parent = workspace

			mole:MoveTo(holeDown[hole].Up.Position)
			mole:WaitForChild("PrimaryPart").Touched:Connect(function(part)
				if part and part.Parent and part.Parent:FindFirstChild("Humanoid") and part.Name == "Hammer" then
					if moleModel.Name == "Mole" then
						score.Value = score.Value + 1
					elseif moleModel.Name == "GoldMole" then
						score.Value = score.Value + 3
					elseif moleModel.Name == "DiamondMole" then
						score.Value = score.Value + 5
					elseif moleModel.Name == "AngryMole" then
						score.Value = score.Value - 2
					elseif moleModel.Name == "BombMole" then
						score.Value = 0
					end
					scoreGui.Text = "Score: " .. score.Value
					moleModel.PrimaryPart:Destroy()
				end
			end)
			end
		end
	end
while true do
	print("poo")
	spawnMoles()
	print("YAY")
	wait(6)
	print("okkkkk")
end

Please help :heart:

How do i fix it? I did try almost every other way

Whoa cool! Is that private or something? I want to try it since mine is very ungood

Well uhh

image
It doesn’t even let me click the button anymore :skull:

This while loop is infinite thus preventing the following code from ever executing. You have two options: either split the code in two scripts or use coroutines.

Example:

white true do
    print("while loop")

    task.wait(1)
end

print("Hello World!") --won't ever run
coroutine.wrap(function()
    while true do
        print("while loop")
        
        wait(1)
     end
end)() --call the coroutine function

print("Hello World") -- will execute because the coroutine runs in a separate thread.

Yeah I run it with Python and APIs its hard to setup but if i ever make it an app somehow il let you know.

I did try using corotune before. Although this looks promising

Trust me your gonna forget by the time its out :skull:

1 Like

“while loop” didn’t print. Doesn’t work. Like I said I already tried this (kinda)

based off the infinite loop thing this could work to stop it.

local function spawnMoles()
    -- Function implementation goes here
end

while true do
    spawnMoles()
    wait(6)
end

image
I literally tried the exact same thing :skull:

:rofl: I think its the infinite loop that’s what my Human brain said first

Well I’ll test it anyways since im desperate. Maybe theres a stray atom in my script that wont let it work? :thinking:

Well what now? I encountered an unfixable script. What do I do now?

1 Like

I am sure that you can not interact with surface guis server sided you need a local script and the surface gui is starter gui.

:skull: Bro that is nowhere near the problem im at

(btw. YOU CAN!!!)

image
Good try. although no moles have spawned :sob:

also the score is nil

Oh sorry i thought it was the problem mb

Nah its fine bro!!111!1111!!!1

My last attempt lol. Try This

local Players = game:GetService("Players")

local player = nil

game.Players.PlayerAdded:Connect(function(plr)
    player = plr
end)

local button = script.Parent.Leaderboard.Leaderboard.Leaderboard.SurfaceGui.Frame.Start
local tool = game.ServerStorage.Hammer

wait(3)

local score = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("Score")
local highScore = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("Highscore")
local gui = player:WaitForChild("PlayerGui"):WaitForChild("Mole"):WaitForChild("Frame")
local mole = player:WaitForChild("PlayerGui"):WaitForChild("Mole")
local timer = gui:WaitForChild("Timer")
local scoreGui = gui:WaitForChild("Score")

local minigameStarted = script.MinigameStarted
local toolrn = nil

button.MouseButton1Click:Connect(function()
    print("Starting mole minigame for player", player.Name)

    if minigameStarted.Value == true then
        print("Minigame already started for player", player.Name)
        return
    end

    minigameStarted.Value = true

    button.Visible = false
    mole.Enabled = true

    -- give the player the tool
    local toolClone = tool:Clone()
    toolClone.Parent = player.Backpack
    toolrn = player.Backpack:WaitForChild("Hammer")

    -- reset score to 0
    score.Value = 0
    print("Reset score to 0 for player", player.Name)
end)

-- start the gameplay
scoreGui.Text = "Score: " .. score.Value

local function updateScore()
    if minigameStarted.Value == true then
        score.Value = score.Value + 1
        scoreGui.Text = "Score: " .. score.Value
    end
end

local timeLeft = 60

local function updateTime()
    if minigameStarted.Value == true then
        timeLeft = timeLeft - 1
        timer.Text = "Time left: " .. timeLeft
        if timeLeft == 0 then
            toolrn:Destroy()
            mole.Enabled = false
            minigameStarted.Value = false
            button.Visible = true
            timeLeft = 60
            timer.Text = "Time left: " .. timeLeft
            if score.Value > highScore.Value then
                highScore.Value = score.Value
                print("Updated highscore to", highScore.Value, "for player", player.Name)
            end
        end
    end
end

while timeLeft > 0 do
    updateTime()
    wait(1)
end

local holeUp = script.Parent.TpMole -- Replace with actual path to holes
local holeDown = script.Parent.TpMole -- Replace with actual path to holes
local moleRegular = script.Parent.Moles.Mole
local moleGold = script.Parent.Moles.GoldMole
local moleDiamond = script.Parent.Moles.DiamondMole
local moleAngry = script.Parent.Moles.AngryMole
local moleBomb = script.Parent.Moles.BombMole
local moleTypes = {moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleRegular, moleGold, moleGold, moleGold, moleAngry, moleAngry, moleBomb, moleDiamond}

local TweenService = game:GetService("TweenService")

local function spawnMoles()
    if minigameStarted.Value == true then
        local holes = {1, 2, 3, 4, 5, 6, 7, 8}
        local numHoles = math.random(1, 5)

        for i = 1, numHoles do
            local index = math.random(1, #holes)
            local hole = holes[index]

            table.remove(holes, index)

            local moleType = moleTypes[math.random(1, #moleTypes)]
            local mole = moleType:Clone()
            local moleModel = mole:FindFirstChildOfClass("Model")
            mole.PrimaryPart = mole.Primary

            mole.Parent = workspace

            mole:MoveTo(holeDown[hole].Up.Position)
            mole:WaitForChild("PrimaryPart").Touched:Connect(function(part)
                if part and part.Parent and part.Parent:FindFirstChild("Humanoid") and part.Name == "Hammer" then
                    if moleModel.Name == "Mole" then
                        score.Value = score.Value + 1
                    elseif moleModel.Name == "GoldMole" then
                        score.Value = score.Value + 3
                    elseif moleModel.Name == "DiamondMole" then
                        score.Value = score.Value + 5
                    elseif moleModel.Name == "AngryMole" then
                        score.Value = score.Value - 2
                    elseif moleModel.Name == "BombMole" then
                        score.Value = 0
                    end
                    scoreGui.Text = "Score: " .. score.Value
                    moleModel.PrimaryPart:Destroy()
                end
            end)
        end
    end
end

while true do
    spawnMoles()
    wait(6)
end

Make sure to replace the placeholder paths for holeUp and holeDown with the actual paths to the corresponding objects in your game.

No it literally is this
image
Theres holes from one to 8. inside them has up and down. So??? I cant??

btw doesn’t work :sob:

Maybe do a table system and store the locations I can see what my Ai gives if you want.