Hey! I found an old script for a Whack-A-Mole game here on the DevForum and wanted to make a game! I need to use an existing meshpart but don’t know how to change the script so I can. (You can’t add TextureId/MeshId with a MeshPart or SpecialMesh)
position = position or self.StartPos
parent = parent or workspace
local model = Instance.new('Part')
model.Name = 'mole'
model.Size = Vector3.new(2,5,2)
model.CFrame = CFrame.new(0,-90,0) * CFrame.fromEulerAnglesXYZ(0, math.rad(-90), 0)
model.Anchored = true
model.Position = position
model.Parent = parent
local molemesh = Instance.new('SpecialMesh', model)
molemesh.MeshId = 'rbxassetid://12117229493'
molemesh.TextureId = 'rbxassetid://12117501922'
molemesh.Scale = Vector3.new(2,2.5,2.5)
local clickd = Instance.new('ClickDetector',model)
clickd.MaxActivationDistance = 100
self.Object = model --self is MoleClass
self.StartPos = position
end
function MoleClass:Infect(bool:boolean) --true/false value
if self.Object == nil then warn('spawn mole first') return end
if bool == nil then bool = true end
if bool then --change the color of the mole if infected
self.Object.BrickColor = BrickColor.new('Medium stone grey')
else
self.Object.BrickColor = BrickColor.new('Medium stone grey')
end
self.Infected = bool
end
function MoleClass:Peek(bool:boolean)
if self.ClickEvent ~= nil then self.ClickEvent:Disconnect() end
if bool == nil then bool = true end
local clickdt:ClickDetector = self.Object:FindFirstChildOfClass('ClickDetector')
local moveto = self.StartPos
local pos=Vector3.new(0,1,0)
if bool then
--go up and allow it to be clicked
moveto+= pos
local event
event=clickdt.MouseClick:Connect(function(player)
event:Disconnect() --alow only one click
hitevent:Fire(player,self.Infected)
end)
self.ClickEvent = event
else
local pos2 = Vector3.new(0,3,0)
moveto-= pos2
end
--create and play movement tween
ts:Create(self.Object,info,{Position=moveto}):Play()
end
--damage the players if they click a non-infected mole
hitevent.Event:Connect(function(player,IsInfected)
--this event fires when the mole is clicked
if player.Character == nil then return end
local hum = player.Character:FindFirstChild('Humanoid')
if IsInfected then
hum.Health += 10
print(player.Name,'hit an infected mole! +10 HP')
else
hum:TakeDamage(50)
print(player.Name,'hit the wrong mole! -50HP')
end
end)
--and thats it for the moles!
--//Game logic//
local holes = {}
for i=1,10 do --theres 10 holes
local holepos = folder:FindFirstChild('hole'..i).Position
holes[i] = MoleClass.new() --add a mole to each hole
holes[i]:Spawn(holepos,folder)
end
local function togglepeek(bool:boolean) --toggle peek for all
for i=1,10 do
holes[i]:Peek(bool)
end
end
local function shufflepick() --shuffle sequence of numbers, the indexes of holes{}
local options = {1,2,3,4,5,6,7,8,9,10}
for i = #options, 2, -1 do
local j = math.random(i)
options[i], options[j] = options[j], options[i]
end
return options
end
local function randomizemoles(max:number)
max=max or 10 --max number of moles to trigger
local picked = shufflepick()
local infectedmole = holes[picked[1]] --pick a mole to infect
infectedmole:Infect()
for i=1,max do
local j = picked[i]
holes[j]:Peek(true)
end
task.wait(1)
togglepeek(false) --has these task waits for aesthetics lol
task.wait(1)
infectedmole:Infect(false)
end
task.wait(5)
togglepeek(false) --also for aesthetics, or it starts immediately
task.wait(1)
while task.wait() do --loop that runs it all
randomizemoles(math.random(1,1))
end```