Value not changing

@PerceptualReality made a lot of the detection script

I have this script, and when a part inside the NPC detects you, it goes after you and tries to kill you. But, there is also an assassination feature in a different script. The InCombat value is for checking if the player can assassinate or not. When the NPC’s CurrentTargetHumanoid is nil, the detection script inside the NPC will put the InCombat value to false. But after leaving combat with the NPC and running away, the InCombat value still stays true. How can I fix this?

Detection script:

local LookPart = script.Parent.Detector

while task.wait() do
	for _, Player in pairs(game.Players:GetPlayers()) do
		Character = Player.Character or Player.CharacterAdded:Wait()
		local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
		local Dot = (LookPart.Position - HumanoidRootPart.Position).Unit:Dot(LookPart.CFrame.LookVector)
		if Dot > 0.7 then
			local Params = RaycastParams.new()
			Params.FilterType = Enum.RaycastFilterType.Exclude
			Params:AddToFilter({LookPart,Character:GetChildren()})
			local Raycast = workspace:Raycast(LookPart.Position,-(LookPart.Position - HumanoidRootPart.Position).Unit * (LookPart.Position - HumanoidRootPart.Position).Magnitude,Params)
			if not Raycast then
				if game.Players:FindFirstChild(Character.Name) then
					local value = game.Players:FindFirstChild(Character.Name).RestrictedArea
					local value2 = game.Players:FindFirstChild(Character.Name).InCombat
					if value.Value == true then
						script.Parent.Configurations.MaximumDetectionDistance.Value = 45
						value2.Value = true
					end
				end
			end
		end
	end
end

while task.wait() do
	if script.Parent.Configurations.CurrentTargetHumanoid == nil then
		game.Players:FindFirstChild(Character.Name).InCombat.Value = false --Here
	end
end

NPC Explorer:

1 Like

Alright I know why this is happening, it is because you have two loops, so basically your first loop is not letting your second loop running as think of it like your code sees the loop, runs till the end and just restarts at the start of the loop again, so your second loop is actually never even being read.

To fix this you can look at coroutines, or you can do something like this which may not be the best way but it should work,


local LookPart = script.Parent.Detector

while task.wait() do
	for _, Player in pairs(game.Players:GetPlayers()) do
		Character = Player.Character or Player.CharacterAdded:Wait()
		local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
		local Dot = (LookPart.Position - HumanoidRootPart.Position).Unit:Dot(LookPart.CFrame.LookVector)
		if Dot > 0.7 then
			local Params = RaycastParams.new()
			Params.FilterType = Enum.RaycastFilterType.Exclude
			Params:AddToFilter({LookPart,Character:GetChildren()})
			local Raycast = workspace:Raycast(LookPart.Position,-(LookPart.Position - HumanoidRootPart.Position).Unit * (LookPart.Position - HumanoidRootPart.Position).Magnitude,Params)
			if not Raycast then
				if game.Players:FindFirstChild(Character.Name) then
					local value = game.Players:FindFirstChild(Character.Name).RestrictedArea
					local value2 = game.Players:FindFirstChild(Character.Name).InCombat
					if value.Value == true then
						script.Parent.Configurations.MaximumDetectionDistance.Value = 45
						value2.Value = true
					end
				end
			end
		end
	end
	check()
end

function check()
	if script.Parent.Configurations.CurrentTargetHumanoid == nil then
		game.Players:FindFirstChild(Character.Name).InCombat.Value = false --Here
	end
end

What I did here is since loop 1 stops loop 2 from running is have loop 2 just a function and since both loops have the same delay I figured this should work, at the end of the loop your 1st loop just calls for the function which then runs what your 2nd loop would have done.

edit: forgot to put some of the code in the block quote :sweat_smile:

1 Like

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