Movement is glitchy/or goes in the wrong direction while dribbling Foot Ball /Best method to create a dribbling system for football game?

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

3 Likes

The error you’re encountering is related to line 57 of the “Ball Controller Script” you provided. The error message suggests that you’re trying to perform arithmetic multiplication (*) between a Vector3 and nil value.

To fix this issue, you need to identify which part of your code is causing the problem and make sure that all the necessary values are properly assigned. It seems like the issue occurs when attempting to calculate the impulse force for the ball based on the player’s input.

One possible cause could be that the camY value is not set correctly or is not being passed as expected to the M1Event function. You should double-check how camY is being calculated and ensure it’s always assigned a valid value.

Additionally, you could add some print statements to debug and track the values of camY, playerMovingDirection, lookVector, and rightVector in the M1Event function. This will help you identify any unexpected behavior or incorrect values that might be causing the issues with movement and lag.

Regarding other methods for achieving the desired functionality in a football game, it depends on the specific requirements and mechanics you’re aiming for. The code you provided seems to handle basic movement based on player input, but there might be alternative approaches or improvements depending on your game’s design. Could you provide more information about the desired gameplay mechanics or any specific issues you’re facing?

1 Like

not likely a chat gpt reply.

well I tried looking, don’t seem to find any solution :frowning:

1 Like

Could you provide more information about the desired gameplay mechanics or any specific issues you’re facing?

2 Likes

like when you click m1 and when touched the ball gotta go in the dir they facing and dire they are moving and there should be no lag.

2 Likes

This error occurs when you try to multiply a Vector3 value with nil, which is not allowed.

Looking at your code, the error seems to occur in the line:

ball:ApplyImpulse((direction * force) * ball.AssemblyMass)

To fix this error, you need to ensure that the direction variable is not nil. You can add a check to handle the scenario where direction is nil:

if direction then
    ball:ApplyImpulse((direction * force) * ball.AssemblyMass)
end

Regarding the movement not being as expected and the ball feeling laggy, there could be several factors contributing to this issue.

2 Likes

ok well that’s okay but what about the ball going in some other dir sometimes

2 Likes

Can you provide a video of what occurs?

1 Like

well i will take a video and send but do you have any proper methods other than this for creating a dribble system according to my needs and a better approch?

1 Like

does anyone else have a better solution? :slight_smile:

1 Like