Trying to make a projectile throwing system for a grenade, but after attempting to compensate for the games gravity the throwing has broken entirely.
Currently I am firing a server event with the mouse’s position on the client, then spawning and throwing the projectile on the server.
Local script:
local plr = game.Players.LocalPlayer
local char = plr.Character
local root = char:WaitForChild("HumanoidRootPart")
local mouse = plr:GetMouse()
local US = game:GetService("UserInputService")
local inputEvent = script.Parent:WaitForChild("Input")
US.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mousePosition = mouse.Hit.Position
inputEvent:FireServer(mousePosition, root.Position)
end
end)
Server script:
local inputEvent = script:WaitForChild("Input")
local projectile = game.ReplicatedStorage:WaitForChild("Ball")
inputEvent.OnServerEvent:Connect(function(plr, mousePos, rootPos)
local direction = mousePos - rootPos
local force = direction * Vector3.new(0, workspace.Gravity * .5, 0)
local clone = projectile:Clone()
clone.Position = rootPos
clone.Parent = workspace
clone:ApplyImpulse(force * clone.AssemblyMass)
end)
