Im writing some code for my game. The premise is that a message is printed in output, in my case it is “Bob time”. It is supposed to rotate an npc in the game I have facing the humanoid root part (Game is single player, so the issue of multiple players isn’t an issue).
I had the script set up so that it is always facing the player (And it worked perfectly fine, but I decided to change it so it snaps to look at the player upon receiving the message in the output.
Here is my code:
local NPC = workspace.bob
local HumanoidRootPart = NPC.PrimaryPart
local Target = Player.Character or Player.CharacterAdded:Wait()
Target = Target:WaitForChild("HumanoidRootPart")
local messageToFind2 = "Bob time"
game:GetService("LogService").MessageOut:Connect(function(message, messageType)
if messageType == Enum.MessageType.MessageOutput and message:find(messageToFind2) then
print("Copy!")
NPC:SetPrimaryPartCFrame(CFrame.lookAt(HumanoidRootPart.Position, Target.Position * Vector3.new(1, 0, 1) + HumanoidRootPart.Position * Vector3.new(0, 1, 0)))
end
end)
For some reason, the script is now broken and doesn’t even rotate the dude at all. Please let me know what is wrong with the script.
(I’m a very new scripter so please understand where im coming from)
I did find a way to get it to work.
I don’t know what exact error you had, but I was assume that the script couldn’t find the the player, since that’s the error I got.
To counter this, you can force the script to wait until it is sure that a player exists and that it has a character.
Here is the “fixed code”(your code does work, but it works before the player exists, so it throws an error, because the player does not exist yet.)
repeat -- wait until it finds a player and the player's character.
task.wait()
local PlayerCount = #game.Players:GetPlayers()
local Player = game.Players:GetPlayers()[1] or nil
local PlayerCharacter = nil
if Player ~= nil then
PlayerCharacter = Player.Character or nil
end
until PlayerCount > 0 and PlayerCharacter ~= nil
local NPC = workspace.bob
local Player = game.Players:GetPlayers()[1]
local HumanoidRootPart = NPC.PrimaryPart
local Target = Player.Character or Player.CharacterAdded:Wait()
Target = Target:WaitForChild("HumanoidRootPart")
local messageToFind2 = "Bob time"
task.delay(0,function() -- This makes it print "Bobe time!" every 2 seconds.
while true do
task.wait(2)
print("Bob time!")
end
end)
game:GetService("LogService").MessageOut:Connect(function(message, messageType)
if messageType == Enum.MessageType.MessageOutput and message:find(messageToFind2) then
warn("Copy!")
NPC:SetPrimaryPartCFrame(CFrame.lookAt(HumanoidRootPart.Position, Target.Position * Vector3.new(1, 0, 1) + HumanoidRootPart.Position * Vector3.new(0, 1, 0)))
end
end)
Hey so I tried this, but now the NPC has his back facing the player instead of the front. (rotated 180 degrees). I know this is probably a super simple fix but i’m not too advanced in terms of scripting.
This should fix it. Just multiply the CFrame.lookat() by CFrame.Angles(0, math.rad(180), 0)
math.rad() is used to convert degrees to radians, which is what cframe.angles() takes.