Script to start mole minigame (where you wack moles) Isn't working. (Workspace Script)

Im working on a mole minigame. That gives a tool to the player and creates a box to prevent players from teleporting. Although the script isnt working at all. nothing prints at all. Also I want a better way of preventing players from interferring from the minigame. (Or atleast turn off the button for use until the minigame is over for the player.)

local Players = game:GetService("Players")

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

local tool = script.Parent.Hammer.Hammer

local function startMoleMinigame(player)
	print("Starting mole minigame for player", player.Name)

	local score = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("Score")
	local highScore = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("Highscore")
	local minigameStarted = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("MinigameStarted")

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

	minigameStarted.Value = true

	button.Visible = false

	-- spawn the box that teleports everyone away except the player
	local box = Instance.new("Part")
	box.Size = Vector3.new(10, 10, 10)
	box.Position = Vector3.new(0, 50, 0) -- adjust this position to where you want the box to spawn
	box.Anchored = true
	box.CanCollide = false
	box.Transparency = 1
	box.Parent = game.Workspace

	local function onTouched(part)
		if part:IsA("BasePart") and part.Parent:IsA("Model") and part.Parent ~= player.Character then
			if part.Parent == player.Character then
				-- teleport the player to a safe location
				part.Parent:MoveTo(Vector3.new(0, 10, 0))
			else
				-- teleport other players away
				part.Parent:MoveTo(Vector3.new(0, 1000, 0))
				part.Parent.HumanoidRootPart.CanCollide = true -- allow other players to collide with each other
			end
			box.Touched:Disconnect() -- disconnect the event to prevent further teleportations
		end
	end

	box.Touched:Connect(onTouched)

	-- give the player the tool
	local toolClone = tool:Clone()
	toolClone.Parent = player.Backpack

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

	-- update highscore if necessary
	if score.Value > highScore.Value then
		highScore.Value = score.Value
		print("Updated highscore to", highScore.Value, "for player", player.Name)
	end
end

button.Activated:Connect(function()
	local player = Players:GetPlayers()
	if player then
		startMoleMinigame(player)
	end
end)

Thank you :heart:


(Im also working on a leaderboard)

2 Likes

This is an interesting issue. I have a couple of questions about this. In your script, you wrote this in your startMoleMinigame function:

local score = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("Score")
local highScore = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("Highscore")
local minigameStarted = player:WaitForChild("Minigames"):WaitForChild("MoleHitters"):WaitForChild("MinigameStarted")

How is the player getting these values inside of their character?

Also, I am assuming that this is in a regular script, not a local script, correct?

You are passing a list of players as the parameter.
Thus, the function is highly likely infinitely waiting for child “Minigames”,
and also why nothing is printing.

2 Likes

Here is a model that I made that should hopefully work for you:

Mole Hitters

Sorry I went to sleep. I’ll check them all really quick to see which one works best

1 Like

image

1 Like

Yeah i even tried a script to insert this with the id it doesn’t work. Thanks anyways.

1 Like

So sorry. I forgot to make it available. My bad. Try it now.