Server script/Ball Controller Script:
local ball = script.Parent
local m1Event = game.ReplicatedStorage.RemoteEvents:FindFirstChild("MouseButton1Event")
function M1Event(player,playerMovingDirection,lookVector,rightVector,camY)
local direction
local YVector
local force
if playerMovingDirection == "Forwards" or playerMovingDirection == "Backwards" or playerMovingDirection == "Right" or playerMovingDirection == "Left" then
if camY <= 11 then
YVector = 0.0
force = 40
elseif camY >= 14 then
YVector = 0.9
force = 42
end
else
if camY <= 12 then
YVector = 0.2
force = 40
elseif camY >= 13 then
YVector = 1.8
force = 42
end
end
if playerMovingDirection == "Forwards" then
direction = lookVector + Vector3.new(0,YVector,0)
elseif playerMovingDirection == "Backwards" then
direction = -lookVector + Vector3.new(0,YVector,0)
elseif playerMovingDirection == "Right" then
direction = rightVector + Vector3.new(0,YVector,0)
elseif playerMovingDirection == "Left" then
direction = -rightVector + Vector3.new(0,YVector,0)
elseif playerMovingDirection == "FrontRight" then
direction = lookVector + rightVector + Vector3.new(0,YVector,0)
elseif playerMovingDirection == "FrontLeft" then
direction = lookVector - rightVector + Vector3.new(0,YVector,0)
elseif playerMovingDirection == "BackRight" then
direction = -lookVector + rightVector + Vector3.new(0,YVector,0)
elseif playerMovingDirection == "BackLeft" then
direction = -lookVector - rightVector + Vector3.new(0,YVector,0)
end
ball.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
ball.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
ball:ApplyImpulse((direction * force) * ball.AssemblyMass)
ball:SetAttribute("LastPlayer", player.Name)
end
m1Event.OnServerEvent:Connect(M1Event)
Local Main Player Script:
local StarterGUI = game:GetService("StarterGui")
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRoot = Character:WaitForChild("HumanoidRootPart")
local FootBallsFolder = game.Workspace:FindFirstChild("FootBalls")
local m1Event = game.ReplicatedStorage.RemoteEvents:FindFirstChild("MouseButton1Event")
repeat task.wait() until Player:HasAppearanceLoaded()
Ring = game.Workspace:FindFirstChild(Character.Name):FindFirstChild("Ring")
Ring.Transparency = 0.9
StarterGUI:SetCore("ResetButtonCallback", false)
StarterGUI:SetCoreGuiEnabled(Enum.CoreGuiType.Health,false)
StarterGUI:SetCoreGuiEnabled(Enum.CoreGuiType.EmotesMenu,false)
StarterGUI:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
StarterGUI:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList,false)
local lastTime = time()
function ValidateFootBall(footBall)
if footBall:IsA("MeshPart") and footBall.Name == "Ball" then
return true
end
end
function ValidateTouchedPart(touchedPart)
if touchedPart ~= nil and touchedPart.Name == "Ring" and touchedPart.Parent:FindFirstChild("Humanoid") and touchedPart.Parent == Character then
return true
end
end
function SetHasTouchedBall(ring,bool)
Ring.Parent:SetAttribute("HasTouchedBall",bool)
end
function OnFootBallTouched(footBall)
footBall.Touched:Connect(function(touchedPart)
if ValidateTouchedPart(touchedPart) then
SetHasTouchedBall(touchedPart,true)
end
end)
end
function OnFootBalTouchEnded(footBall)
footBall.TouchEnded:Connect(function(touchEndedPart)
if ValidateTouchedPart(touchEndedPart) then
SetHasTouchedBall(touchEndedPart,false)
end
end)
end
for i, footBall in pairs(FootBallsFolder:GetChildren()) do
if ValidateFootBall(footBall) then
OnFootBallTouched(footBall)
OnFootBalTouchEnded(footBall)
end
end
FootBallsFolder.ChildAdded:Connect(function(child)
if ValidateFootBall(child) then
OnFootBallTouched(child)
OnFootBalTouchEnded(child)
end
end)
UIS.InputBegan:Connect(function(input,gp)
if gp then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if Character:GetAttribute("HasTouchedBall") then
if (tick() - lastTime) > 0.2 then
lastTime = tick()
local playerMovingDirection = Ring:GetAttribute("CharacterDirection")
local lookVector = HumanoidRoot.CFrame.LookVector
local rightVector = HumanoidRoot.CFrame.RightVector
local camY = Ring:GetAttribute("CameraY")
print(playerMovingDirection,lookVector,rightVector,camY)
m1Event:FireServer(playerMovingDirection,lookVector,rightVector,camY)
end
end
end
end)
Sometimes I get the error:
15:11:08.174 Workspace.Ball.BallController:57: attempt to perform arithmetic (mul) on Vector3 and nil - Studio
15:11:08.174 Stack Begin - Studio
15:11:08.174 Script 'Workspace.Ball.BallController', Line 57 - function M1Event - Studio
15:11:08.174 Stack End - Studio
Also sometimes the movement is not as expected and may go in the wrong direction and the ball seems laggy sometimes.
Please, someone, help me if you can, and also let me know if you know any other methods for achieving this. I am not 100% sure if my method is good for a football Game