[CLOSED] Trouble Converting Gun To Filtering Enabled Compatible

I’m working on a game that uses guns, and one of the guns I have is a free model, yet, I made sure everything was in order. But when I shoot someone, they die, yes, but they don’t respawn. Mostly because the gun is client-sided and doesn’t replicate to the server. I looked at the gun’s main script, which is a Local Script. So I just make the Local Script into a regular Script, with the same name as the previous script, yet it’s not working, nothing is. I just copied the code from the Local Script into the regular one and now nothing is working. Here’s my code.

--------------------- TEMPLATE ASSAULT RIFLE WEAPON ---------------------------
-- Waits for the child of the specified parent
local function WaitForChild(parent, childName)
	while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
	return parent[childName]
end

----- MAGIC NUMBERS ABOUT THE TOOL -----
-- How much damage a bullet does
local Damage = 100
-- How many times per second the gun can fire
local FireRate = 1
-- The maximum distance the can can shoot, this value should never go above 1000
local Range = 350
-- In radians the minimum accuracy penalty
local MinSpread = 0.01
-- In radian the maximum accuracy penalty
local MaxSpread = 0.1
-- Number of bullets in a clip
local ClipSize = 3
-- DefaultValue for spare ammo
SpareAmmo = script.Parent.MaxAmmo.Value
-- The amount the aim will increase or decrease by
-- decreases this number reduces the speed that recoil takes effect
local AimInaccuracyStepAmount = 0.08
-- Time it takes to reload weapon
local ReloadTime = 2.3
----------------------------------------

-- Colors
local FriendlyReticleColor = Color3.new(0, 1, 0)
local EnemyReticleColor	= Color3.new(1, 0, 0)
local NeutralReticleColor	= Color3.new(1, 1, 1)

local Remote = script.Parent.Shell

local Spread = MinSpread
local AmmoInClip = ClipSize

local Tool = script.Parent
local Handle = WaitForChild(Tool, 'Handle')
local WeaponGui = nil
local readytoshoot = false
local LeftButtonDown
local Reloading = false
local IsShooting = false
local drawing = false
-- Player specific convenience variables
local MyPlayer = nil
local MyCharacter = nil
local MyHumanoid = nil
local MyTorso = nil
local MyMouse = nil

local RecoilAnim
local RecoilTrack = nil

local IconURL = Tool.TextureId  -- URL to the weapon icon asset

local DebrisService = game:GetService('Debris')
local PlayersService = game:GetService('Players')

local down = false

local FireSound

local OnFireConnection = nil
local OnReloadConnection = nil

local DecreasedAimLastShot = false
local LastSpreadUpdate = time()

-- this is a dummy object that holds the flash made when the gun is fired
local FlashHolder = nil


local WorldToCellFunction = Workspace.Terrain.WorldToCellPreferSolid
local GetCellFunction = Workspace.Terrain.GetCell

function RayIgnoreCheck(hit, pos)
	if hit then
		if hit.Transparency >= 1 or string.lower(hit.Name) == "water" or
				hit.Name == "Effect" or hit.Name == "Rocket" or hit.Name == "Bullet" or
				hit.Name == "Handle" or hit:IsDescendantOf(MyCharacter) then
			return true
		elseif hit:IsA('Terrain') and pos then
			local cellPos = WorldToCellFunction(Workspace.Terrain, pos)
			if cellPos then
				local cellMat = GetCellFunction(Workspace.Terrain, cellPos.x, cellPos.y, cellPos.z)
				if cellMat and cellMat == Enum.CellMaterial.Water then
					return true
				end
			end
		end
	end
	return false
end

-- @preconditions: vec should be a unit vector, and 0 < rayLength <= 1000
function RayCast(startPos, vec, rayLength)
	local hitObject, hitPos = game.Workspace:FindPartOnRay(Ray.new(startPos + (vec * .01), vec * rayLength), Handle)
	if hitObject and hitPos then
		local distance = rayLength - (hitPos - startPos).magnitude
		if RayIgnoreCheck(hitObject, hitPos) and distance > 0 then
			-- there is a chance here for potential infinite recursion
			return RayCast(hitPos, vec, distance)
		end
	end
	return hitObject, hitPos
end



function TagHumanoid(humanoid, player)
	-- Add more tags here to customize what tags are available.
	while humanoid:FindFirstChild('creator') do
		humanoid:FindFirstChild('creator'):Destroy()
	end 
	local creatorTag = Instance.new("ObjectValue")
	creatorTag.Value = player
	creatorTag.Name = "creator"
	creatorTag.Parent = humanoid
	DebrisService:AddItem(creatorTag, 1.5)

	local weaponIconTag = Instance.new("StringValue")
	weaponIconTag.Value = IconURL
	weaponIconTag.Name = "icon"
	weaponIconTag.Parent = creatorTag
end


local function CreateBullet(bulletPos)
	local bullet = Instance.new('Part', Workspace)
	bullet.FormFactor = Enum.FormFactor.Custom
	bullet.Size = Vector3.new(0.1, 0.1, 0.1)
	bullet.BrickColor = BrickColor.new("Black")
	bullet.Shape = Enum.PartType.Block
	bullet.CanCollide = false
	bullet.CFrame = CFrame.new(bulletPos)
	bullet.Anchored = true
	bullet.TopSurface = Enum.SurfaceType.Smooth
	bullet.BottomSurface = Enum.SurfaceType.Smooth
	bullet.Name = 'Bullet'
	DebrisService:AddItem(bullet, 3)
	return bullet
end

local function Reload()
	if not Reloading then
		Reloading = true
		-- Don't reload if you are already full or have no extra ammo
		if AmmoInClip ~= ClipSize and script.Parent.MaxAmmo.Value > 0 then
			if RecoilTrack then
				RecoilTrack:Stop()
			end
			if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
				if WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
					WeaponGui.Crosshair.ReloadingLabel.Visible = true
				end
			end
			script.Parent.Handle.Reload:Play()
            ReloadTrack:Play()
			wait(ReloadTime)
			-- Only use as much ammo as you have
			local ammoToUse = math.min(ClipSize - AmmoInClip, SpareAmmo)
			AmmoInClip = AmmoInClip + ammoToUse
			script.Parent.MaxAmmo.Value = script.Parent.MaxAmmo.Value - ammoToUse
			UpdateAmmo(AmmoInClip)
			WeaponGui.Reload.Visible = false
		end
		Reloading = false
	end
script.Parent.Barrel.Transparency = 0
			script.Parent.BarrelBack.Transparency = 1
			script.Parent.Handle.Slidein:Play()
end

function OnFire()
	if IsShooting then return end 
	if MyHumanoid and MyHumanoid.Health > 0 then
		if RecoilTrack and AmmoInClip > 0 then
		end
		IsShooting = true
		if LeftButtonDown and AmmoInClip > 0 and not Reloading and down == false and readytoshoot == true then
			if Spread and not DecreasedAimLastShot then
				Spread = math.min(MaxSpread, Spread + AimInaccuracyStepAmount)
				UpdateCrosshair(Spread)
			end
			DecreasedAimLastShot = not DecreasedAimLastShot
			if Handle:FindFirstChild('FireSound') then
				Handle.FireSound:Play()
	script.Parent.Flash.Light.Enabled = true
	script.Parent.Flash.Smoke.Enabled = true
	Handle.Flash.Enabled = true
	script.Parent.Barrel.Transparency = 1
	script.Parent.BarrelBack.Transparency = 0
	script.Parent.Flash.Light.Enabled = false
			end
			if MyMouse then
				RecoilTrack:Play()
				local targetPoint = MyMouse.Hit.p
				local shootDirection = (targetPoint - Handle.Position).unit
				-- Adjust the shoot direction randomly off by a little bit to account for recoil
				shootDirection = CFrame.Angles((0.5 - math.random()) * 2 * Spread,
																(0.5 - math.random()) * 2 * Spread,
																(0.5 - math.random()) * 2 * Spread) * shootDirection
				local hitObject, bulletPos = RayCast(Handle.Position, shootDirection, Range)
				local bullet
				-- Create a bullet here
				if hitObject then
					bullet = CreateBullet(bulletPos)
					local hitsoundricochet = Instance.new("Sound")
						hitsoundricochet.SoundId = "http://www.roblox.com/asset/?id=259586543"						
						hitsoundricochet.Parent = bullet
						hitsoundricochet.Volume = 10
						hitsoundricochet:Play()
						hitsoundricochet.Volume = 2
		                hitsoundricochet.MaxDistance = 15
		                hitsoundricochet.EmitterSize = 15
		                hitsoundricochet.Pitch = 1 * math.random(38, 42)/40
				end
				 if hitObject and hitObject.Parent then
					local hitHumanoid = hitObject.Parent:FindFirstChild("Humanoid")
					if hitHumanoid then
						local hitPlayer = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent)
						if MyPlayer.Neutral or hitPlayer then
							TagHumanoid(hitHumanoid, MyPlayer)
							hitHumanoid:TakeDamage(Damage)
							if bullet then
								--bullet.Transparency = 1
							end
							Spawn(UpdateTargetHit)
						end
					end
				end
				AmmoInClip = AmmoInClip - 1
				UpdateAmmo(AmmoInClip)
			end
			wait(FireRate)
			Remote:FireServer()
		end
		Handle.Flash.Enabled = false
		script.Parent.Flash.Smoke.Enabled = false
	script.Parent.Barrel.Transparency = 0
	script.Parent.BarrelBack.Transparency = 1
		IsShooting = false
		if AmmoInClip == 0 then
			Handle.Tick:Play()
			WeaponGui.Reload.Visible = true
			script.Parent.Barrel.Transparency = 1
			script.Parent.BarrelBack.Transparency = 0
			script.Parent.Handle.SlideOut:Play()
		end
		if RecoilTrack then
		end
	end
end

local TargetHits = 0
function UpdateTargetHit()
	TargetHits = TargetHits + 1
	if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
		WeaponGui.Crosshair.TargetHitImage.Visible = true
	end
	wait(0.5)
	TargetHits = TargetHits - 1
	if TargetHits == 0 and WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
		WeaponGui.Crosshair.TargetHitImage.Visible = false
	end
end

function UpdateCrosshair(value, mouse)
	if WeaponGui then
		local absoluteY = 650
		WeaponGui.Crosshair:TweenSize(
			UDim2.new(0, value * absoluteY * 2 + 23, 0, value * absoluteY * 2 + 23),
			Enum.EasingDirection.Out,
			Enum.EasingStyle.Linear,
			0.33)
	end
end

function UpdateAmmo(value)
	if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then
		WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip
		if value > 0 and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
			WeaponGui.Crosshair.ReloadingLabel.Visible = false
		end
	end
	if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('TotalAmmo') then
		WeaponGui.AmmoHud.TotalAmmo.Text = script.Parent.MaxAmmo.Value
	end
end


function OnMouseDown()
	LeftButtonDown = true
	OnFire()
end

function OnMouseUp()
	LeftButtonDown = false
end


function OnKeyDown(key)
	
	if string.lower(key) == "r" then
    Reload()
  else
    if string.lower(key) == "g" then
	   if script.Parent.Down.Value == 0 then
        down = true
        GunDownTrack:Play()
        IdleAnim:Stop()
        script.Parent.Down.Value = 1
	 elseif
		script.Parent.Down.Value == 1 then
		down = false
		LeftButtonDown = false
	    GunDownTrack:Stop()
        IdleAnim:Play()
        script.Parent.Down.Value = 0
     end
      else
     if string.lower(key) == "m" then
	   if script.Parent.Mode.Value == 1 then
        script.Parent.Idle.AnimationId = "http://www.roblox.com/Asset?ID=1339246491"
        script.Parent.Recoil.AnimationId = "http://www.roblox.com/Asset?ID=1339258823"
        script.Parent.Mode.Value = 0
      else
        if script.Parent.Mode.Value == 0 then
          script.Parent.Idle.AnimationId = "http://www.roblox.com/Asset?ID=1337828469"
          script.Parent.Recoil.AnimationId = "http://www.roblox.com/Asset?ID=1338327142"
          script.Parent.Mode.Value = 1
          end
        end
      end
    end
  end
end


function OnEquipped(mouse)
    IdleAnim = WaitForChild(Tool, "Idle")
    GunDownAnim = WaitForChild(Tool, "GunDown")
    RecoilAnim = WaitForChild(Tool, "Recoil")
    ReloadAnim = WaitForChild(Tool, "Reload")
	FireSound  = WaitForChild(Handle, 'FireSound')
	
	MyCharacter = Tool.Parent
	MyPlayer = game:GetService('Players'):GetPlayerFromCharacter(MyCharacter)
	MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
	MyTorso = MyCharacter:FindFirstChild('UpperTorso')
	MyMouse = mouse
	WeaponGui = WaitForChild(Tool, 'WeaponHud'):Clone()
	if WeaponGui and MyPlayer then
		WeaponGui.Parent = MyPlayer.PlayerGui
		UpdateAmmo(AmmoInClip)
	end
	if IdleAnim then
    IdleAnim = MyHumanoid:LoadAnimation(IdleAnim)
  end
  if RecoilAnim then
    RecoilTrack = MyHumanoid:LoadAnimation(RecoilAnim)
  end
  if ReloadAnim then
    ReloadTrack = MyHumanoid:LoadAnimation(ReloadAnim)
  end
if GunDownAnim then
    GunDownTrack = MyHumanoid:LoadAnimation(GunDownAnim)
  end
	if MyMouse then
		-- Disable mouse icon
		MyMouse.Icon = "http://www.roblox.com/asset/?id=18662154"
		MyMouse.Button1Down:connect(OnMouseDown)
		MyMouse.Button1Up:connect(OnMouseUp)
		MyMouse.KeyDown:connect(OnKeyDown)
	end
  wait()
   Handle.EquipSound:Play()
    script.Parent.Barrel.Transparency = 1
    script.Parent.BarrelBack.Transparency = 0
    wait()
    script.Parent.Barrel.Transparency = 0
    script.Parent.BarrelBack.Transparency = 1
    wait()
    wait()
    if drawing == false then
      IdleAnim:Play()
      readytoshoot = true
    else
      IdleAnim:Stop()
    end
  end
  wait()


-- Unequip logic here
function OnUnequipped()
	
	IdleAnim:Stop()
	readytoshoot = false
	LeftButtonDown = false
	Reloading = false
	MyCharacter = nil
	MyHumanoid = nil
	MyTorso = nil
	MyPlayer = nil
	MyMouse = nil
  if IdleAnim then
    IdleAnim:Stop()
  end
  if RecoilTrack then
    RecoilTrack:Stop()
  end
if GunDownTrack then
    GunDownTrack:Stop()
    down = false
end
  MyHumanoid = nil
  MyTorso = nil
  MyPlayer = nil
  MyMouse = nil
  if OnFireConnection then
    OnFireConnection:disconnect()
  end
  if OnReloadConnection then
    OnReloadConnection:disconnect()
  end
  if FlashHolder then
    FlashHolder = nil
  end
  if WeaponGui then
    WeaponGui.Parent = nil
    WeaponGui = nil
  end
  IdleAnim:Stop()
end

local function SetReticleColor(color)
	if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
		for _, line in pairs(WeaponGui.Crosshair:GetChildren()) do
			if line:IsA('Frame') then
				line.BorderColor3 = color
			end
		end
	end
end


Tool.Equipped:connect(OnEquipped)
Tool.Unequipped:connect(OnUnequipped)

while true do
	wait(0.033)
	if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and MyMouse then
		WeaponGui.Crosshair.Position = UDim2.new(0, MyMouse.X, 0, MyMouse.Y)
		SetReticleColor(NeutralReticleColor)

		local target = MyMouse.Target
		if target and target.Parent then
			local player = PlayersService:GetPlayerFromCharacter(target.Parent)
			if player then
				if MyPlayer.Neutral or player.TeamColor ~= MyPlayer.TeamColor then
					SetReticleColor(EnemyReticleColor)
				else
					SetReticleColor(FriendlyReticleColor)
				end
			end
		end
	end
	if Spread and not IsShooting then
		local currTime = time()
		if currTime - LastSpreadUpdate > FireRate * 2 then
			LastSpreadUpdate = currTime
			Spread = math.max(MinSpread, Spread - AimInaccuracyStepAmount)
			UpdateCrosshair(Spread, MyMouse)
		end
	end
end

Nobody is going to want to help you if you just post your entire free model gun script without putting any real effort into trying; this forum is better used for help with small issues or guidance.

As for converting it, it will be much easier to create your own new gun script (if you are experienced enough) otherwise you should become more familiar with Roblox Lua before attempting a gun script.

Here is a tutorial I found from searching the forum, you can probably find other tutorials if it confuses you: The Basics of adjusting your game to the removal of Experimental Mode with FilteringEnabled

3 Likes

This isn’t the ENTIRE script. This is just the MAIN script, but I get what you’re getting at.

If you really need to show us your code, at least do it properly by having it indented and using the “preformatted text” option. Also, no one will want to read that long of a script. It is usually a better practice to post the most important parts of your code.

For your question, the logic of filtering enabled is simple: Clients (players) cannot make changes on the game visible to the server and therefore other players. They can only make changes for themselves. However, all actions by the server are global, meaning all players see them.

Because of these principles, your gun local script cannot lower the HP of the enemies. You should use that local script only to detect when the player fires, reloads, etc. and add animations. Besides all these, you need to tell the server that you have fired to some specific coordinates. We do that by using remote events. (If you need information returned back, use a remote function.)

Once the server receives the action, it should do the thing.

I am using Remote Events in the gun. It’s still not working.

Real quick:

  1. I noticed that the script contains methods for input and displaying gui’s, which should be contained in a local script.
  2. Event methods are deprecated, change :connect and :disconnect to :Connect and :Disconnect, which can be fixed with a simple Find and Replace.
  3. Are there any errors/warnings showing up in output? Give it 5-10 seconds for any :WaitForChild warnings to come up.
  4. If the tool is in the character, it can be operated with a local script, if you didn’t already know. You will have to create a server-sided system for bullets that are controlled by the player.
  5. MyMouse, or the Player:GetMouse method, has been superseded by UsersInputService and ContextActionService, which contain more sophisticated API’s for detecting the client’s input and won’t break anytime soon.
  6. You should probably analyze your code before you ask further questions, so that you can get familiar with the script.

The local function CreateBullet should definitely be in a server script, but will probably be needed in the local script nonetheless for a good user experience. Everything else most likely belongs in a local script, since the rest is gui modification and detecting user input, which the server can’t control.
You should be using a remote function in a coroutine to create the bullet, and making the bullet’s LocalTransparencyModifier 1 when it shows up on the creator’s client while spawning the bullet locally in the current thread. Make sure to use sanity checks and a leaky bucket algorithm.

It depends on how you use them. In your question, you said: “They die but they do not respawn.”. This basically means they only die for the shooting player. Other players don’t see that change because that action wasn’t carried out by a global script. The remote events must be used, so the local script tells the server script to lower the target’s HP.

Beware though, if the local script gives very specific information to the server, so can the exploiters to kill everyone. But for now, I would focus on managing to do it.

Please, put your code in a readable format

I would love to help, but all you did was paste a huge blob of text with no description.

To make code actually readable:

    ```lua
      --Code here
    ```

Converts to:

if code then
--Readable code
else
--Not readable
end

Filtering Enabled

As for how filtering enabled works:

  • The servers changes replicate to all clients
  • The clients changes only are visible to themselves

If players are doing without respawning, you are killing them on the client

Just fixed it, so now you can help assist me. Also, of course I know what client-sided means, killing them only applies to the client, not the server. That’s what I’ve been saying the whole time!

You would need to use a remote event for this section:

If you would like a example of how to do this, you can check out my FE comparable gun kit: https://www.roblox.com/library/2098248758/Timothys-Tool-Gun-Kit

You are also using a very outdated free model. CreatorTags are very old, and that system should be updated.

If you have further questions, feel free to ask them.

  1. Are any of the guns R15 compatbile? 2. Do they have animations, or are they scripted?

They are R15 compatible, they do not have custom animations but you can easily add them.

At any rate, it’s better than the free model.

How can I do that? I have the animations themselves, but where in the script does it mention animations? Like, is there a line in which I can delete the dashes that allows for custom animations?

I’d appreciate you pasting some code that I can paste into the gun’s main script that can use the animations.

No, you would have to add it yourself in the relative spots. However, animations are not complicated to play.

local animation = --Object in replicated storage
local character = game.Players.LocalPlayer.Character
local humanoid = character:WaitForChild([[Humanoid]])
local loadeda = humanoid:LoadAnimation(animation)
loadeda:Play()
end

It should be easy to put these in their respective spots.


The Devloper Fourm is to help developers not give them free code. Please keep in mind these are the rules of the devloper fourm.

I’m silently grumbling because of that rule. Whatever, I got a different issue, relating to guns.

Can you please express your exact issue, instead of just wanting 20 different ones?

Okay, okay, new issue. So I have a game that uses ROBLOX’s stock guns, but I added my own custom animations. It didn’t require modifying the scripts. So, when I’m playing with someone else, and they hold their gun out or shoot or something, it doesn’t show them actually holding the gun, it just shows them standing with their arms down, holding the gun. I will give you a link to the game, and you can join me and see what I mean, if that’s okay with you.

Why are you using 10 year old guns? They are very broken, very uncompatiable, and it would be easier to make your own gun scripts then salvage those old ones. Why exactly do you want the retro guns?

Not all catalog items are updated and stay up to date with FE. The gun script is very outdated and I would recommend using another script, even if you want the same look of the gun.


I’m not silly; You are for using old guns scripts and wondering why they dont work.


Roblox does not update their gun scripts constantly.


Other than that, I have given you a ton of options to have working guns and how to make them. If you spend a little bit of time you could easily get a cool animated and working gun. I am not going to keep answering your same question. I have given you the resources to solve the problem; use them.