Heya y’all!
I’ve been working on a prototype to hopefully get better with the physics in Roblox, and making parts throwable. Initially, I wanted to make the part be thrown in the direction the player’s mouse is looking at. But I keep getting an error and I don’t necessarily know how to fix this.
Any help is greatly appreciated and I hope y’all have a great day!!!
Script:
local RS = game:GetService("ReplicatedStorage")
local ThrowEvent = RS.Events.ThrowPart
ThrowEvent.OnServerEvent:Connect(function(player, Tool)
local Char = player.Character
local Mouse = player:GetMouse()
local ClonePart = RS.Parts:FindFirstChild(Tool.Name):Clone()
ClonePart.Size = Vector3.new(1,1,1) * Tool.Size.Value
ClonePart.Parent = workspace.Parts
--local pos1 = workspace.From.Position
--local pos2 = workspace.To.Position
local pos1 = Char.Head.Position
local pos2 = Mouse.Hit.Position
local dir = pos2 - pos1
local dur = math.log(1.001 + dir.Magnitude * 0.01)
pos2 = Char.HumanoidRootPart.CFrame.LookVector * dur
dir = pos2 - pos1
local force = dir / dur + Vector3.new(0, game.Workspace.Gravity * dur / 2, 0)
ClonePart.Position = pos1
--ClonePart.AssemblyLinearVelocity = force
ClonePart:ApplyImpulse(force * ClonePart.AssemblyMass)
ClonePart:SetNetworkOwner(nil)
Tool:Destroy()
end)
Player.Mouse can only be read from by the client
You’ll have to fire Hit as an argument in the :FireServer
Forgot to mention, Mouse is depreciated
You should be manually raycasting with Camera:ViewportPointToRay and UserInputService functions
local UserInputService = game:GetService('UserInputService')
local function GetMousePosition()
local Camera = workspace.CurrentCamera
local MouseLocation = UserInputService:GetMouseLocation()
local MouseRay = Camera:ViewportPointToRay(MouseLocation.X, MouseLocation.Y, 1)
local Result = workspace:Raycast(MouseRay.Origin, MouseRay.Direction * 2500)
if Result then
return Result.Position
else
return MouseRay.Origin + MouseRay.Direction * 2500
end
end
You can also construct a CFrame with CFrame.lookAt
So I typed in this function into the local script that fires the ThrowEvent and added a variable into the event function for the mouse’s position. My problem now is the sometimes the part will be thrown the opposite direction of where the mouse is.
ThrowEvent.OnServerEvent:Connect(function(player, Tool, MousePos)
local Char = player.Character
local ClonePart = RS.Parts:FindFirstChild(Tool.Name):Clone()
ClonePart.Size = Vector3.new(1,1,1) * Tool.Size.Value
ClonePart.Parent = workspace.Parts
--local pos1 = workspace.From.Position
--local pos2 = workspace.To.Position
local pos1 = Char.Head.Position
local pos2 = MousePos
local dir = pos2 - pos1
local dur = math.log(1.001 + dir.Magnitude * 0.01)
pos2 = MousePos * dur
dir = pos2 - pos1
local force = dir / dur + Vector3.new(0, game.Workspace.Gravity * dur / 2, 0)
ClonePart.Position = pos1
--ClonePart.AssemblyLinearVelocity = force
ClonePart:ApplyImpulse(force * ClonePart.AssemblyMass)
ClonePart:SetNetworkOwner(nil)
Tool:Destroy()
end)