Script to start mole minigame (where you wack moles) Isn’t working. (Workspace Script) (I need help. I cant fix this)

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:

Please respond.

Is button a TextButton or something else?

Yeah its a text button named start

TextButton.Activated is not used for that if it exists. Use TextButton.MouseButton1Click:Connect(function()

If it worked dont forget to mark as solution

Pretty sure it is.
It’s a thing used for both ios and pc to activate it


And for some reason that worked. Although there is more around it


I found a solution for it but I don’t know how to get the local player? See that Starting mole minigame for player nil

Actually, its because you are getting a table of all the players, not one player.

GetPlayers() or GetChildren() returns a table

I only know how to get the player here. I still want the mousebuttononeclick function.

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

how to implement it here?

If you are looking for the player, player IS the player, not a table, which is giving nil

im sorry i have to go ill try to return soon

What do you mean? Like what is player here


YE YES YES YES

THIS IS THE FIX

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 = script.Parent.Hammer.Hammer

button.MouseButton1Click:Connect(function()
	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)

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