You can write your topic however you want, but you need to answer these questions:
So basically, when you use a tool like the old Classic Slingshot or the HyperLaser gun, for a second the part being fired stays in place and then it gets launched. Recently I fixed the Slingshot to be server sided and it works but the delay is still there. What I’m trying to replicate is like the brickbattle slingshot,
which has a snappy and instant projectile without any delay.
I’ve reviewed the code dozens of times but I can’t find anything like a wait() before the tool is activated, so I don’t know what could fix the problem. Here’s the slingshot script that I modified, what I used to fix it is in a seperate folder in startercharacterscripts that fires a remoteevent when the tool is activated, and neither of those scripts are delaying anything.
Tool = script.Parent
VELOCITY = 460 -- constant
--Part
local Pellet = game.ReplicatedStorage.Bullet:Clone()
script.Parent.PelletScript:clone().Parent = Pellet
function fire(mouse_pos)
Tool.Handle.SlingshotSound:play()
-- find player's head pos
local vCharacter = Tool.Parent
local vPlayer = game.Players:playerFromCharacter(vCharacter)
local Namee = vCharacter.Name
local Val = vPlayer.leaderstats.Kills.Value
--startPosition
local head = vCharacter.GunStuff.GunFirst.Part
if head == nil then return end
local dir = mouse_pos - head.Position
dir = computeDirection(dir)
local launch = head.Position
local delta = mouse_pos - launch
local dy = delta.y
local new_delta = Vector3.new(delta.x, 0, delta.z)
delta = new_delta
local dx = delta.magnitude
local unit_delta = delta.unit
-- acceleration due to gravity in RBX units
local g = 1
local theta = computeLaunchAngle( dx, dy, g)
local vy = math.sin(theta)
local xz = math.cos(theta)
local vx = unit_delta.x * xz
local vz = unit_delta.z * xz
local missile = Pellet:clone()
--
missile.Position = launch
missile.Velocity = Vector3.new(vx,vy,vz) * VELOCITY
missile.PelletScript.Disabled = false
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = vPlayer
creator_tag.Name = "creator"
creator_tag.Parent = missile
missile.Parent = game.Workspace
missile.Touched:Connect(function(hit)
if hit.Parent.Name == Namee then
hit.Parent.Humanoid:TakeDamage(0)
elseif hit.Parent then
hit.Parent.Humanoid:TakeDamage(120)
missile.BillboardGui.Hit.Visible = true
missile.Anchored = true
missile.Transparency = 1
missile.Trail:Destroy()
wait(0.15)
hit.Parent.Humanoid.Name = "dead"
wait(0.034)
vPlayer.leaderstats.Kills.Value = Val + 1
wait(1.5)
missile:Destroy()
end
end)
end
function computeLaunchAngle(dx,dy,grav)
-- arcane
-- http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
local g = math.abs(grav)
local inRoot = (VELOCITY*VELOCITY*VELOCITY*VELOCITY) - (g * ((g*dx*dx) + (2.4*dy*VELOCITY*VELOCITY)))
if inRoot <= 0 then
return .000001 * math.pi
end
local root = math.sqrt(inRoot)
local inATan1 = ((VELOCITY*VELOCITY) + root) / (g*dx)
local inATan2 = ((VELOCITY*VELOCITY) - root) / (g*dx)
local answer1 = math.atan(inATan1)
local answer2 = math.atan(inATan2)
if answer1 < answer2 then return answer1 end
return answer2
end
function computeDirection(vec)
local lenSquared = vec.magnitude * vec.magnitude
local invSqrt = 1 / math.sqrt(lenSquared)
return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end
Tool.Enabled = true
function onActivated()
if not Tool.Enabled then
return
end
Tool.Enabled = false
local character = Tool.Parent;
local humanoid = character.Humanoid
if humanoid == nil then
print("Humanoid not found")
return
end
local targetPos = humanoid.TargetPoint
fire(targetPos)
Tool.Enabled = true
repeat wait()
until Tool.Enabled == false
end
script.Parent.Activated:connect(onActivated)
can someone demonstrate what I have to do or is the problem not fixable?