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
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.
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.