You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
I am making a game like blade ball, but with different game modes. -
What is the issue?
after a player blocks the ball, it is supposed to target the closest player that is not the person who blocked it. but it keep targeting the player who blocked it.
server script (name: BallHandle)
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 function findNearestPlr(PreviousTarget)
local nearestPlr = nil
local ValidPlrs = 0
local shortestDistance = math.huge
for i, plr in pairs(Players:GetPlayers()) do
--ERROR ON LINE BELOW
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) and PreviousTarget ~= plr.UserId then
shortestDistance = distance
nearestPlr = plr
end
end
end
if ValidPlrs > 1 then
targetPlayer = nearestPlr
end
end
local function reset()
if clonedBall then
clonedBall:Destroy()
end
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
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 = 1
explosion.BlastPressure = 5
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).leaderstats.Kills.Value +=1
end
reset()
task.wait(0.5)
if gameActive.Value == true then
SpawnBall()
end
else
print("Blocked!")
playerWhoBlocked = targetPlayer
charWhoBlocked = targetPlayer.Character
findNearestPlr(targetPlayer.UserId)
CSpeed = CSpeed + 0.5
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
local direction = (targetPosition - currentPosition)
CSpeed = CSpeed + config.BallAcceleration
clonedBall.Velocity = direction * CSpeed
else
reset()
end
end)
