Position Problems

So I am making a projectile system and I want the visuals to be presented in client while all the rest (like math, hitting, damage, etc) on server. To do this, I am making a false variable that imitates where the projectile will hit as well as a raycast. ideally, I’m checking to see if the magnitude between the raycast result’s position and the fake projectile is <1.

The problem is that my predictedProjectile variable barely changes at all for whatever reason, even though the movement math is nearly identical to the real projectile on client side. Why is this happening?

Here is the script (I am just testing so the script is all messed up):

while true do
        local predictedProjectilePosition = Vector3.new()
        predictedProjectilePosition = char.HumanoidRootPart.Position
        
        local projectileDirection = (mouseP - predictedProjectilePosition).Unit
        
        local raycastResult = workspace:Blockcast(CFrame.new(origin), Vector3.new(2, 2, 2), direction, rparams)
        
        local newPredictedProjectilePosition = predictedProjectilePosition + projectileDirection * projectileSpeed * task.wait(1/60)
        predictedProjectilePosition = newPredictedProjectilePosition
        
        print(predictedProjectilePosition)
        if (predictedProjectilePosition - raycastResult.Position).Magnitude < 1 then
            print('hit')
        end
end

local predictedProjectilePosition = Vector3.new()

I think the issue might be that you initialize the position at the start of the while loop instead of before it. Meaning each loop your position is reset and wont accumulate distance overtime :smiley:

Let me know if changing your code to this helps:

local predictedProjectilePosition = Vector3.new()
predictedProjectilePosition = char.HumanoidRootPart.Position
        
local projectileDirection = (mouseP - predictedProjectilePosition).Unit
while true do
        
        local raycastResult = workspace:Blockcast(CFrame.new(origin), Vector3.new(2, 2, 2), direction, rparams)
        
        local newPredictedProjectilePosition = predictedProjectilePosition + projectileDirection * projectileSpeed * task.wait(1/60)
        predictedProjectilePosition = newPredictedProjectilePosition
        
        print(predictedProjectilePosition)
        if (predictedProjectilePosition - raycastResult.Position).Magnitude < 1 then
            print('hit')
        end
end

Yes! It works really well… thank you very much! :pray:

1 Like