How would i add physics to this code?

hey so basically im working on an RPG system (i came back 2 scripting on roblox wooohooo) and the thing is, i want it to be realistic BUT it goes whenever i point and click it goes to where i pressed, but i want it to like, require me to aim up, so if i aim for the same place, it will kind of fall, and then explode in the wrong place so i have to aim correctly. idk how to do that though, help would be appreciated


local tool = script.Parent
local replicatedStorage = game:GetService("ReplicatedStorage")

local createPartEvent = Instance.new("RemoteEvent")
createPartEvent.Name = "CreatePartEvent"
createPartEvent.Parent = replicatedStorage

local function calculateDistance(pointA, pointB)
	return (pointA - pointB).magnitude
end

local function calculateTravelTime(distance, speed)
	return distance / speed
end

local function createPart(player, clickedPosition)
	local character = player.Character
	if character then
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if humanoidRootPart then
			-- Calculate the distance and travel time
			local distance = calculateDistance(humanoidRootPart.Position, clickedPosition)
			local speed = 350  -- Set the speed of the part (studs per second)
			local travelTime = calculateTravelTime(distance, speed)
			wait(travelTime)
			local explosion = Instance.new("Explosion")
			explosion.BlastRadius = 50
			explosion.Position = clickedPosition
			explosion.Parent = workspace
			local soundPart = Instance.new("Part", workspace)
			soundPart.Anchored = true
			soundPart.Transparency = 1
			soundPart.CanCollide = false
			soundPart.Position = clickedPosition
			local NewSound = Instance.new("Sound", soundPart)
			NewSound.SoundId = "rbxassetid://137743235617417"
			NewSound:Play()
			wait(0.2)
			local part = game.ReplicatedStorage.FirePart:Clone()
			part.Parent = game.Workspace
			part.Anchored = true
			part.CanCollide = false
			part.Transparency = 1
			part.Position = clickedPosition
			local otherPart = Instance.new("Part", part)
			otherPart.Position = clickedPosition
			otherPart.Anchored = true
			otherPart.CanCollide = false
			otherPart.Transparency = 1
			wait(20)
			part:Destroy()
		end
	end
end

createPartEvent.OnServerEvent:Connect(createPart)

createPartEvent.OnServerEvent:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	if char then
		local tool = player.Character:FindFirstChild("RocketLauncher")
		if tool then
			tool:FindFirstChild("Model"):FindFirstChild("Grenade").Transparency = 1
			tool:FindFirstChild("Model"):FindFirstChild("Grenade"):FindFirstChild("ParticleEmitter").Enabled = true
			wait(5)
			tool:FindFirstChild("Model"):FindFirstChild("Grenade"):FindFirstChild("ParticleEmitter").Enabled = false
	end
	end
end)

game.ReplicatedStorage.Reload.OnServerEvent:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	if char then
		local tool = player.Character:FindFirstChild("RocketLauncher")
		if tool then
			tool:FindFirstChild("Model"):FindFirstChild("Grenade").Transparency = 0
		end
	end
end)
1 Like

You should try using a BodyVelocity

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.