Ive been looking everywhere for this solution, devforum, youtube, but nothing appears to work for me.
Ive been wanting to make an npc that stops moving when you look at it, and start moving when you look away.
Ive tried following a devforum post, didnt work.
also, i am not a very good scripter at all, i just started, so any help would be great
You could probably fire a ray from the player’s head to a certain distance and see if the ray hit the NPC. Probably not the best solution but it’s the only thing I can think of.
Also @BandolPole you should format your posts better for readability sake. Use proper grammar and paste your code surrounded by 3 backticks (```) instead of screenshotting it.
if not PlayerCanSee then
NPC:PivotTo(PlayerCharacter:GetPivot())
end
If statements work by running the code inside of them only if the statement between the if and the then is true. In this case, the statement is not PlayerCanSee which equates to PlayerCanSee == nil or PlayerCanSee == false.
local Camera = workspace.CurrentCamera
local Stan = workspace.Stan
local Players = game:GetService("Players")
game:GetService('RunService').Stepped:Connect(function()
local Position, PlayerCanSee = Camera:WorldToScreenPoint(Stan.HumanoidRootPart.Position)
if PlayerCanSee then
workspace.Stan.HumanoidRootPart.Anchored = true
if not PlayerCanSee then
Stan:PivotTo(game.Players.LocalPlayer.Character:GetPivot())
end
end)
You put the not PlayerCanSee if statement inside of an if statement where the code will only run if PlayerCanSee is true. You need to move it outside, like this:
local RunService = game:GetService "RunService"
local Players = game:GetService "Players"
local Camera = workspace.CurrentCamera
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Stan = workspace.Stan
RunService.PreSimulation:Connect(function()
local Position, PlayerCanSee = Camera:WorldToScreenPoint(Stan.HumanoidRootPart.Position)
if PlayerCanSee then
workspace.Stan.HumanoidRootPart.Anchored = true
end
if not PlayerCanSee then
Stan:PivotTo(Character:GetPivot())
end
end)
it works, but now he noclips into the floor at times, oh also he noclips right into the player, which flings the player, oh also sometimes he just floats