I have this assault rifle code, and I'm wonder how to go about making it so no matter what, there holding this tools and It will be functional. Would something as simple as unequipped = false work? or will it be more complex.Here is the beginning gun code:
ToolName="Assault Rifle"
ClipSize=30
ReloadTime=2
Firerate=.08
MinSpread=0.15
MaxSpread=0.15
SpreadRate=0.2
BaseDamage= 31
automatic=true
burst=false
shot=false --Shotgun
BarrlePos=Vector3.new(-2.5,.60,0)
Cursors={"rbxasset://textures\\GunCursor.png"}
ReloadCursor="rbxasset://textures\\GunWaitCursor.png"
-------------------------------------
equiped=false
sp=script.Parent
RayLength=1000
Spread=0.15
enabled=true
reloading=false
down=false
r=game:service("RunService")
last=0
last2=0
last3=0
last4=0
last5=0
last6=0
Bullet=Instance.new("Part")
Bullet.Name="Bullet"
Bullet.BrickColor=BrickColor.new("New Yeller")
Bullet.Anchored=true
Bullet.CanCollide=false
Bullet.Locked=true
Bullet.Size=Vector3.new(1,1,1)
--Bullet.Transparency=.65
Bullet.formFactor=0
Bullet.TopSurface=0
Bullet.BottomSurface=0
mesh=Instance.new("SpecialMesh")
mesh.Parent=Bullet
mesh.MeshType="Brick"
mesh.Name="Mesh"
mesh.Scale=Vector3.new(.15,.15,1)
function check()
sp.Name=ToolName.." ["..tostring(sp.Ammo.Value).."]"
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
------------------------------------------------------------------------------------Raycasting functions
function cross(vector1, vector2)
return Vector3.new(vector1.y * vector2.z - vector2.y * vector1.z, vector1.z * vector2.x - vector1.x * vector2.z, vector1.x * vector2.y - vector2.x * vector1.y)
end
function dot(vector1, vector2)
return (vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z)
end
function getLineSphereCollide(linePoint1, lineVector, sphereCenter, radius)
local a = lineVector.x * lineVector.x + lineVector.y * lineVector.y + lineVector.z * lineVector.z
local b = lineVector.x * (linePoint1.x - sphereCenter.x) + lineVector.y * (linePoint1.y - sphereCenter.y) + lineVector.z * (linePoint1.z - sphereCenter.z)
local c = (linePoint1.x - sphereCenter.x) * (linePoint1.x - sphereCenter.x) + (linePoint1.y - sphereCenter.y) * (linePoint1.y - sphereCenter.y) + (linePoint1.z - sphereCenter.z) * (linePoint1.z - sphereCenter.z) - radius * radius
if (a > 0) and (b * b >= a * c) then
local diff = math.sqrt(b * b - a * c)
return ((-b - diff) / a), ((diff - b) / a)
else
return -1, -1
end
end
--Returns hit, position, normal, time
function raycast(model, start, vector, brickFunction)
local hit, normal, time = raycastRecursive(model, start, vector, brickFunction, vector.unit, dot(start, vector.unit))
if (dot(normal, vector) > 0) then
normal = -normal
end
return hit, start + time * vector, normal.unit, time
end
function raycastRecursive(model, start, vector, brickFunction, unitVec, startDist)
if (model.className == "Part") or (model.className == "Seat") or (model.className =="SpawnLocation") then
local range = model.Size.magnitude / 2
local dist = dot(model.Position, unitVec) - startDist
if (dist + range > 0) and (dist - range < vector.magnitude) and ((dist * unitVec + start - model.Position).magnitude < range) and brickFunction(model) then
local halfSize = model.Size / 2
if (model.Shape == Enum.PartType.Ball) then
local time, timeMax = getLineSphereCollide(start, vector, model.Position, halfSize.x)
if (time < 1) and (time >= 0) then
return model, (time * vector + start - model.Position), time
else
return nil, Vector3.new(0, 0, 0), 1
end
elseif (model.Shape == Enum.PartType.Block) then
local time = 1
local cf = model.CFrame - model.Position
local xvec = cf * Vector3.new(1, 0, 0)
local yvec = cf * Vector3.new(0, 1, 0)
local zvec = cf * Vector3.new(0, 0, 1)
local xspd = -dot(xvec, vector)
local yspd = -dot(yvec, vector)
local zspd = -dot(zvec, vector)
local xmin, xmax, ymin, ymax, zmin, zmax = -1
local dotProd = dot(xvec, start - model.Position)
if (xspd ~= 0) then
xmin = (dotProd - halfSize.x) / xspd
xmax = (dotProd + halfSize.x) / xspd
if (xmax < xmin) then
local swap = xmin
xmin = xmax
xmax = swap
end
else
if (math.abs(dotProd) < halfSize.x) then
xmax = 1
xmin = 0
else
return nil, Vector3.new(0, 0, 0), 1
end
end
local dotProd = dot(yvec, start - model.Position)
if (yspd ~= 0) then
ymin = (dotProd - halfSize.y) / yspd
ymax = (dotProd + halfSize.y) / yspd
if (ymax < ymin) then
local swap = ymin
ymin = ymax
ymax = swap
end
else
if (math.abs(dotProd) < halfSize.y) then
ymax = 1
ymin = 0
else
return nil, Vector3.new(0, 0, 0), 1
end
end
local dotProd = dot(zvec, start - model.Position)
if (zspd ~= 0) then
zmin = (dotProd - halfSize.z) / zspd
zmax = (dotProd + halfSize.z) / zspd
if (zmax < zmin) then
local swap = zmin
zmin = zmax
zmax = swap
end
else
if (math.abs(dotProd) < halfSize.z) then
zmax = 1
zmin = 0
else
return nil, Vector3.new(0, 0, 0), 1
end
end
if (xmin <= ymax) and (xmax >= ymin) and (xmin <= zmax) and (xmax >= zmin) and (zmin <= ymax) and (zmax >= ymin) then
local normal = xvec
local min = xmin
if (ymin > min) then
min = ymin
normal = yvec
end
if (zmin > min) then
min = zmin
normal = zvec
end
if (min >= 0) and (min < 1) then
time = min
elseif (xmax > 0) and (ymax > 0) and (zmax > 0) and (min < 0) then
time = 0
normal = Vector3.new(0, 0, 0)
end
return model, normal, time
else
return nil, Vector3.new(0, 0, 0), 1
end
else -- Cylinder
local time = 1
local cf = model.CFrame - model.Position
local xvec = cf * Vector3.new(1, 0, 0)
local xspd = -dot(xvec, vector)
local xmin, xmax = -1
local dotProd = dot(xvec, start - model.Position)
if (xspd ~= 0) then
xmin = (dotProd - halfSize.x) / xspd
xmax = (dotProd + halfSize.x) / xspd
if (xmax < xmin) then
local swap = xmin
xmin = xmax
xmax = swap
end
else
if (math.abs(dotProd) < halfSize.x) then
xmax = 1
xmin = 0
else
return nil, Vector3.new(0, 0, 0), 1
end
end
local relVec = cf:pointToObjectSpace(vector) * Vector3.new(0, 1, 1)
local relPos = model.CFrame:pointToObjectSpace(start) * Vector3.new(0, 1, 1)
local rmin, rmax = getLineSphereCollide(relPos, relVec, Vector3.new(0, 0, 0), halfSize.y)
if (xmin <= rmax) and (xmax >= rmin) and (rmax > 0) then
local normal = xvec
local min = xmin
if (rmin > min) then
min = rmin
normal = cf * (relPos + relVec * min)
end
if (min >= 0) and (min < 1) then
time = min
elseif (xmax > 0) and (rmax > 0) and (min < 0) then
time = 0
normal = Vector3.new(0, 0, 0)
end
return model, normal, time
else
return nil, Vector3.new(0, 0, 0), 1
end
return nil, Vector3.new(0, 0, 0), 1
end
end
return nil, Vector3.new(0, 0, 0), 1
elseif (model.className=="Model") or (model.className=="Workspace") or (model.className=="Hat") or (model.className == "Tool") then
local children=model:GetChildren()
function tagHumanoid(humanoid)
local plr=game.Players:playerFromCharacter(sp.Parent)
if plr~=nil then
local tag=Instance.new("ObjectValue")
tag.Value=plr
tag.Name="creator"
tag.Parent=humanoid
delay(2,function()
if tag~=nil then
tag.Parent=nil
end
end)
end
end
