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