I’m trying to make a prop system and I’m having some trouble getting the prop offset off of the camera’s CFrame. I currently have this as the function which offsets the prop from the camera’s CFrame. It’s in a modulescript called by a serverscript.
function PropService.PropHeld(Prop)
local CameraCFrame = Camera:GetRenderCFrame()
local RequiredPropPosition = CameraCFrame * CFrame.new(0, 0, -5)
Prop.CFrame = Prop.CFrame:Lerp(RequiredPropPosition, TweenService:GetValue(0, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut))
end
end)
end)
(this function is then called using Heartbeat in the ServerScript so that it updates every frame)
The problem here is that the prop doesnt move at all.
Things I’m trying to achieve with this (just so you know)
Smooth movement at a specific offset from the camera
Smooth movement to this offset (not instant teleportation)
Works within my current setup (serverscript parented to the prop calls functions within a modulescript which do everything to minimise repeated code - i don’t want everything within the serverscript as that would mean that each prop requires a lengthy script which is inefficient)
Firstly, should probably mention that those first two "end"s are out of place and shouldn’t be there. Probably a small mistake made when pasting things into the DevForum text editor?
Pretty sure I helped you last time, and mentioned you should try printing things to make sure the code runs. So here’s a test-- inside the PropService.PropHeld function, do print(Prop.CFrame) before and after setting the CFrame to see if there is any change. Additionally, as a secondary test, put the same print statements around the PropService.PropHeld() function call like so:
Both tests should help you narrow down the specific issue. (Though I suspect it has to do with TweenService:GetValue(), so I’m going to take a look at that after posting this reply)
Edit:
Found the issue. You set the first argument in TweenService:GetValue() to 0, which is equivalent to Prop.CFrame = Prop.CFrame:Lerp(RequiredPropPosition, 0). The reason the prop doesn’t move at all is because there is no interpolation.
Hi, you did help me last time (thanks very much)
I did try printing stuff and using that narrowed it down to that TweenService:GetValue
Your solution does do something, however now the prop just jitters in midair for no apparent reason - I don’t think it can find the camera, which makes no sense because you can get workspace.CurrentCamera from everywhere (I think)
It also gives a lot of lag after triggering it. Probably something to do with all the tweens being created.
I don’t think this is an issue with GetValue(), for some reason the script thinks the camera is at this random position (31, 5, -37). I don’t really know why and I can’t think of a logical reason for it
Yeah, still thinks that it’s at this position. Moving the part does change where it thinks the camera is however I can’t see any relation between them. If you want, I can provide the Studio file for you to take a better look?
Alright, I found the solution to both issues-- the jittering, and the random position.
First off, all you need to do to fix the jittering is anchor/unanchor the prop in the Prompt.Triggered connection. When I did that, the prop sat in place without moving a bit.
Next, the camera position. You’ll notice that if you move your camera around in the workspace before play testing the game, that the position the prop sits at while held will change as well. This is because the PropService module is getting the CurrentCamera that exists on the server, not the client.
The second issue is a little more complicated to deal with. You’ll need to find some way to supply the PropService with the CFrame the prop should be moving to from the client.
Alright thanks (again), I’ll try figure out that CurrentCamera stuff. It’s late for me so I’ll try in the morning but if it works I’ll mark your post as solution. Seems like it should work though
Hi, sorry to annoy you about this again but I’ve been playing around with that getting the camera stuff. The system I’m going to be using is one where a part is offset from the camera off of the client, not the server, and then the prop will go to that. It makes things much easier than passing the required value through RemoteEvents (it worked but the game was extremely laggy)
The issue I have now is that the client part that moves just disappears after a few seconds of testing for no reason. I don’t want it to have collisions, but for some reason when I turn collisions on it stays. It’s within the StarterCharacter, if that helps. I can’t link it to anything else in the startercharacter as that breaks everything, and if I put it as a child of the script it still disappears.
If you have any suggestions that’d be great, thanks.
EDIT: Fixed it by anchoring the part. Not sure why that works but it does (possibly a bug with Roblox?)