How to only allow certain things in a large and function-heavy script to happen if a bool is true?

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to create a neutral npc which will only target players when a boolvalue inside the npc is true. Right now, I have an enemy system (a free model which I’ve minimally changed) which targets anything with a humanoid in the workspace.
  2. What is the issue? Include screenshots / videos if possible!
    I’ve tried putting this into a few major parts of the script, within a looping area, with no success:
if script.Parent.Hostile == true then
--code to perform hostile action

--(Hostile is a boolValue within the npc character, alongside the script)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve searched around DevForum, but nothing is relevant. I am a very new scripter, so perhaps there is something I simply missed

Given the specificity of this problem, here is the rbxm:

hostilenpc_devforum.rbxm (56.6 KB)

Its either not finding the value, or your setting it wrong, can I see the part where you set the bool value to true?

1 Like

Maybe use

script.Parent.Hostile.Value
3 Likes

It isn’t in the file I shared. The addition is at line 3 in the code below:

while true do
	wait(0.001)
	if script.Parent.Hostile.Value == true then
		local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
		if target ~= nil then
			local sight = workspace:Raycast(script.Parent.HumanoidRootPart.Position,(target.Position-script.Parent.HumanoidRootPart.Position).Unit*configuration.sightDistance.Value,params)
			script.Parent.Humanoid:MoveTo(target.Position + Vector3.new(rand:NextNumber(configuration.moveOffset2.Value.X,configuration.moveOffset.Value.X),0,rand:NextNumber(configuration.moveOffset2.Value.Z,configuration.moveOffset.Value.Z)), target)
			if sight ~= nil and sight.Instance == target then
				if tool.currentAmmo.Value > 0 and tool.currentMagazines.Value > 0 and canFire and not reloading then
					fireAnimTrack = holderHumanoid:LoadAnimation(tool.fireAnim)
					fireAnimTrack.Priority = Enum.AnimationPriority.Action
					fireAnimTrack.Looped = false
					fireAnimTrack:Play(tool.fireAnim.fadeTime.Value,tool.fireAnim.weight.Value,tool.fireAnim.animSpeed.Value)
					for i = 1, configuration.projectileProperties.bulletCount.Value do
						local spreadX = rand:NextNumber(configuration.projectileProperties.spreadMin.Value.X, configuration.projectileProperties.spreadMax.Value.X)
						local spreadY = rand:NextNumber(configuration.projectileProperties.spreadMin.Value.Y, configuration.projectileProperties.spreadMax.Value.Y)
						caster:Fire(startPos.Position, (target.Position - startPos.Position + Vector3.new(spreadX/configuration.projectileProperties.spreadDivisior.Value, spreadY/configuration.projectileProperties.spreadDivisior.Value, 0)).Unit,configuration.projectileProperties.velocity.Value, castBehavior)
						whiz:Play()
					end
					local fireClone = tool:WaitForChild("Handle"):WaitForChild("fireSound"):Clone()
					fireClone.Name = "fireClone"
					fireClone.Parent = tool:WaitForChild("Handle")
					fireClone:Play()
					local casing = Instance.new("Part", workspace)
					casing.CFrame = tool.casingPart.CFrame * CFrame.fromEulerAnglesXYZ(1.5,0,0)
					casing.Size = configuration.casingProperties.Size.Value
					casing.Anchored = false
					casing.CanCollide = configuration.casingProperties.CanCollide.Value
					casing.Shape = Enum.PartType.Cylinder
					casing.Name = "bulletCasing"
					if configuration.casingProperties.useColor3.Value == true then
						casing.Color = configuration.casingProperties.Color3.Value
					else
						casing.BrickColor = configuration.casingProperties.BrickColor.Value
					end
					casing.Velocity = tool.casingPart.CFrame.LookVector * configuration.casingProperties.velocity.Value + Vector3.new(rand:NextNumber(configuration.casingProperties.forceMin.Value.X, configuration.casingProperties.forceMax.Value.X),rand:NextNumber(configuration.casingProperties.forceMin.Value.Y, configuration.casingProperties.forceMax.Value.Y), rand:NextNumber(configuration.casingProperties.forceMin.Value.Z,configuration.casingProperties.forceMax.Value.Z))
					casing.RotVelocity = configuration.casingProperties.rotVelocity.Value
					if configuration.casingProperties.casingEnabled then
						game.Debris:AddItem(casing, configuration.casingProperties.lifeTime.Value)
					end
					casing.Touched:Connect(function(hitPart)
						if not hitPart:IsDescendantOf(holderHumanoid.Parent) then
							local casingSound = configuration.casingProperties.casingSound:Clone()
							casingSound.Parent = casing
							casingSound:Play()
						end
					end)
					tool.Parent.Humanoid.WalkSpeed = 1
					tool.firePart.muzzleLight.Enabled = true
					task.delay(0.1,function()
						tool.firePart.muzzleLight.Enabled = false
					end)
					for i,v in pairs(tool.firePart:GetDescendants()) do
						if v.Name == "muzzleEffect" then
							v.Enabled = true
							task.delay(0.1,function()
								v.Enabled = false
							end)
						end
					end
					canFire = false
					game.Debris:AddItem(fireClone, fireClone.TimeLength)
					tool.currentAmmo.Value = tool.currentAmmo.Value - 1
					if configuration.boltAction.Value == true then
						task.spawn(function()
							task.wait(configuration.fireRate.Value)
							tool:WaitForChild("Handle"):WaitForChild("boltSound"):Play()
							local boltAnimTrack = holderHumanoid:LoadAnimation(tool.boltAnim)
							boltAnimTrack.Looped = false
							boltAnimTrack:Play(tool.boltAnim.fadeTime.Value,tool.boltAnim.weight.Value,tool.boltAnim.animSpeed.Value)
						end)
					end
					task.delay(fireAnimTrack.Length, function()
						fireAnimTrack:Stop()
					end)
					task.wait(configuration.fireRate.Value)
					canFire = true
					tool.Parent.Humanoid.WalkSpeed = 16
				elseif tool.currentAmmo.Value <= 0 and tool.currentMagazines.Value > 0 and not reloading then
					reload()
				end
			end
		end
	end
end

Good catch, but it didn’t seem to work for me

(This is the model with the change, if it helps)

hostilenpc_devforum.rbxm (56.6 KB)