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.
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
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