A bug that i tried fixing for a few hours but it didn't

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:
fix

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.

Could you explain what the bug is? Everything in the video seemed fine to me.

As said above, please explain what the expected behavior is and what exactly is happening that you consider a bug. Then we can try to help you

When as she spots you, the field of view increases, heartbeat plays, and blood on the screen, but it is supposed to go back to normal when she loses you it goes back to normal too early.

1 Like

I don’t see what part of the script does that. Could you send the parts that handle that?

1 Like

The scare player script (That’s what i call)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")


local shouldScarePlayer = ReplicatedStorage:WaitForChild("shouldScarePlayer")
local StartPlayerScare = ReplicatedStorage:WaitForChild("StartPlayerScare")
local StopPlayerScare = ReplicatedStorage:WaitForChild("StopPlayerScare")
local HeartBeat = script:WaitForChild("HeartBeat")
local Camera = workspace.CurrentCamera


local StartTween = TweenService:Create(Camera, TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 90})
local StopTween = TweenService:Create(Camera, TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70})
local ShowBlood = TweenService:Create(script.Parent:WaitForChild("BloodScreen").Blood, TweenInfo.new(0.6), {ImageTransparency = 0.6})
local HideBlood = TweenService:Create(script.Parent:WaitForChild("BloodScreen").Blood, TweenInfo.new(0.6), {ImageTransparency = 1})

StartPlayerScare.OnClientEvent:Connect(function()
		ShowBlood:Play()
		StartTween:Play()
		HeartBeat:Play() 
end)


StopPlayerScare.OnClientEvent:Connect(function()
		HideBlood:Play()
		StopTween:Play()
		HeartBeat:Stop()
end)

The status when it should do increasing the FOV and other things

game.ReplicatedStorage.botStatus.Changed:Connect(function()
if game.ReplicatedStorage.botStatus.Value == "Chasing" then
	game.ReplicatedStorage.StartPlayerScare:FireAllClients()
end

if game.ReplicatedStorage.botStatus.Value == "Patrolling" then
	game.ReplicatedStorage.StopPlayerScare:FireAllClients()
	end
	

end)

The spotted 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

I guess that’s it?

1 Like

In this part, it’s checking if the sound is playing before setting the value to “Chasing”. I don’t see the sound being stopped when it’s status is changed back “Patrolling”.

Try stopping the sounds when the bot goes to the “Patrolling” state and see if that fixes it.

I believe in the video the bot loses sight of you for a second, and calls it to stop playing the effect. However, when it gets a visual on you again, the sound is still playing, failing the check and not recalling the effect to play.

1 Like