Making a chargeable projectile

Hey there, I have been trying to work on a grenade projectile to make it chargeable, for example, the main velocity is 100, that is the default. But I want the velocity to be changed when the grenade has been held for a certain time. Example, if it’s held for .5 seconds then the velocity will now be 200.

–Serverscript

local hold; local throw; local reload

script.Parent.GrenadeEvent.OnServerEvent:Connect(function(player, mouseDirection, holdTime, GrenadeType)
	local char = player.Character
	local humanoid = char:FindFirstChild("Humanoid")
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	
	if GrenadeType == "Hold" then
		-- Get Animations
		script.Parent.Handle.Armed:Play()
		if not hold then hold = humanoid:LoadAnimation(script.Hold) end
		hold:Play()
	elseif GrenadeType == "Throw" then
		script.Parent.Handle.Throw:Play()
		if not hold then hold = humanoid:LoadAnimation(script.Hold) end
		hold:Stop()
		if not throw then throw = humanoid:LoadAnimation(script.Throw) end
		throw:Play()
		wait(.15)
		script.Parent.Handle.Transparency = 1
		-- Making the grenade
		local clonedGrenade = script.Parent.Handle:Clone()
		clonedGrenade.Transparency = 0
		clonedGrenade.CanCollide = true
		clonedGrenade.Parent = workspace
		clonedGrenade.CFrame = CFrame.new(script.Parent.Handle.Position,mouseDirection)
		clonedGrenade.Velocity = clonedGrenade.CFrame.lookVector * 100
		

		local player = game.Players.LocalPlayer
		local mouse = player:GetMouse()

		local held = false
		mouse.Button1Down:connect(function()
			held = true
			print("left mouse button held")
			wait(0.5)
		clonedGrenade.Velocity = clonedGrenade.CFrame.LookVector * 5000
		end)

		mouse.Button1Up:connect(function()
			held = false
			print("left mouse button pressed")
		end)

		spawn(function()
			while true do
				wait(.1)
				print(held)
			end
		end)
		

		
		wait(9999-holdTime)
		-- Make an explosion
		--clonedGrenade.Transparency = 1
		--local explosion = Instance.new("Explosion",workspace)
		--explosion.Position = clonedGrenade.Position
		--explosion.BlastRadius = 25

		local explosionSFX = script.Parent.Handle.Explosion:Clone()
		explosionSFX.Parent = clonedGrenade
		explosionSFX:Play()
		wait(5)
		clonedGrenade:Destroy()
	elseif GrenadeType == "Explode" then
		hold:Stop()
		script.Parent.Handle.Transparency = 1
		local explosion = Instance.new("Explosion",workspace)
		explosion.DestroyJointRadiusPercent = 0
		explosion.BlastRadius = 25
		explosion.Position = script.Parent.Handle.Position
		script.Parent.Handle.Explosion:Play()
		humanoid:TakeDamage(0)
		humanoid.WalkSpeed = 0
	elseif GrenadeType == "Reload" then
		if not reload then reload = humanoid:LoadAnimation(script.Reload) end
		reload:Play()
		wait(.8)
		script.Parent.Handle.Transparency = 0
		humanoid.WalkSpeed = 16
	end
end)

script.Parent.Unequipped:Connect(function()
	if hold then hold:Stop() end; if throw then throw:Stop() end; if reload then reload:Stop() end
end)

If you can give feedback please help, :slight_smile:

1 Like

It’s alright, when we are new, we do stuff like this!

Use UIS and count how long does the left-mouse button has been held(timeHeld). Then, parse the timeHeld amount to the server.

1 Like

I already have a detection for when player holds a mouse or clicks it immediately, also the time that the player has held. I just need to know how I would be able to change the velocity after a certain time, if you know how I can do this please message me, also thanks for the feedback!

1 Like

Wouldn’t this work?

clonedGrenade.AssemblyLinearVelocity = clonedGrenade.CFrame.LookVector * (timeHeld * basicVelocity)

*edit
I noticed that you didn’t use any Remote. :0

Try #help-and-feedback:scripting-support instead of #help-and-feedback:creations-feedback :wink:

What does that mean? Can you help? She’s not asking for random motivations, but scripting support…

Earlier I said this was my first post and I didn’t know where to place this post in the topics section

I replied to the dev doge not you don’t worry

I just gave you a quicker answer

How would I implement this in the script?

1 Like