Hello! I have a script that fires a projectile. It all works fine, there is no delay. But, sometimes, specifically for mobile users, the part just won’t move anywhere. It parents to the workspace, but there is no velocity, and you can walk into it and it will move. I’m not really sure how to go about fixing it.
--"player" is a Player object obtained from a RemoteEvent
--"height", "range", and "speed" are obtained from the RemoteEvent
--"mouseFrame" is the player's camera CFrame (i never got round to changing the name of the variable)
--please ignore the indentation, it's there because this code is inside a function
local velocity = Instance.new("BodyVelocity")
velocity.Parent = clonedBall
velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
velocity.Velocity = (mouseFrame.LookVector * range * speed + Vector3.new(0, height, 0))
clonedBall:SetNetworkOwner(player)
task.wait(1 / speed)
velocity:Destroy()
task.wait(1 - (1 / speed))
player.CanThrow.Value = true
is this all done from a server sided script? if the client now has complete control over the projectile then the server might not be able to effect its velocity anymore and you should instead set its velocity from a localscript.
im not 100% sure on this since I never made projectile systems in this manner.
try setting network ownership first and then setting velocity, also BodyVelocity is depreciated which could also be why its not working, I recommend using LinearVelocity instead.
they look complicated but they are pretty easy to use, you basically just pair it with an attachment. after setting it to OneAttachment you can set it up just like a normal bodyvelocity.
Update 2: I’ve reverted back to a BodyVelocity because a LinearVelocity just completely messed up the accuracy. Ill look into changing it at some point.
The issue is still occuring, I have tried lowering the speed and messing with a frw other physics.
what do you mean when the accuracy is off? if there is a slight offset from where the projectile is supposed to be at then that’s caused by network latency and not the mover itself. try just making the projectile client sided with linear velocity used. put it all in a localscript and don’t set the network ownership and see if that does it.
If I move it client-side, it will only move for that client, and I need it to move for all the players, so that wouldn’t work. Any other ideas? I’m all out to be honest…
having it client sided would be just to see if it works. in most cases when making projectiles you have a client sided projectile and a server sided projectile.
the client projectile is for special effects and smooth visuals.
the server hitbox is only for calculating stuff.
everyone has their own way of doing it but for me personally I like to use a single modulescript that uses runservice to seperate client and server environments.
in essence it goes like this:
1: localscript fires a projectile, then tells the server via a remote event to fire a server projectile.
2: server recieves the aim position the client wants to fire at and tells all other clients via a remote event to create their own client projectile by sending the aim position and the players character position as the start point, server projectile also is fired.
3: client hitbox hits and produces any effects you want it to have, and the server projectile deals damage.
the benifit of this is that projectiles appear smooth and there wont be and network ownership mishaps.
The projectile only fires once every two seconds, so I don’t really need to optimise it that much. Firing it client-side and telling each client wouldn’t work - it’s a projectile with a curved path, which means it’s just more work to do it server-side and client-side instead of just server-side, because I can’t just cast a ray.
What’s confusing me is that this issue only happens on mobile - it doesn’t even happen in Studio’s mobile test setting.
Ok, I’ve figured out the issue now.
It’s not to do with the actual code, it’s to do with the device. I tried the game on my friend’s phone, which has more RAM than mine, and I didn’t get the issue once. Not sure how I would go about fixing this, though.
your still using bodyvelocity, that’s definitely the reason why it wont move anymore.
use linearvelocity, looking at the scripts in the past im wondering if the math/order of operations your using is messing it up so this time try setting all the math you need in variables then when setting the vector velocity do something like this:
No, BodyVelocity isn’t the issue because the velocity still works on other devices that have more RAM. I tried using both LinearVelocity and VectorForce and messing around with mechanics, values, etc. (yes I remembered to set the constraint type on linear one) and the ball didn’t move at all. Using other velocities completely messes with the mechanics.
I tried it again earlier today and it didn’t work. At some point I’ll convert to it if there’s a way to make it behave like a BodyVelocity.
its better not to use legacy bodymovers anymore, if it is indeed broken roblox wont be fixing them anytime soon since they are now depreciated.
if you need the ball to just move briefly there is an assembly velocity function called ApplyImpulse()
which applies a brief force to objects just like bodyForce.
this math pushes an object to a positon using this method: object:ApplyImpulse((object.Position - goalpos).Unit * -7000)
side note: I copied this snippet from one of my projects so you might have to play with the code a bit as -7000 might be reversed for you.
After a lot of trying, I got it converted to a LinearVelocity. But, now there’s a problem: the ball projectile just forces itself to the ground and I have no idea why.