BodyForce issue

Just trying to make the ball go in the mouse’s direction.

The issue is in BodyForce’s force I believe, because the ball does shoot, just not where it’s supposed to, instead it shoots in random directions.
Everything is good, just the BodyForce script isn’t.

I tried to look on the wiki, but I didn’t really find anything that could help, I asked a few people if they would know, but they didn’t, I also experimented for good 3 hours, yet no solution.
I’m sure it’s simple, but I’m just not sure what the problem is.

Here’s my LocalScript calling all the RemoteEvents and everything:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local PlayerGui = player:WaitForChild("PlayerGui")
local InGameGui = PlayerGui:WaitForChild("InGame")
local PowerBarGui = InGameGui:WaitForChild("PowerBar")
local PowerBarFrame = PowerBarGui:WaitForChild("PowerBarFrame")
local PowerFrame = PowerBarFrame:WaitForChild("PowerFrame")
local PowerEvent = game:GetService("ReplicatedStorage"):WaitForChild("PowerEvent")
local PowerEvent2 = game:GetService("ReplicatedStorage"):WaitForChild("PowerEvent2")
local cam = workspace.CurrentCamera
local maxGuiDistance = 1
local maxDistance = 100
local debounce = true

UIS.InputBegan:Connect(function(input, IsTyping)
	if IsTyping then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 and debounce == true then
		local mousePos = mouse.Hit.Position
		debounce = false
		if mousePos then
			local distance1 = (character:WaitForChild("GolfBall").Position - mousePos).Magnitude
			if distance1 > maxDistance then
				PowerEvent2:FireServer(distance1, maxDistance, mousePos)
			elseif distance1 <= maxDistance then
				PowerEvent2:FireServer(distance1, mousePos)
				wait(distance1)
				debounce = true
			end
		end
		--if character.GolfBall:FindFirstChild("BodyForce") then
			--return false
		--elseif not character.GolfBall:FindFirstChild("BodyForce") then
		--end
	end
end)

mouse.Move:Connect(function()
	local mousePos = mouse.Hit.Position
	local distance1 = (character:WaitForChild("GolfBall").Position - mousePos).Magnitude
	if distance1/10 > maxGuiDistance then
		PowerFrame:TweenSize(UDim2.new(1, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5, true)
	elseif distance1/10 <= maxDistance then
		local distance2 = distance1/10
		PowerFrame:TweenSize(UDim2.new(distance2, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5, true)

	end
end)

And this is the server script:

--------------------------------------------VARIABLES--------------------------------------------

local PowerEvent = game:GetService("ReplicatedStorage"):WaitForChild("PowerEvent")
local PowerEvent2 = game:GetService("ReplicatedStorage"):WaitForChild("PowerEvent2")
local bf = Instance.new("BodyForce")

--==============================================================================================--

------------------------------------------FUNCTIONALITY 1------------------------------------------

PowerEvent2.OnServerEvent:Connect(function(player, power, mousePos, mouse)
    local character = player.Character or player.CharacterAdded:Wait()
    local GolfBall = character:WaitForChild("GolfBall")
    force = Vector3.new(mousePos) * power
    print(mousePos)
    local bf = Instance.new("BodyForce")
    bf.Parent = GolfBall
    bf.Force = GolfBall.CFrame:vectorToWorldSpace(mousePos)
    wait(0.5)
    print("after")
    bf:Destroy()
end)

--===============================================================================================--

------------------------------------------FUNCTIONALITY 2------------------------------------------

PowerEvent2.OnServerEvent:Connect(function(player, power, mousePos, mouse)
	local character = player.Character or player.CharacterAdded:Wait()
	local GolfBall = character:WaitForChild("GolfBall")
	force = Vector3.new(mouse.X, 0, ) * power
	print(mousePos)
	bf.Parent = GolfBall
	bf.Force = GolfBall.CFrame:vectorToWorldSpace(mousePos)
	wait(0.5)
	print("after")
	bf.Parent = nil
end)

--===============================================================================================--

I’ve just been stuck on this for a while, any help is highly appreciated.
Thanks in advance!

2 Likes

Just assuming(could be wrong), 1. There is only 2 Variables for Vector3 and 2. The force variable isn’t use anywhere, your just using mousePos

force = Vector3.new(mouse.X, 0, ) * power
	print(mousePos)
	bf.Parent = GolfBall
	bf.Force = GolfBall.CFrame:vectorToWorldSpace(mousePos)
	wait(0.5)
2 Likes

Sorry for late reply, thanks, I’ll see what I can do. Ah yes, I’m not using the force there because force is equal to 0.