Hello, so I’m making a Piggy fangame, but there’s a bug in the script
Here’s the video.
https://drive.google.com/u/2/uc?id=1t7KYD6d75W8XjpRU0fPo6mvSPPMdVjl3&export=download
Also here’s the script
local myHuman = script.Parent:WaitForChild("Piggy")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local myHead = script.Parent:WaitForChild("Head")
local tweenService = game:GetService("TweenService")
local _
_G.CanAttack = true
local damage = 0
function findTarget()
local target
local dist = 50
for i,v in pairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local rootPart = v:FindFirstChild("HumanoidRootPart")
if human and rootPart and v ~= script.Parent then
if (myRoot.Position - rootPart.Position).magnitude <= dist and human.Health > 0 then
dist = (myRoot.Position - rootPart.Position).magnitude
target = rootPart
end
end
end
return target
end
function checkSight(target)
local ray = Ray.new(myHead.Position,(target.Position - myHead.Position).Unit * 200)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
if hit then
if hit:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then
if math.abs(hit.Position.Y - myRoot.Position.Y) < 2 then
return true
else
return false
end
else
return false
end
else
return false
end
end
local doorDebounce = true
myHuman.Touched:Connect(function(obj)
if obj.Name == "DoorFrame" and doorDebounce == true then
doorDebounce = false
local opened = obj.Parent:FindFirstChild("Opened")
local hinge = obj.Parent:FindFirstChild("Hinge")
local hingeOpened = obj.Parent:FindFirstChild("HingeOpened")
local doorLock = obj:FindFirstChild("DoorLock")
local doorOpen = obj:FindFirstChild("DoorOpen")
if opened and hinge and hingeOpened and doorLock and doorOpen then
if opened.Value == false then
doorLock:Play()
opened.Value = true
myHuman.WalkSpeed = 0
obj.CanCollide = false
wait(.25)
doorOpen:Play()
tweenService:Create(hinge,TweenInfo.new(.35,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{CFrame = hingeOpened.CFrame}):Play()
wait(.45)
coroutine.resume(coroutine.create(function()
wait(2)
obj.CanCollide = true
end))
myHuman.WalkSpeed = 18
end
end
end
doorDebounce = true
end)
while wait() do
local target = findTarget()
if target then
local sight = checkSight(target)
if sight == true then
repeat wait()
myHuman:MoveTo(target.Position)
if script.Parent.HumanoidRootPart.Chase.IsPlaying == false and _G.CanAttack == true then
script.Parent.HumanoidRootPart.Chase:Play()
script.Parent.HumanoidRootPart.Spotted:Play()
game:GetService("ReplicatedStorage"):WaitForChild("botStatus").Value = "Chasing"
end
until checkSight(target) == false
elseif sight == false then
game:GetService("ReplicatedStorage"):WaitForChild("botStatus").Value = "Patrolling"
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position,target.Position)
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(path:GetWaypoints()) do
myHuman:MoveTo(v.Position)
--script.Parent.HumanoidRootPart.Footstep:Play()
if v.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
if checkSight(target) == true then
print("Chasing")
break
end
script.Parent.PrimaryPart:SetNetworkOwner(nil)
local timeOut = myHuman.MoveToFinished:Wait()
if not timeOut then
print("Patrolling")
break
end
end
else
local door
local dist = 200
for i,v in pairs(workspace:GetDescendants()) do
local doorFrame = v:FindFirstChild("DoorFrame")
if doorFrame then
local doorClose = doorFrame:FindFirstChild("DoorClose")
local opened = v:FindFirstChild("Opened")
if opened and doorClose then
if (myRoot.Position - doorFrame.Position).magnitude <= dist then
dist = (myRoot.Position - doorFrame.Position).magnitude
door = doorFrame
end
end
end
end
if door then
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position,door.Position)
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(path:GetWaypoints()) do
myHuman:MoveTo(v.Position)
if v.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
if checkSight(target) == true then
break
end
local timeOut = myHuman.MoveToFinished:Wait()
if not timeOut then
print("huh4")
break
end
end
end
end
end
end
end
wait()
end
local debounce = false
local Gamemode = game.ReplicatedStorage.Mode
game.ReplicatedStorage.Stun.OnServerEvent:Connect(function(plr,target)
game.ReplicatedStorage.Announcement:FireAllClients("Piggy is gone for 20 seconds")
if target then
if target:FindFirstAncestorOfClass("Model") then
-- Check to see if clicked model is a real player
local pig = game.Players:GetPlayerFromCharacter(target:FindFirstAncestorOfClass("Model"))
if pig then
if pig:FindFirstChild("MemoryBot") then -- Check to see if it is actually Piggy or just another contestant
if not debounce then
local stunTag = Instance.new("BoolValue") -- Add stun tag so that we can check in other scripts such as Piggy Bat script whether Piggy is stunned or not
stunTag.Name = "IsStunned"
stunTag.Parent = pig
if pig.Character:FindFirstChild("Torso") then
-- Add confusion sparkles to show Piggy is stunned
local Sparkles = Instance.new("Sparkles")
Sparkles.Parent = pig.Character:FindFirstChild("Head")
Sparkles.SparkleColor = Color3.fromRGB(255,255,255)
end
-- Announce to all players in GUI that Piggy is stunned
if Gamemode.Value == "Traitor" then
pig.Character.Humanoid.Health = 0
end
-- Enable debounce so stun effect cannot be applied again for 20 seconds
-- At the end of the debounce, remove sparkles
if pig.Character then
if pig.Character:FindFirstChild("Torso"):FindFirstChild("Sparkles") then
pig.Character:FindFirstChild("Torso"):FindFirstChild("Sparkles"):Destroy()
end
end
-- Delete stunned tag so we can tell from other scripts that the
-- piggy is no longer stunned.
end
end
end
end
end
end)
function getHumanoid(model)
for _, v in pairs(model:GetChildren()) do
if v:IsA'Humanoid' then
return v
end
end
end
local ai = script.Parent
local human = getHumanoid(ai)
local hroot = ai.HumanoidRootPart
local pfs = game:GetService("PathfindingService")
function GetPlayerNames()
local players = game:GetService('Players'):GetChildren()
local name = nil
for _, v in pairs(players) do
if v:IsA'Player' then
name = tostring(v.Name)
end
end
return name
end
for _, zambieparts in pairs(ai:GetChildren()) do
if zambieparts:IsA'Part' and zambieparts.Name == "Torso" then
zambieparts.Touched:connect(function(p)
if p.Parent.Name == GetPlayerNames() and p.Parent.Name ~= ai.Name then -- damage
local enemy = p.Parent
local enemyhuman = getHumanoid(enemy)
enemyhuman:TakeDamage(damage)
end
end)
end
end
Here’s the explorer:
I would be very thankful if someone will find a way to fix this. Since I tried fixing for a few hours and it didn’t work.