Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Tips to optimize a gun?

I made this script, but the gun is poorly optimized. I want to optimize it, but I don't know much about that.

Server:

local Arma = script.Parent
local Servicios = {PLS = game:GetService("Players")}

Arma.CF:WaitForChild("Disparo").OnServerEvent:Connect(function(Jugador, Raton)
local Psj = Jugador.Character
local Rayo, Deteccion = Ray.new(Arma.Handle.CFrame.p, (Raton - Arma.Handle.CFrame.p).unit * 300)
local Detector = workspace:FindPartOnRay(Rayo, Psj, false, true)

local Bala = Arma.CF:WaitForChild("Bala"):Clone() Bala.Parent = workspace
Bala.Anchored = false
Bala.CFrame = CFrame.new(Arma.Salida_de_bala.CFrame.p)
Bala.CFrame = CFrame.new(Bala.Position, Raton)

local Fuerza = Instance.new("BodyForce", Bala)
Fuerza.Force = Vector3.new(0, workspace.Gravity, 0)*Bala:GetMass()
	
local Velo = Instance.new("BodyThrust", Bala)
Velo.Force = Vector3.new(0, 0, -500)
	
local Attach0 = Instance.new("Attachment", Bala)
Attach0.Position = Vector3.new(0, 0.10, 0)
		
local Attach1 = Instance.new("Attachment", Bala)
Attach1.Position = Vector3.new(0, -0.10, 0)

Bala.R.Attachment0 = Attach0
Bala.R.Attachment1 = Attach1

Arma.Salida_de_bala.Efecto:Play()

game:GetService("Debris"):AddItem(Bala, 1.5)
Arma.Salida_de_bala.Particula.Transparency = NumberSequence.new(.7)
wait(.01)
Arma.Salida_de_bala.Particula.Transparency = NumberSequence.new(1)

if Detector then
	if Servicios.PLS:GetPlayerFromCharacter(Detector.Parent) then
		Detector.Parent.Humanoid:TakeDamage(20)
		wait(.001)
		Bala:Destroy()
	else
		wait(1)
		Bala:Destroy()
	end
end
end)

Client:

local Servicios = {UIS = game:GetService("UserInputService"), PLS = game:GetService("Players")}
local Jugador = Servicios.PLS.LocalPlayer
local Psj = Jugador.CharacterAdded:Wait()
local Camara = workspace.CurrentCamera
local Arma = script.Parent
local Salida_M = Arma:WaitForChild("Salida_M")
local Autorizacion = true

Arma.Equipped:Connect(function(Raton)
local Pose = Psj:WaitForChild("Humanoid"):LoadAnimation(Arma.CF.Pose)
Pose:Play()
local Clon = script.Parent.CF.Panta Clon.Parent = Jugador.PlayerGui
local Carga = Clon.Balas:WaitForChild("Carga")
local Valor = Carga:WaitForChild("Valor")
local RatonX = Raton.Button1Down:Connect(function()
 if Valor.Value <= 20 and Autorizacion == true then
	Valor.Value = Valor.Value - 1
	Arma.CF:WaitForChild("Disparo"):FireServer(Raton.Hit.p)
	wait(.1)
	Carga.Text = Valor.Value.."/20"
end
if Valor.Value <= 0 then
	Autorizacion = false
end
end)

local US = Servicios.UIS.InputBegan:Connect(function(Tecla, Activo)
	if not Activo then
	if Valor.Value <= 19 then
	 if Tecla.UserInputType == Enum.UserInputType.Keyboard then
		if Tecla.KeyCode == Enum.KeyCode.R then
			Autorizacion = false
			Salida_M.Recarga:Play()
		local Recargar = Jugador.Character.Humanoid:LoadAnimation(Arma.CF.Recargar)
		Valor.Value = tonumber(20)
		Recargar:Play()
		wait(.1)
		local Municion = Arma.CF.Municion:Clone() Municion.Parent = workspace
		Municion.CFrame = CFrame.new(Salida_M.CFrame.p)
		Municion.Anchored = false
		Municion.Transparency = 0
		Recargar:Stop()
		wait(.7)
		Carga.Text = Valor.Value.."/20"
		Municion:Remove()
		Autorizacion = true
		end
	end
	end
	end
end)

Arma.Unequipped:Connect(function()
		Pose:Stop()
		Salida_M.Recarga:Stop()
		Clon.Parent = Arma.CF
		US:Disconnect()
		RatonX:Disconnect()
	end)
end)

2 comments

1

Is this your script? First, you should look for any deprecated code. One of them being the second parameter to Instance.new(). The parent parameter is deprecated so use Object.Parent instead.

0

Yes, is my script, I speak spanish. Okay, i going to try it.

1 answer

✓ Accepted answer

I agree with xPolarium. First, you should clean up any deprecated code using the Roblox wiki as a reference. (from my quick look-through, they're doesn't seem to much).

Second, you have pretty much zero exploit security and that could hurt your game in a big way. Exploiters could break your game and kill players instantly and the game may even lag if you rely on the server too much.

To fix that I recommend you do a series of server checks to make sure the player isn't exploiting once, Arma.CF:WaitForChild("Disparo"):FireServer(Raton.Hit.p) is fired on the client. For an example, you could compare the position of the player to the hit position of the bala/bullet.

If the player is far away from the bala's hit position then kick the player or give them a warning. That's just one of many ways to check if the player is cheating on the server.

Third, you could shorten and clean up your code a lot by just using Ray.new, FindPartOnRay, and instance.New. Your code is WAY more complicated than it needs to be, I recommend using https://www.robloxdev.com/articles/Making-a-ray-casting-laser-gun-in-Roblox as a reference

1 comment

1

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.