What error i get is attempt to index upvalue 'target' (a nil value) it makes the character reflectance random but doesn't make it go back the hell ? Ignore the other lines, but the math random lines and the last one with reflectance ? Why does the first 6 lines of math random work but others on the end not ? example:
local sp = script.Parent
local turret = sp.Gun
local target = nil
local ready = true
local targettable = {}
local set = require(script.Settings)
game:GetService('RunService').Stepped:connect(function()
targettable = {}
for _,plr in pairs(game.Players:GetChildren()) do
if plr:DistanceFromCharacter(turret.Position) <= set.radius and plr.TeamColor ~= set.allycolor and plr.Character.Humanoid.Health >0 then
table.insert(targettable,plr)
else
target = nil
targettable = {}
end
end
if table.getn(targettable)>0 then
target = targettable[math.random(1,#targettable)]
elseif table.getn(targettable) == 0 then
target = nil
end
if target ~= nil then
if ready == true then
ready = false
Laser(turret.Position,target.Character.Torso.Position)
target.Character.Humanoid:TakeDamage(set.damage)
target.Character:findFirstChild("Head").Reflectance =(math.random(0.2,1))
target.Character:findFirstChild("Torso").Reflectance =(math.random(0.2,1))
target.Character:findFirstChild("Right Arm").Reflectance =(math.random(0.2,1))
target.Character:findFirstChild("Right Leg").Reflectance =(math.random(0.2,1))
target.Character:findFirstChild("Left Arm").Reflectance =(math.random(0.2,1))
target.Character:findFirstChild("Left Leg").Reflectance =(math.random(0.2,1))
target.Character.Humanoid.WalkSpeed = 8
turret.Fire:Play()
wait(set.shotcooldown)
ready = true
end
end
target = nil
end)
function Laser(StartPosition,TargetPosition,Hit)
turret.Material = Enum.Material.Neon
local ray = Instance.new("Part")
ray.TopSurface = Enum.SurfaceType.Smooth
ray.BottomSurface = Enum.SurfaceType.Smooth
ray.FormFactor = Enum.FormFactor.Custom
ray.Size = Vector3.new(0.5, 0.2, 0.2)
ray.CanCollide = false
ray.Locked = true
ray.Anchored = true
ray.Name = "Laser"
ray.BrickColor = BrickColor.new("Cyan")
ray.Material = Enum.Material.Neon
ray.Size = Vector3.new(0.0, 0.5, 0.2)
local raymesh = Instance.new("SpecialMesh")
raymesh.Name = "Mesh"
raymesh.MeshType = Enum.MeshType.Brick
raymesh.Scale = Vector3.new(0.2, 0.2, 1)
raymesh.Offset = Vector3.new(0, 0, 0)
raymesh.VertexColor = Vector3.new(1, 1, 1)
raymesh.Parent = ray
local Vec = (TargetPosition - StartPosition)
local Distance = Vec.magnitude
local Direction = Vec.unit
local PX = (StartPosition + (0.25 * Distance) * Direction)
local PY = (StartPosition + (0.5 * Distance) * Direction)
local PZ = (StartPosition + (0.75 * Distance) * Direction)
local DX = (StartPosition - PX).magnitude
local DY = (PX - PY).magnitude
local DZ = (PY - PZ).magnitude
local Limit = 2
local AX = (PX + Vector3.new(math.random(math.max(-Limit, (-0.21 * DX)), math.min(Limit, (0.21 * DX))),math.random(math.max(-Limit, (-0.21 * DX)),math.min(Limit, (0.21 * DX))), math.random(math.max(-Limit, (-0.21 * DX)), math.min(Limit, (0.21 * DX)))))
local AY = (PY + Vector3.new(math.random(math.max(-Limit, (-0.21 * DY)), math.min(Limit, (0.21 * DY))),math.random(math.max(-Limit, (-0.21 * DY)),math.min(Limit, (0.21 * DY))), math.random(math.max(-Limit, (-0.21 * DY)), math.min(Limit, (0.21 * DY)))))
local AZ = (PZ + Vector3.new(math.random(math.max(-Limit, (-0.21 * DZ)), math.min(Limit, (0.21 * DZ))),math.random(math.max(-Limit, (-0.21 * DZ)),math.min(Limit, (0.21 * DZ))), math.random(math.max(-Limit, (-0.21 * DZ)), math.min(Limit, (0.21 * DZ)))))
local Rays = {
{Distance = (AX - StartPosition).magnitude, Direction = CFrame.new(StartPosition, AX)},
{Distance = (AY - AX).magnitude, Direction = CFrame.new(AX, AY)},
{Distance = (AZ - AY).magnitude, Direction = CFrame.new(AY, AZ)},
{Distance = (TargetPosition - AZ).magnitude, Direction = CFrame.new(AZ, TargetPosition)},
}
for i, v in pairs(Rays) do
local Ray = ray:Clone()
local Mesh = Ray.Mesh
Mesh.Scale = (Vector3.new(0.15, 0.15, (v.Distance / 1)) * 5)
Ray.CFrame = (v.Direction * CFrame.new(0, 0, (-0.5 * v.Distance)))
game:GetService("Debris"):AddItem(Ray, (0.1 / (#Rays - (i - 1))))
Ray.Parent = workspace
end
turret.Material = Enum.Material.Neon
end

4 comments
You need to include the whole script, not just a fragment.
Okay, i did most of the lines aren't mine
What i want it to do is make the humanoid reflectance 0 and speed back to normal 16 once he stops being attacked by the turret
Okay i've fixed the reflectance and speed with a solution i cloned a localscript into a player, but it clones alot of them because my turret fires every 0.10 seconds how do i make clone only 1 time when it starts shooting ?