"If" Statement not being read

  1. What do you want to achieve? I’m making a movement system for Chica in my FNAF game. I want it so when the game starts she moves off the stage into the dining room

  2. What is the issue? To get her to move off the stage into the dining room, I use an “if” statement checking where her HumanoidRootPart’s position is. If she is on the stage, she moves to the dining room. But for whatever reason, it doesn’t work even when the statement is obviously true.

  3. What solutions have you tried so far? I tried fixing it for hours. Too many to list.

At the beginning of the game, her HumanoidRootPart is at cam1A, but it still doesn’t work. I don’t understand why and it’s very frustrating. I would appreciate it if someone could help me. (Note: this is just a sample to demonstrate my code but I determined that this is the problem because I tested everything else to make sure it works)

local moveableCameras = {
   cam1A = Vector3.new(0.7, 6.92, -65.9)
}

if HRP.Position == moveableCameras.cam1A then
	print("works")
end
1 Like

It’s very unlikely that the HRP’s position will exactly match that position. Any physics acting on the HRP, for example, will move it away, and even if it’s off by the tiniest amount this will not match. You might want to perform a magnitude check instead of a direct equality between Vector3s, alternatively Vector3 has FuzzyEq for this purpose aswell, you can for fuzzy equality instead of direct equality.

I’m pretty sure that’s why your if statement isn’t passing, but perhaps there’s more to the situation.

4 Likes

I use the same system for Bonnie in my game and it works fine.

2 Likes

I’ll look at the FuzzyEq thing, thanks for the suggestion

3 Likes

Actually, the HumanoidRootPart is anchored and there aren’t any other physics acting on it.

2 Likes

In this code: HRP.Position == moveableCameras.cam1A you need to also check the position of the cam1A. Right now you’re just checking if the instance even exists.

So it would be like this: HRP.Position == moveableCameras.cam1A.Position

2 Likes

Got this in the output: “Position is not a valid member of Vector3 - Server - ChicaScript:90”

2 Likes

Oh yeah sorry I forgot your position was a Vector3 value in a table, I thought it was an instance.

I think your problem is the one @CompilerError mentioned, you would just anchor the HRP which you could do if the animatronic doesn’t have any animation.

2 Likes

All the HumanoidRootPart’s of my animatronics are anchored and they animate fine; I don’t need them to walk or anything, just pose like in the actual game

1 Like

Okay maybe the poses alter the position of the HRP.

The smartest thing I can think of that you can do is just to make a local storing the position that the HRP has been teleported to.

You could make a local function called Teleport() or something similar that will put a local Vector3 value called ‘chicaPos’ to the position and then teleport the HRP to there as well.

Then you could simply do: if chicaPos == moveableCameras.cam1A then

1 Like

I checked if the animations altered the position like an hour ago. I’ll check again.

2 Likes

No change. Also I just realized if that was the case Bonnie wouldn’t be working.

1 Like

Could you send over your code so I can look into it?

1 Like

Hold on, i’m trying something rn

1 Like

Okay, here’s my code.

local TweenService = game:GetService("TweenService")
local TeleportService = game:GetService("TeleportService")
local animatronic = script.Parent
local humanoid = animatronic:WaitForChild("Humanoid")
local HRP = animatronic:WaitForChild("HumanoidRootPart")
local rightDoorValue = game.ReplicatedStorage:WaitForChild("Values").RightDoor
local camerasValue = game.ReplicatedStorage:WaitForChild("Values").Cameras
local lookForwardEvent = game.ReplicatedStorage:WaitForChild("LookForwardEvent")
local moveSound = game.SoundService:WaitForChild("MoveSound")
local jumpscareSound = game.SoundService:WaitForChild("JumpscareSound")

local moveableCameras = {
	cam1A = Vector3.new(0.7, 6.92, -65.9),
	cam1B = Vector3.new(0, 5.92, -41.4),
	cam4A = Vector3.new(6, 5.92, -9.9),
	cam4B = Vector3.new(3.6, 5.92, 18.2),
	cam6 = Vector3.new(14.7, 5.92, -16),
	cam7 = Vector3.new(22.9, 5.92, -53.5),
	attackPos = Vector3.new(2.341, 5.92, 4.391)
}

local animations = {
	stagePose = script.Parent.StagePose,
	diningRoomPose = script.Parent.DiningRoomPose,
	restroomPose = script.Parent.RestroomPose,
	hallPose = script.Parent.HallPose,
	cornerPose = script.Parent.CornerPose,
	attackPose = script.Parent.AttackPose,
	jumpscare = script.Parent.Jumpscare
}

local animationTracks = {
	stagePoseTrack = humanoid:LoadAnimation(animations.stagePose),
	diningRoomPoseTrack = humanoid:LoadAnimation(animations.diningRoomPose),
	restroomPoseTrack = humanoid:LoadAnimation(animations.restroomPose),
	hallPoseTrack = humanoid:LoadAnimation(animations.hallPose),
	cornerPoseTrack = humanoid:LoadAnimation(animations.cornerPose),
	attackPoseTrack = humanoid:LoadAnimation(animations.attackPose),
	jumpscareTrack = humanoid:LoadAnimation(animations.jumpscare)
}

local function CAM1BMove()
	HRP.Position = moveableCameras.cam1B
	HRP.Orientation = Vector3.new(0, 90, 0)
end

local function CAM4AMove()
	HRP.Position = moveableCameras.cam4A
	HRP.Orientation = Vector3.new(0, -180, 0)
end

local function CAM4BMove()
	HRP.Position = moveableCameras.cam4B
	HRP.Orientation = Vector3.new(0, 0, 0)
end

local function CAM6Move()
	HRP.Position = moveableCameras.cam6
	HRP.Orientation = Vector3.new(0, 90, 0)
end

local function CAM7Move()
	HRP.Position = moveableCameras.cam7
	HRP.Orientation = Vector3.new(0, 45, 0)
end

local function JumpscareMove(player)
	HRP.Position = Vector3.new(-6, -3.68, 8.8)
	HRP.Orientation = Vector3.new(0, -180, 0)
	TweenService:Create(HRP, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Position = Vector3.new(-6, 5.22, 8.8)}):Play()
	wait(0.4)
	animationTracks.jumpscareTrack:Play()
	jumpscareSound:Play()
	wait(0.5)
	player.PlayerGui.GameOverGui.Enabled = true
	jumpscareSound.Ended:Connect(function()
		TeleportService:Teleport(13882586605, player)
	end)
end

local function movement(player)
	local playerGui = player:WaitForChild("PlayerGui")
	local videoStatic = playerGui:WaitForChild("CameraGui").VideoStatic.VideoTransparency
	moveSound:Play()
	if camerasValue.Value == true then
		videoStatic.Transparency = NumberSequence.new(0)
		wait(1)
		videoStatic.Transparency = NumberSequence.new(0.8)
	end
	if HRP.Position == moveableCameras.cam1A then
		CAM1BMove()
		animationTracks.diningRoomPoseTrack:Play()
		wait(math.random(30, 60))
		animationTracks.diningRoomPoseTrack:Stop()
		movement(player)
	elseif HRP.Position == moveableCameras.cam1B then
		local rng = math.random(1, 3)
		if rng == 1 then
			CAM7Move()
			animationTracks.restroomPoseTrack:Play()
			wait(math.random(30, 60))
			animationTracks.restroomPoseTrack:Stop()
			movement(player)
		elseif rng == 2 then
			CAM6Move()
			wait(math.random(30, 60))
			movement(player)
		elseif rng == 3 then
			CAM4AMove()
			animationTracks.hallPoseTrack:Play()
			wait(math.random(30, 60))
			animationTracks.hallPoseTrack:Stop()
			movement(player)
		end
	elseif HRP.Position == moveableCameras.cam4A then
		CAM4BMove()
		animationTracks.cornerPoseTrack:Play()
		wait(math.random(30, 60))
		animationTracks.cornerPoseTrack:Stop()
		movement(player)
	elseif HRP.Position == moveableCameras.cam4B then
		HRP.Position = moveableCameras.attackPos
		HRP.Orientation = Vector3.new(0, 112.5, 0)
		animationTracks.attackPoseTrack:Play()
		wait(math.random(10, 15))
		animationTracks.attackPoseTrack:Stop()
		if rightDoorValue.Value == false then
			lookForwardEvent:FireClient(player)
			JumpscareMove(player)
		elseif rightDoorValue.Value == true then
			local rng = math.random(1, 2)
			if rng == 1 then
				CAM7Move()
				animationTracks.restroomPoseTrack:Play()
				wait(math.random(30, 60))
				animationTracks.restroomPoseTrack:Stop()
				movement(player)
			elseif rng == 2 then
				CAM6Move()
				wait(math.random(30, 60))
				movement(player)
			elseif rng == 3 then
				CAM1BMove()
				animationTracks.diningRoomPoseTrack:Play()
				wait(math.random(30, 60))
				animationTracks.diningRoomPoseTrack:Stop()
				movement(player)
			end
		end
	elseif HRP.Position == moveableCameras.cam6 then
		CAM1BMove()
		animationTracks.diningRoomPoseTrack:Play()
		wait(math.random(30, 60))
		animationTracks.diningRoomPoseTrack:Stop()
		movement()
	elseif HRP.Position == moveableCameras.cam7 then
		CAM1BMove()
		animationTracks.diningRoomPoseTrack:Play()
		wait(math.random(30, 60))
		animationTracks.diningRoomPoseTrack:Stop()
		movement()
	end
end

game.Players.PlayerAdded:Connect(function(player)
	animationTracks.stagePoseTrack:Play()
	wait(math.random(30, 60))
	animatronic.EXLeftHand.Cupcake:Destroy()
	animationTracks.stagePoseTrack:Stop()
	movement(player)
end)
1 Like

The issue may potentially be because of lines like these:

if HRP.Position == moveableCameras.cam1A then

Sometimes, unlike integers and boolean values, equating vector values may not be exact and have issues. Have you tried something along the lines of:

local function checkIfNear(position1, position2, margin)
	margin = margin or 0.1  -- change this around if needed
	return (position1 - position2).Magnitude < margin
end

if checkIfNear(HRP.Position, moveableCameras.cam1A) then
2 Likes

First off I highly recommend you make just one function for all the ‘CamMove’, as making a new function for each cam ruins the point of even having a function. So here’s an example:

local function moveAnimatronic(pos, orientation)
	if pos and orientation then
		HRP.Position = pos
		HRP.Orientation = orientation
	end
end

-Where you obviously just use the ‘moveableCameras’ table for the pos and just do a Vector3.new(x,x,x) as the orientation. Or you could implement the orientation in the table as well.

I think you should try making sure that the HRP is exactly the same position as the one you’re checking, and if that doesn’t work then make some kind of ‘system’ with just a local variable like I mentioned above. Because that would 100% work.

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