I have already achieved on rotating the player based on the workspace’s currentcamera. The problem I am having is making it so everyone can see what direction I am facing. I’ve actually gotten close but then I got lost.
I’m actually quite stumbled on this. I know how remote events and functions work but maybe I should relearn them because I’m lost. The main problem isn’t really with the remote event I think, it’s a problem with this specific chunk of my code.
Server Script
-- Functions
LookEvent.OnServerEvent:Connect(function(player, neckCF, waistCF)
for _, plr in ipairs(Players:GetPlayers()) do
if plr ~= player then -- plr == player prints ('WRYYYYYYY) for some reason, why? I need it to be plr ~= player
print('WRYYYYYY') -- prints when 2 or more players are present, but neither of them can see the other players rotation
else ----------------------------------------------------------------They print at the same time on both clients I think
print('YOU THOUGHT YOUR CODE WORKED BUT IT WAS ME! KONO DIO DA!') --also prints when 2 or more players are present
LookEvent:FireClient(plr, player, neckCF, waistCF) -- fires
print('IIMA FIRE DA CLIENT') -- even tho this prints, it doesn't work, because the "KONO DIO DA" print was printed
end
end
end)
I tried using a remote function at first but that didn’t really work out. Couldn’t figure out why so I moved on the remote event. Might go back and use the remote function in the future to figure this out.
I don’t think I’m looping though the players incorrectly, but all other parts of the code in the local script work just fine. Is it the looping I’m doing wrong, or the way I’m trying to use the remote event?
Basically, the loop is to get the individual player to see the other players rotated body.
And what I mean by
I mean both prints work. Sorry, could’ve worded that better but I’m extremely tired. But yeah when 2 or more players are present, they both print, but the player cannot see the other players rotated body.
Well that doesn’t mean it doesn’t work, because the KONO DIO DA! print is in the same section as the IIMA FIRE DA CLIENT print. They’re both afte the else statement and before the first “end”.
I just found something else. I formatted the if statement differently and now the code kinda works but now I have a DIFFERENT problem smh. I put the end statement after the print and not the fireclient function. Now the code is working.
forgot to include working code
LookEvent.OnServerEvent:Connect(function(player, neckCF, waistCF)
for _, plr in ipairs(Players:GetPlayers()) do
if plr ~= player then -- plr == player prints for some reason, why? I need it to be plr ~= player. When I put plr == player, it prints ('WRYYYYYY') but doesn't fire the client
print('WRYYYYYY') -- prints when 2 or more players are present, but doesn't work
else ----------------------------------------------------------------They print at the same time on one client, but not on the other idk I've been stuck for too long
print('YOU THOUGHT YOUR CODE WORKED BUT IT WAS ME! KONO DIO DA!') --also prints when 2 or more players are present
end
LookEvent:FireClient(plr, player, neckCF, waistCF)
print('IIMA FIRE DA CLIENT') -- even tho this fires, it doesn't work, because the "KONO DIO DA" print was printed
end
end)
The NEW problem is it’s not smooth because I have to limit the remote event to fire once per second or else ya know, exhaustion and stuff like that would happen. I think I’ll have to use lerp or TweenService so as to not fire the remote event so many times.
Here is the local script if you have any other input as to what I should change or change nothing
LookEvent.OnClientEvent:Connect(function(plr, neckCF, waistCF)
local Necc = plr.Character:FindFirstChild('Neck', true)
local Waste = plr.Character:WaitForChild('UpperTorso'):FindFirstChild('Waist', true)
if Necc then
Necc.C0 = neckCF
end
if Waste then
Waste.C0 = waistCF
end
end)
while true do
wait(1)
LookEvent:FireServer(Neck.C0, Waist.C0)
end
Oh okay nice. Hmmm… what kind of rotation are you trying to do to the player? Like turning their entire body to a direction momentarily? Or what purpose does the rotation serve?
Well I think I should explain the script step by step. I was exploring the forum in search of how to rotate the player, but they all used such complicated math. The rotation works like so
CF = CFrame
CFA = CFrame.Angles
M_R = math.rad
local cam_direction = Humanoid_Root_Part.CFrame:ToObjectSpace(Camera.CFrame).LookVector --This just sets the direction we want to look at. The forward direction
Neck.C0 = CF(0, Neck_Y, 0) * CFA(0, -cam_direction.X * M_R(35) , 0) * CFA(cam_direction.Y * M_R(35) , 0, 0)
Waist.C0 = CF(0, Waist_Y, 0) * CFA(0, -cam_direction.X * M_R(35) , 0) * CFA(cam_direction.Y * M_R(35) , 0, 0)
We set the Motor6Ds part of the player CFrame and we rotate that relative to the cameras front facing direction. Now we multiply it by 35 degrees converted to radians. Think of it like clamping the maximum the player can rotate.
Then we use renderstepped to have a smooth rotation to fire every frame.
To clarify, the problem isn’t the smoothness on the client, it’s the smoothness on the receiving client.
EX: Player1 sees Player2s rotation, but it is delayed by 1 second
Hope that makes sense
Edit: Sorry, I didn’t even notice I didn’t copy and paste the whole local script. If you still need it lmk but it’s just what I wrote before and a bunch of variables that don’t pertain to the problem
Edit2: So the code works, it’s just that it still prints the KONO DIO DA, whose purpose is to print if the code doesn’t work. I ran 5 tests and 5/5 times the code worked but 3/5 times it only printed KONO DIO DA. 2/5 times it printed both WRRYYY and KONO DIO DA.
Still not smooth tho. Dunno why I chose those print strings, was tired.
Instead of firing the values from and to the server/client using Remote Events, why not have NumValues instead? Then you could have the server script change the values and have the localscript immediately react to that using .Changed.
The thing is, it really depends on what you’re using this for. Is this for a simple rotation, like a 90 degree turn or something?
I believe I might have an idea of why this is happening.
It could be doing the “ipairs” loop, but being out of order. Think about it.
Let’s say you have two players, “Player1” and “Player2”. Perhaps Player2 is the first one to send that Remote Event to the server. The ipairs loop will go into Players:GetPlayers() and search. Seems all fine right? Wrong. If it looks through that table and first finds “Player1”, then it’ll print the “KONO DIA DA!”. Then, it’ll go back to the beginning of the loop and search again, now finding “Player2”, and printing “WRYYYYYY”.
So there’s nothing wrong with it printing both. It’s simply doing that because every now and then, it first finds the player that is not itself, and then moving on until it does. And I believe that is the case because you said this only happens when there’s 2 or more players in the game.
Ohhh okay. Hmmm, now that I know what it’s for, sure the NumValues would work.
I was thinking you could try holding the CFrame values in NumValues/IntValues (NumValues allow for decimals, IntValues don’t). Then have the server script change those values instantly, as long as it’s they’re somewhere available to both, like in a ModuleScript maybe, or using _G, or something. And then the localscript will detect the change using .Changed, and make the player’s character the same, like this:
local numValueObject = -- idk
numValueObject.Changed:Connect(function()
player.C0 = numValueObject.Value -- Idk how the C0 thing works :P
end)
And you could either use numValues/intValues for each of the CFrame values required, or store it in a stringValue, then "un-stringing’’ it somehow.
The thing I’m stumped on, is how to detect when to make those changes and to detect it from the player to be mimicked. Though you could have a way to tell it through another value when to enable the “mimicking”, and who to mimick from and to. Sorry for saying mimick lol, I just assume that’s what you’re trying to do.
I’ll continue this later today or else my eyes will fall out of my head.
Don’t know if this is important or not but, if I test this solo, the rotation of the player doesn’t replicate to the server. Not really a problem I think since it’s solo but still.
With 2 people as I said, not smooth and prints both even with your take on why it prints both statements, when it should only be printing one statement, which is WRYYYY. I’d like to focus on how to make this smoother though because to me it doesn’t make sense as to why both statements should print when the code works. Just not a smooth rotation as stated before. Smoothness being not the actual player, but the receiver.
If you’re available tomorrow I’d love your continued help. Thank you for staying with me so far.
I’ll be sure to check out the NumValue later today. Thanks so far.
Well it’s most probably not smooth due to the RemoteEvent having to fire every 1 second to avoid network problems. And about it printing both statements, if it only does it when there’s 2 or more players in the game, not when there’s only 1, then it’s completely fine as I said earlier.
But when it comes to smoothening it, we’ll probably need to find another way to have very fast updates on it (faster than once per second of course haha). That’s why I thought of perhaps using values to dictate the movement from client1 to server to client2 for the mimicking, because that would be near-instantaneous if it works the way I’m thinking it would, and wouldn’t have issues with the smoothness.
And when you’re back online, could you let me know if this is correct?: The problem right now is that the player’s character that is mimicking the other player’s is not smooth, it’s choppy. And that is likely caused by the receiver, not the client, with the receiver being the server when it receives the Remote Event from the first player and sends it off to the other player to mimick the first player every 1 second.
^ Just making sure I understand everything correctly, or if I don’t.
Yeah, for sure! I do have school going on, so I may be on later in the day, but I’ll do my best to remember to get on here as soon as I can. If others help you find a solution though, then great I’ll put reminders on my whiteboard haha. I wish you the best in the meanwhile! Good night.
So the Update is a variable and it’s equal to 1/20. If I ever feel like changing it, I just change it to 1 or .5 or whatever.
I had put this script below originally in the local script. Because I assumed if I connected with heartbeat, it would smoothly transition, but that wasn’t the case it just made it worse, and it would not update. The smoothness comes from how fast the remote event is fired. And I read that firing it 1/20 of a second is fine and won’t jam the server.
Still looking for a better way though.
Original Script
LookEvent.OnClientEvent:Connect(function(plr, neckCF, waistCF)
Heartbeat:Connect(function()
local Characterr = plr.Character
local Necks = Characterr:FindFirstChild('Neck', true)
local Waists = Characterr:FindFirstChild('Waist', true)
Necks.C0 = neckCF
Waists.C0 = waistCF
end)
end)