Object not decreasing in velocity properly

So i have a racket that is supposed to spawn a ball and then after a bit the ball loses velocity,
But for some reason the ball flys up and then loses velocity! The script is in serverscriptservice, and is a server script.

Ignore the –

local storage = game.ReplicatedStorage

local RacketEvent = storage.RacketEvents.RacketClient



local function onActivation(player)

	
	local character = player.Character or player.CharacterAdded:Wait()
	local root = character:WaitForChild("HumanoidRootPart")

	

	local ogball = game.ReplicatedStorage:WaitForChild("ball")
	local ball = ogball:Clone()
    --ball.Shape = Enum.PartType.Ball
	--ball.Color = Color3.new(1, 0.333333, 0)
	--ball.Size = Vector3.new(2,2,2)
	--ball.Material = Enum.Material.Granite
	--ball.CanCollide = true
	wait(.1)


	ball.Parent = root

	local newCFrame = root.CFrame * CFrame.new(Vector3.new(0,-2,0))
	local cf = ball.CFrame

	ball.CFrame = newCFrame


	--local Velocity = Instance.new("BodyVelocity")

	--Velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) 
	ball.Velocity = ball.CFrame.lookVector * 90
	ball.Parent = game.Workspace
	
	local speed = Vector3.new(90,90,90)


		wait(.5)
		for count = 1, 10 do
			wait(.1)
			ball.Velocity = speed - Vector3.new(10,10,10)
			print('this is working')
		end



	

wait(1)
	--Velocity.Parent = ball



	ball.Touched:connect(function(hit) 
		if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character then
			
			local humanoid1 = hit.Parent:FindFirstChild('Humanoid')

			ball.CanCollide = false
			ball.CanTouch = false
			ball.Transparency = 1
		--sound:Play()

		end
	end)

	

--	ball.Touched:connect(function(hit) 
--		if hit:IsA("Part")  and hit.Parent ~= player.Character and hit.Parent ~= player.Character.Racket then

		

	--		ball.CanCollide = false
		--	ball.CanTouch = false
	--		ball.Transparency = 1

			--sound:Play()

		end

	--end)
	


--end


RacketEvent.OnServerEvent:Connect(onActivation)

You know, there’s a reason why roblox both deprecated and hid the .Velocity property. It literally says there to “expect problems”.

Consider using :ApplyImpulse() or just a bodymover instead

Sorry!
I knew it was deprecated, but I didn’t know apply impulse was a thing
How could I keep the look vector part of the code though?

Actually, the problem seems to be your math

The speed variable doesn’t change at all, so that statement evaluates to ball.Velocity = Vector3.new(80,80,80) and the script keeps the velocity at that
Perhaps you meant to do:

for count = 1, 10 do
	wait(.1)
	speed -= Vector3.new(10,10,10)
	ball.Velocity = speed
	print('this is working')
end

In addition to that, you should definitely not be using the .Velocity property itself, and instead switch to .AssemblyLinearVelocity

I changed it to how you stated, but the issue still persists.

Should i just change it to applyimpulse?
And if so, how would i go about doing that.

You would simply just do

local speed = Vector3.new(90,90,90)
ball:ApplyImpulse(speed)

And disregard the loop completely

I fixed it what you said, but the object flys up into one location, and very fast.

robloxapp-20220508-1445425.wmv (1.4 MB)

Did you remove this line? If it’s still there then it’s putting additional force on the ball
image

Only the ApplyImpulse function should be there

Yes I removed that line.


Try stepping down the velocity since mass also matters

Alright so changing the velocity stops it from going very high up and what not but it still doesnt go in the direction of the torso. Just in that same direction /:

You didn’t explain it clearly in your original post that that’s what you wanted to achieve :sweat_smile:

To do that, you will have to first find the displacement between the ball and the player’s torso

--example code
local disp = player.HumanoidRootPart.CFrame.Position - ball.CFrame.Position

And then use that as the direction for the impulse:

local vec = disp.Unit * 90 --90 is the speed
ball:ApplyImpulse(vec)

Note that if the ball goes in the opposite direction (a 180 turn), you just need to flip over the vector by negating it

local vec = -(disp.Unit * 90)

From there, you can add a height offset to make it shoot upwards

local vec = disp.Unit * 90 + Vector3.new(0, 10, 0)

Ye sorry my bad.

So I re edited the script to what you said

local storage = game.ReplicatedStorage

local RacketEvent = storage.RacketEvents.RacketClient



local function onActivation(player)

	
	local character = player.Character or player.CharacterAdded:Wait()
	local root = character:WaitForChild("HumanoidRootPart")

	

	local ogball = game.ReplicatedStorage:WaitForChild("ball")
	local ball = ogball:Clone()
    --ball.Shape = Enum.PartType.Ball
	--ball.Color = Color3.new(1, 0.333333, 0)
	--ball.Size = Vector3.new(2,2,2)
	--ball.Material = Enum.Material.Granite
	--ball.CanCollide = true
	wait(.1)


	ball.Parent = root

	local newCFrame = root.CFrame * CFrame.new(Vector3.new(0,-2,0))
	local cf = ball.CFrame

	ball.CFrame = newCFrame


	--local Velocity = Instance.new("BodyVelocity")

	--Velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) 
	--ball.Velocity= ball.CFrame.lookVector * 90
	--ball.Parent = game.Workspace
	
	--example code
	local disp = player.root.CFrame.Position - ball.CFrame.Position
	local vec = disp.Unit * 90 --90 is the speed
	ball:ApplyImpulse(vec)
	--local speed = Vector3.new(9,9,9)
	--ball:ApplyImpulse(speed)

	--wait(.5)
--	for count = 1, 10 do
		--wait(.1)
		--speed -= Vector3.new(10,10,10)
		--ball.Velocity = speed
		---print('this is working')
	--end


	

wait(1)
	--Velocity.Parent = ball



	ball.Touched:connect(function(hit) 
		if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character then
			
			local humanoid1 = hit.Parent:FindFirstChild('Humanoid')

			ball.CanCollide = false
			ball.CanTouch = false
			ball.Transparency = 1
		--sound:Play()

		end
	end)

	

--	ball.Touched:connect(function(hit) 
--		if hit:IsA("Part")  and hit.Parent ~= player.Character and hit.Parent ~= player.Character.Racket then

		

	--		ball.CanCollide = false
		--	ball.CanTouch = false
	--		ball.Transparency = 1

			--sound:Play()

		end

	--end)
	


--end


RacketEvent.OnServerEvent:Connect(onActivation)

I get the error that root isnt part of players.(my username)

Even though root is defined to be the humanoid root part…

What did you do here???
image
root is the variable, not the name of a member

local character = player.Character or player.CharacterAdded:Wait()
	local root = character:WaitForChild("HumanoidRootPart")

I defined it to be the humanoid root part… Did I do that incorrectly or…

Nonono, look what you’re doing here

Why are you indexing for root? You already have the root
It should be

local disp = root.CFrame.Position - ball.CFrame.Position

OHHh I didnt notice the player.root XD

I didnt realise what I was doing there my bad, once again

Edit: By fixing that now the ball just flys straight up into the air, does that mean I have to adjust the vector like you said in your other comment?

yes, I do believe moving at 90 studs per second would be a bit extreme for a small ball

Ye lol

It seems to be working now.

Do you mind if i message you in the future if I have any other issues regarding this topic?

1 Like