Body Velocity not working

I’m making a script so that when you touch a ball with the golf club, and you enter a value, the ball moves but this doesn’t give me any error nor does it work.

Code: Script

local Distance = game.StarterGui.ScreenGui.CalcFrame.Distance.Text
local Height = game.StarterGui.ScreenGui.CalcFrame.Height.Text
local Curve = game.StarterGui.ScreenGui.CalcFrame.Curve.Text

	club.Handle.Touched:Connect(function(hit)
		local golfBall = hit.Parent
		if golfBall then
			local bv = game.Workspace.Ball.BodyVelocity
			game.ReplicatedStorage.DetailsEvent.OnServerEvent:Connect(function()
				bv.Velocity = Vector3.new(Distance, Height, Curve)
			end)
		end
	end)

Local Script:

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.DetailsEvent:FireServer()
end)

This is how to UI is laid out
image

1 Like

You’ll have to get the updated text from the client and send this to the server to be used in the calculation.

How would I do that? Would I use while wait() do?

Nope, grab the new text values in the local script and pass them as arguments when “Firing” the event to the server.

game.ReplicatedStorage.DetailsEvent:FireServer(dist, height, curve)
game.ReplicatedStorage.DetailsEvent.OnServerEvent:Connect(function(player, dist, height, curve)
    if(player.userId ~= myPlayer.userId)then return end --Only the intended player....
    bv.Velocity = Vector3.new(dist, height, curve)
end)
1 Like

It still doesn’t work.
Script:

	club.Handle.Touched:Connect(function(hit)
		local golfBall = hit.Parent
		if golfBall then
			local bv = game.Workspace.Ball.BodyVelocity
			game.ReplicatedStorage.DetailsEvent.OnServerEvent:Connect(function(Distance, Height, Curve)
				bv.Velocity = Vector3.new(Distance, Height, Curve)
			end)
		end
	end)

Local Script:

local Distance = script.Parent.Parent.Distance.Text
local Height = script.Parent.Parent.Height.Text
local Curve = script.Parent.Parent.Curve.Text

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.DetailsEvent:FireServer(Distance, Height, Curve)
end)

Don’t forget about the “player” parameter that is automatically sent to the server.

So I put a ‘player’ parameter for both server and local script?

When you fire the event on the client. Roblox automatically includes a “player” parameter as the first parameter. This tells the server which client/player fired the remote. Review the code sample I posted above it shows that the player parameter isn’t included by you when you fire from the client, but it does show up as the first parameter when handling that event on the server.

image
I’m getting this error.
And this

well “myPlayer” is something you would need to define. I just added that line to demonstrate why you would use the “player” parameter. In this case it looks like the “myPlayer” variable would hold the player who own the “GolfClub”.

So I would do

local myPlayer = GolfClub.Parent

correct?

If the GolfClub is a tool. Then the parent would be the backpack (if not equipped) or the character (if equipped). You want the player. So

(If equipped)

player = game:GetService("Players"):GetPlayerFromCharacter(GolfClub.Parent)
1 Like

image
this error is still there. I did

myPlayer = game:GetService("Players"):GetPlayerFromCharacter(club.Parent)

According to the error your tool is not yet equipped. Its still in the backpack.

GolfClub.Parent = Backpack
GolfClub.Parent.Parent = Player


my tool is equipped but it gave me the same error.

The error indicates the golfClub tool your programming with is still in the backpack. If its in the backpack its not equipped. Could there be more than one tool?

GolfClub is the only tool and it is in the starterPack.

I’d need to see more of your code. Somewhere along the lines the code is firing when the tool is still in the backpack.

This is the layout.

StarterPack, just automatically gives the player the tools. (puts it in the backpack) When you equip the tool, its moved into the character object.

When you attempt to retrieve the player obj. You need to know where the tool is at that time the code is run to get the player.