Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a BodyVelocity send an object towards the mouse?

Recently I've been trying to make a function that shoots an object towards a players mouse. It used to work fine in local scripts, but now, when I send the Mouse.Hit across to the server and try to make something move towards it it no longer works. I've tried many things but I always get the same result. The object just ends up flying somewhere else, no mouse input detected.

Here's the code:

**Client Side

plr = game.Players.LocalPlayer
repeat wait() until plr.Character~= nil
m = plr:GetMouse()
loaded = true

local mousep = m.Hit

m.KeyDown:connect(function(key)
	key = key:lower()
	if key == "x" and loaded == true then
		loaded = false	
			workspace.Events.GravityRock:FireServer(mousep)
		wait(8)
		loaded = true
	end
end)

**Server Side

workspace.Events.GravityRock.OnServerEvent:Connect(function(plr, mousep)
 local anim = plr.Character.Humanoid:LoadAnimation(script.GravityRock)
anim:Play()

rock1.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(25,-4,0)
rock2.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(-25,-4,0)
rock3.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,-4,25)
rock4.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,-4,-25)

wait(0.3)

rock1.Anchored = false
rock2.Anchored = false
rock3.Anchored = false
rock4.Anchored = false


local bv = Instance.new("BodyVelocity")
  bv.MaxForce = Vector3.new(1e8,1e8,1e8)
  bv.Velocity = Vector3.new(0,80,0)
  bv.Parent = rock1
local bv2 = Instance.new("BodyVelocity")
  bv2.MaxForce = Vector3.new(1e8,1e8,1e8)
  bv2.Velocity = Vector3.new(0,80,0)
  bv2.Parent = rock2
local bv3 = Instance.new("BodyVelocity")
  bv3.MaxForce = Vector3.new(1e8,1e8,1e8)
  bv3.Velocity = Vector3.new(0,80,0)
  bv3.Parent = rock3
local bv4 = Instance.new("BodyVelocity")
  bv4.MaxForce = Vector3.new(1e8,1e8,1e8)
  bv4.Velocity = Vector3.new(0,80,0)
  bv4.Parent = rock4

wait(0.8)

bv:remove()
bv2:Remove()
bv3:remove()
bv4:remove()


local bv = Instance.new("BodyVelocity")
  bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
  bv.Velocity = mousep.lookVector * 50
  bv.Parent = rock1
local bv2 = Instance.new("BodyVelocity")
   bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
  bv.Velocity = mousep.lookVector * 50
  bv2.Parent = rock2
local bv3 = Instance.new("BodyVelocity")
  bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
  bv.Velocity = mousep.lookVector * 50
  bv3.Parent = rock3
local bv4 = Instance.new("BodyVelocity")
   bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
  bv.Velocity = mousep.lookVector * 50
  bv4.Parent = rock4


You can see here that on the Server Side, the rocks/objects are generated beneath the player, then fly up into the air, and then after that I insert new a BodyVelocity into the rocks that is supposed to follow the mouse. The problem is, it does no such thing.

2 answers

✓ Accepted answer

I think I just figured it out. You assigned the m.Hit, right before the event. This variable will never change, as it is set once there.

I changed your local script, give this a shot and let me know if you need further help:

local plr = game:GetService("Players").LocalPlayer
local ui = game:GetService("UserInputService")
repeat wait() until plr.Character~= nil
local m = plr:GetMouse()
local loaded = true

ui.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.X then
        loaded = false
        workspace:WaitForChild("Events").GravityRock:FireServer(m.Hit)
        wait(8)
        loaded = true
    end
end)

You have a few mistakes in your code: you forgot to add local before your variables in your localscript you didnt add an end) in the end of your server script when making the player load an animation in the server side script you didnt take the animationid not a mistake but using userinputservice is better to detect inputs you also didnt have the variables for the rocks setup, but im assuming you already had them from before

i didnt test it but here's what a fixed version of both scripts would look like i think:

** Client Sided

local plr = game:GetService("Players").LocalPlayer
local ui = game:GetService("UserInputService")
repeat wait() until plr.Character~= nil
local m = plr:GetMouse()
local loaded = true

local mousep = m.Hit

ui.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.X then
		loaded = false
		workspace:WaitForChild("Events").GravityRock:FireServer(mousep)
		wait(8)
		loaded = true
	end
end)

** Server Sided

workspace:WaitForChild("Events").GravityRock.OnServerEvent:Connect(function(plr, mousep)
 local anim = plr.Character.Humanoid:LoadAnimation(script.GravityRock.AnimationId)
anim:Play()

if plr.Character:FindFirstChild("HumanoidRootPart") then
	rock1.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(25,-4,0)
	rock2.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(-25,-4,0)
	rock3.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,-4,25)
	rock4.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,-4,-25)
end

wait(0.3)

rock1.Anchored = false
rock2.Anchored = false
rock3.Anchored = false
rock4.Anchored = false


local bv = Instance.new("BodyVelocity")
  bv.MaxForce = Vector3.new(1e8,1e8,1e8)
  bv.Velocity = Vector3.new(0,80,0)
  bv.Parent = rock1
local bv2 = Instance.new("BodyVelocity")
  bv2.MaxForce = Vector3.new(1e8,1e8,1e8)
  bv2.Velocity = Vector3.new(0,80,0)
  bv2.Parent = rock2
local bv3 = Instance.new("BodyVelocity")
  bv3.MaxForce = Vector3.new(1e8,1e8,1e8)
  bv3.Velocity = Vector3.new(0,80,0)
  bv3.Parent = rock3
local bv4 = Instance.new("BodyVelocity")
  bv4.MaxForce = Vector3.new(1e8,1e8,1e8)
  bv4.Velocity = Vector3.new(0,80,0)
  bv4.Parent = rock4

wait(0.8)

bv:remove()
bv2:Remove()
bv3:remove()
bv4:remove()


local bv = Instance.new("BodyVelocity")
  bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
  bv.Velocity = mousep.lookVector * 50
  bv.Parent = rock1
local bv2 = Instance.new("BodyVelocity")
   bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
  bv.Velocity = mousep.lookVector * 50
  bv2.Parent = rock2
local bv3 = Instance.new("BodyVelocity")
  bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
  bv.Velocity = mousep.lookVector * 50
  bv3.Parent = rock3
local bv4 = Instance.new("BodyVelocity")
   bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
  bv.Velocity = mousep.lookVector * 50
  bv4.Parent = rock4
end)

1 comment

0

Thanks, but unfortunately the rocks still don't go towards the mouse. They just kind of drop wherever.

This discussion is preserved as it was. New answers, comments, and votes are closed.