So currently i have a modules that fires rays and damages players. Script below. I want to know if there are any ways i can make this script run smoother
local module = {}
local findifout = function(name)
local sleeping1 = false
local bodies = game.Workspace.Bodies:GetChildren()
for i,v in pairs(bodies) do
if v.Name == name then
sleeping1 = true
end
end
return(sleeping1)
end
local Module = require(script.Parent:WaitForChild("PartFractureModule"))
local RunService = game:GetService("RunService")
function module.FireRay(oldpos, mousehit, shotVel, drop, ignore, plr, clone, strdamage, dmg, Sound)
local current = tick()
local sound
local sleeping
repeat
if tick() - current > 20 then
clone:Destroy()
return
end
RunService.Heartbeat:Wait()
shotVel[1] = (shotVel[1] - .05)
drop[1] = (drop[1] + .1)
local ray = Ray.new(oldpos, (mousehit - oldpos).unit * shotVel[1] + Vector3.new(0,-drop[1],0))
local hit, position, normal = workspace:FindPartOnRayWithIgnoreList(ray,ignore)
oldpos = position
clone.Position = oldpos
if hit ~= nil then
clone:Destroy()
if hit.Name == plr.Name or hit.Parent.Name == plr.Name then
else
wait(0.1)
if hit then
if hit.Material == Enum.Material.Glass then
local attach = Instance.new("Attachment")
attach.Parent = hit
attach.Name = "BreakingPoint"
attach.Visible = true
local BreakingPoint = hit:FindFirstChild("BreakingPoint")
if BreakingPoint and BreakingPoint:IsA("Attachment") then
BreakingPoint.WorldPosition = position
BreakingPoint.Position = Vector3.new(BreakingPoint.Position.X, BreakingPoint.Position.Y, BreakingPoint.Position.Z)
Module.FracturePart(hit)
end
end
if hit.Name == "Place" then
local hole = game.ReplicatedStorage.Gunassets.BulletHole:Clone()
hole.CFrame = CFrame.new(position, position + normal)
hole.Parent = workspace
game:GetService("Debris"):AddItem(hole, 2)
local ParticleEmitter = require(script.ParticleEmitter)
ParticleEmitter:emit(ParticleEmitter.particleType.electricSparks,CFrame.new(position, position + normal), 30, 20, 0.1, 1, true)
local damage = strdamage
local code = hit.Parent.Place.Stats.ID.Value
local hitplayer = hit.Name
script.Parent.Building:FireServer(damage, code, hitplayer)
end
if hit.Parent:IsA("Accessory") then
hit = hit.Parent
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
if hit.Name == "Head" then
local damage = dmg.Value*2
local hitplayer = hit.Parent.Name
local weapon = script.Parent.Name
local already = false
local hit = game.Workspace:FindFirstChild(hitplayer, true)
local find = "Humanoid"
local humanoid = hit:FindFirstChild(find, true)
humanoid:TakeDamage(damage)
if humanoid.Health <= 0 and already == false then
local name = hitplayer
sleeping = findifout(name)
if sleeping == false then
already = true
local clone = script.KillGui:Clone()
clone.Frame.TextLabel.Text = "you where killed by " .. plr.Name .. " with " .. weapon
local player = game.Players:FindFirstChild(hitplayer)
clone.Parent = player.PlayerGui
already = false
elseif sleeping == true then
hit:Destroy()
local text = "you where killed by " .. plr.Name .. " with " .. weapon
local current = hitplayer
local folder = Instance.new("Folder")
folder.Parent = game.ReplicatedStorage.KillValues
folder.Name = current
local textvalue = Instance.new("StringValue")
textvalue.Parent = folder
textvalue.Value = text
textvalue.Name = "Text"
end
end
else
local damage = dmg.Value
local hitplayer = hit.Parent.Name
local weapon = script.Parent.Name
local already = false
local hit = game.Workspace:FindFirstChild(hitplayer, true)
local find = "Humanoid"
local humanoid = hit:FindFirstChild(find, true)
humanoid:TakeDamage(damage)
if humanoid.Health <= 0 and already == false then
local name = hitplayer
sleeping = findifout(name)
if sleeping == false then
already = true
local clone = script.KillGui:Clone()
clone.Frame.TextLabel.Text = "you where killed by " .. plr.Name .. " with " .. weapon
local player = game.Players:FindFirstChild(hitplayer)
clone.Parent = player.PlayerGui
already = false
elseif sleeping == true then
hit:Destroy()
local text = "you where killed by " .. plr.Name .. " with " .. weapon
local current = hitplayer
local folder = Instance.new("Folder")
folder.Parent = game.ReplicatedStorage.KillValues
folder.Name = current
local textvalue = Instance.new("StringValue")
textvalue.Parent = folder
textvalue.Value = text
textvalue.Name = "Text"
end
end
end
end
else
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
if hit.Name == "Head" then
sound = "headshot"
Sound:FireClient(plr, sound)
local damage = dmg.Value*2
local hitplayer = hit.Parent.Name
local weapon = script.Parent.Name
local already = false
local hit = game.Workspace:FindFirstChild(hitplayer, true)
local find = "Humanoid"
local humanoid = hit:FindFirstChild(find, true)
humanoid:TakeDamage(damage)
if humanoid.Health <= 0 and already == false then
local name = hitplayer
sleeping = findifout(name)
if sleeping == false then
already = true
local clone = script.KillGui:Clone()
clone.Frame.TextLabel.Text = "you where killed by " .. plr.Name .. " with " .. weapon
local player = game.Players:FindFirstChild(hitplayer)
clone.Parent = player.PlayerGui
already = false
elseif sleeping == true then
hit:Destroy()
local text = "you where killed by " .. plr.Name .. " with " .. weapon
local current = hitplayer
local folder = Instance.new("Folder")
folder.Parent = game.ReplicatedStorage.KillValues
folder.Name = current
local textvalue = Instance.new("StringValue")
textvalue.Parent = folder
textvalue.Value = text
textvalue.Name = "Text"
end
end
else
sound = "hit"
Sound:FireClient(plr, sound)
local damage = dmg.Value
local hitplayer = hit.Parent.Name
local weapon = script.Parent.Name
local already = false
local hit = game.Workspace:FindFirstChild(hitplayer, true)
local find = "Humanoid"
local humanoid = hit:FindFirstChild(find, true)
humanoid:TakeDamage(damage)
if humanoid.Health <= 0 and already == false then
local name = hitplayer
sleeping = findifout(name)
if sleeping == false then
already = true
local clone = script.KillGui:Clone()
clone.Frame.TextLabel.Text = "you where killed by " .. plr.Name .. " with " .. weapon
local player = game.Players:FindFirstChild(hitplayer)
clone.Parent = player.PlayerGui
already = false
elseif sleeping == true then
hit:Destroy()
local text = "you where killed by " .. plr.Name .. " with " .. weapon
local current = hitplayer
local folder = Instance.new("Folder")
folder.Parent = game.ReplicatedStorage.KillValues
folder.Name = current
local textvalue = Instance.new("StringValue")
textvalue.Parent = folder
textvalue.Value = text
textvalue.Name = "Text"
end
end
end
else
local hole = game.ReplicatedStorage.Gunassets.BulletHole:Clone()
hole.CFrame = CFrame.new(position, position + normal)
hole.Parent = workspace
game:GetService("Debris"):AddItem(hole, 2)
local ParticleEmitter = require(script.ParticleEmitter)
ParticleEmitter:emit(ParticleEmitter.particleType.electricSparks,CFrame.new(position, position + normal), 30, 30, 0.1, 1, true)
end
end
end
end
end
until
hit ~= nil
end
return module
the full game file with the entire gun system is below
Weapon System.rbxl (130.1 KB)