Im trying to create a football click system pls help on how i do it

  1. What do you want to achieve? Keep it simple and clear!

ive recently make a football system where if the player touch the ball the ball shoots,
but i want to make it so that when a player is currently touching the ball/dribbling
could click his mouse so the ball shoots.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have looked and tryed to find a solution, i tryed using player.name and other stuff
but it didnt work.
So i really want to know on how i could make it.

Code:

local ball = script.Parent
local value = script.Parent.Value

function onTouched(otherPart)
	
	if otherPart.Parent:FindFirstChild("Humanoid") then
		
		local humanoidRootPart = otherPart.Parent:FindFirstChild("HumanoidRootPart")
		
		local forceDirection = (humanoidRootPart.Position - ball.Position).unit
		forceDirection = Vector3.new(forceDirection.x, value.Value, forceDirection.z)
		
		local forceMagnitude = -50
		
		ball.Velocity = forceDirection * forceMagnitude
	end
end

ball.Touched:Connect(onTouched)

5 Likes

Not my most beautiful attempt but give something like this a try in a localscript.

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local MouseEvent
local ball = game:GetService("Workspace"):WaitForChild("Part")
local value = --Get Value

local function onButton1Down()
	--Mouse Button Clicked, Shoot Ball, Disconnect mouse.Button1Down Event
	MouseEvent:Disconnect()
	MouseEvent = nil
	print("Disconnecting Mouse Event")
	local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
	local forceDirection = (humanoidRootPart.Position - ball.Position).unit
	forceDirection = Vector3.new(forceDirection.x, value.Value, forceDirection.z)

	local forceMagnitude = -50

	ball.Velocity = forceDirection * forceMagnitude
end

function onTouched(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") then
		
		if MouseEvent == nil then
			MouseEvent = mouse.Button1Down:Connect(onButton1Down)
			print("Mouse scanning...")
		end
		
	end
end

ball.Touched:Connect(onTouched)

but could it be on server script because its supposed to be a team game?

2 Likes

Yes in the onButton1Down() function you can use a remote event then do the rest in a server script however player:GetMouse() only seems to work in localscripts.

3 Likes

ill try that hopefully it works

2 Likes

what i meant by my post was when the player touch the ball the player has the ability to shoot with mouse button down but when like touched it shooted so its the same thing

my code was just the touched and what i want was like the player could shoot the ball with clicking after touching it

Use this in a localscript placed in StarterPlayer > StarterPlayerScripts to detect mouseclicks on the clientside (Remember to create a RemoteEvent in ReplicatedStorage named “ServerEvent” and to set local ball = --Balls Location In Workspace)

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local ball = game:GetService("Workspace"):WaitForChild("Part") --Set this to ball in workspace

local ServerEvent = game:GetService("ReplicatedStorage"):WaitForChild("ServerEvent") --Create Remote Event In ReplicatedStorage Named ServerEvent
local MouseEvent

local function onButton1Down()
	--Mouse Button Clicked, Shoot Ball, Disconnect mouse.Button1Down Event
	MouseEvent:Disconnect()
	MouseEvent = nil
	
	--Fire RemoteEvent To Server
	ServerEvent:FireServer()
end

function onTouched(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") then
		if MouseEvent == nil then
			MouseEvent = mouse.Button1Down:Connect(onButton1Down)
		end
	end
end

ball.Touched:Connect(onTouched)

Use this in a server script like you were before

local ServerEvent = game:GetService("ReplicatedStorage"):WaitForChild("ServerEvent")
local ball = script.Parent
local value = script.Parent.Value

local function ServerEventFunc(player)
	local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
	local forceDirection = (humanoidRootPart.Position - ball.Position).unit
	forceDirection = Vector3.new(forceDirection.x, value.Value, forceDirection.z)

	local forceMagnitude = -50

	ball.Velocity = forceDirection * forceMagnitude
end

ServerEvent.OnServerEvent:Connect(ServerEventFunc)
2 Likes

thanks it work but how do add range of click do i need to use raycast?

Do you mean you want players to only be able to click and shoot the ball from a certain distance away?

yes so that the player that touches it couldnt just shoot the ball when the player that touched it is far away

Replacing local function onButton1Down() with this should work

local function onButton1Down()
	--Mouse Button Clicked, Shoot Ball, Disconnect mouse.Button1Down Event
	local Mag = (character:WaitForChild("HumanoidRootPart").Position - ball.Position).Magnitude
	if Mag <= 12 then --12 is the distance player is from the ball
		--Still Close Enough To Shoot Ball So Disconnect And Send ServerEvent
		MouseEvent:Disconnect()
		MouseEvent = nil
		
		--Fire RemoteEvent To Server
		ServerEvent:FireServer()
	else
		--Too Far Away Quit Listening For Mouse Clicks 
		--(Note You Will Have To Touch Ball Again To Start Listening For MouseClicks Again)
		MouseEvent:Disconnect()
		MouseEvent = nil
	end
end
2 Likes

nice man thats what i want thank you so much man, you dont need to make full scripts but ok ill take that thankss

1 Like

I haven’t worked with mouse clicks on roblox yet so I just took it as an opportunity to familiarize myself with it. =p

yeah thats right, for me its magnitude its my first time knowing how to use it now

nice man

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.