Problems with .Velocity

So I am making a game, where a part flys toward the nearest player, but the part keeps going under the world and hitting the players feet. (Which makes it impossible for the player to see the part coming) I tried adding a

if part.CFrame.Y > 2.5 then
part.CFrame.Y = 2.5
end

but the part still attempts to go under and gets teleported back which looks weird and causes the parts speed to glitch.

When the if part CFrame is not in the script, the part curves below the world, flys to the player and curves up and hits the player from the bottom, (Like a U but wider).

When the script is in though, the part gets really slow (because its trying to go down, not straight fsr) but as it gets closer to the player it speeds up and is just really inconsistent.

script:


RunService.Heartbeat:Connect(function()
		local targetPosition = targetPlayer.Character:WaitForChild("Head").Position
		local currentPosition = clonedBall.Position
		if clonedBall.CFrame.Y < 2.5 then
			clonedBall.CFrame = CFrame.new(currentPosition.X, 2.5, currentPosition.Z)
		local direction = (targetPosition - currentPosition).unit
		CSpeed = CSpeed + config.BallAcceleration
		clonedBall.Velocity = direction * CSpeed
end)

all helps is appreciated.

Also I think .Velocity is depreciated or something but I followed a tutorial which came out recently and it worked for the YT so idk.

also this is not the full code jsyk

Could we see what it looks like it would help better

1 Like

yeah, it might be a few minutes before I can send a vid though.

1 Like

Yeah I would say maybe try a different method of clamping the balls position, like I said it would be easier to diagnose it when I see the error so yeah and you mentioned that it wasnt the full script so anything else would help really that is related to the ball movement

1 Like

Thats all thats related besides finding the target player.

vid with the IF
robloxapp-20231127-1114580.wmv (1.7 MB)

vid without the if
robloxapp-20231127-1116491.wmv (942.5 KB)

Okay so in the second clip where was the ball starting?

1 Like

Right in the middle, however it IS unachored with cancollide off so I was thinking that it fell and rises up again.

I think you might have just found your problem, make sure its not falling down when the round is starting

1 Like

How would I do that? Because if its anchored it won’t move.

Keep the ball in its same position until its found its target

1 Like

as soon as it spawns it choses a target. I can send the full script.

Then how is it falling down then? Are you using body movers to move it?

1 Like

I have no idea, that’s why I made this post

server side script

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local config = require(game.ServerScriptService:WaitForChild("Config"))

local CSpeed = config.BallSpeed
local gameActive = replicatedStorage.RoundSystemEvents.GameActive

local ball = serverStorage.Ball
local clonedBall
local targetPlayer
local playerWhoBlocked = nil
local charWhoBlocked = nil
local deadPlayers = {}
local hitNum = 0

local function findNearestPlr(PreviousTarget)
	local nearestPlr = nil
	local ValidPlrs = 0
	local shortestDistance = math.huge
	
	for i, plr in pairs(Players:GetPlayers()) do
		if not plr.Character then return end
		
		
		local distance = (plr.Character.HumanoidRootPart.Position - ball.Position).magnitude
		local InGame = plr:FindFirstChild("InGame")
		
		if InGame then
			ValidPlrs +=1
			if distance < shortestDistance and not table.find( deadPlayers, plr.UserId) then
				if playerWhoBlocked then
					if plr.UserId ~= playerWhoBlocked.UserId then
						shortestDistance = distance
						nearestPlr = plr
					end
				else
					shortestDistance = distance
					nearestPlr = plr
				end
			end
		end		
	end
	if ValidPlrs > 0 then
		targetPlayer = nearestPlr
	end
end

local function reset()
	if clonedBall then
		clonedBall:Destroy()
	end
	playerWhoBlocked = nil
	charWhoBlocked = nil
	CSpeed = config.BallSpeed
	targetPlayer = nil
	
	for i,plr in pairs(Players:GetPlayers()) do
		if plr.Character and plr.Character:FindFirstChildOfClass("Highlight") then
			plr.Character:FindFirstChildOfClass("Highlight"):Destroy()
		end
	end
end

local function HighlightPlayers()
	for i,plr in pairs(Players:GetPlayers()) do
		if plr:FindFirstChild("InGame") then 
			if plr.Character and plr.Character:FindFirstChildOfClass("Highlight") then
				plr.Character:FindFirstChildOfClass("Highlight"):Destroy()
			end
			
			local highlight = Instance.new("Highlight", plr.Character)
			highlight.Adornee = plr.Character
			
			if plr == targetPlayer then
				highlight.FillColor = Color3.fromRGB(255,0,0)
				highlight.OutlineColor = Color3.fromRGB(255,0,0)
			else
				highlight.FillTransparency = 1
				highlight.OutlineColor = Color3.fromRGB(255,255,255)
			end
		end
	end
end

local function SpawnBall()
	reset()
	findNearestPlr()
	
	if not targetPlayer or gameActive.Value ~= true then
		reset()
		return
	end
	playerWhoBlocked = nil
	charWhoBlocked = nil
	clonedBall = ball:Clone()
	clonedBall.Parent = workspace
	
	HighlightPlayers()
	
	clonedBall.Touched:Connect(function(hit)
		local HRP = hit.Parent:FindFirstChild("HumanoidRootPart")

		if HRP then
			if HRP.Parent == charWhoBlocked  then
				return
			end
			if HRP.Parent.Blocking.Value == false or HRP.Parent.Blocking == nil then
				local explosion = Instance.new("Explosion")
				explosion.BlastRadius = 25
				explosion.BlastPressure = 3000
				explosion.Position = HRP.Position
				explosion.Parent = HRP
				
				local InGame = targetPlayer:FindFirstChild("InGame")
				if InGame then
					InGame:Destroy()
				end
				
				if not table.find(deadPlayers, targetPlayer.UserId) then
					table.insert(deadPlayers, targetPlayer.UserId)
				end
				if playerWhoBlocked ~= nil then
					game.Players:GetPlayerByUserId(playerWhoBlocked.UserId).leaderstats.Kills.Value += 1
					game.Players:GetPlayerByUserId(playerWhoBlocked.UserId).leaderstats.Cash.Value += 50
					game.ReplicatedStorage.KilledPlayerEvent:FireAllClients(playerWhoBlocked.UserId, targetPlayer.Name)
					
				end
				
				reset()
				task.wait(0.5)
				
				if gameActive.Value == true then
					SpawnBall()
				end
			else
				print(targetPlayer.Name .. " Blocked!")
				playerWhoBlocked = targetPlayer
				charWhoBlocked = targetPlayer.Character
				findNearestPlr(targetPlayer.UserId)
				CSpeed	*= 1.08
				hitNum += 1
				print(hitNum)
			end
		end
	end)
end

Players.PlayerRemoving:Connect(function(plr)
	if plr == targetPlayer then
		table.insert(deadPlayers, plr.UserId)
		SpawnBall()
	end
end)

gameActive.Changed:Connect(function()
	if gameActive.Value == true then
		task.wait(1.5)
		SpawnBall()
	else
		table.clear(deadPlayers)
	end
end)

RunService.Heartbeat:Connect(function()
	if clonedBall == nil then
		SpawnBall()
	end
	if targetPlayer and gameActive.Value == true and targetPlayer:FindFirstChild("InGame") then
		local targetPosition = targetPlayer.Character:WaitForChild("Head").Position
		local currentPosition = clonedBall.Position
		--[[if clonedBall.CFrame.Y < 2.5 then
			clonedBall.CFrame = CFrame.new(currentPosition.X, 2.5, currentPosition.Z)
		end]]
		local direction = (targetPosition - currentPosition).unit
		CSpeed = CSpeed + config.BallAcceleration
		clonedBall.Velocity = direction * CSpeed

	else
		reset()
	end
	if clonedBall == nil and gameActive.Value == 1 then
		reset()
	end
	
end)

while wait(0.05) do
	HighlightPlayers()
end

Wait a second your using cframe to move the ball not any physics related stuff?

1 Like

yes… is that a bad idea or something? I don’t really know the difference between vector velocity and linear velocity and vector force

you change change the cframe of anchored obects though…?

1 Like

I think its because of .Velocity

How are you using cframe to move it aswell as .Velocity?

It’s either one or the other

1 Like