I making this gun and it works perfectly in Studio but when in a server with a friend my friend can't see my bullets and killing each other doesn't work.
Could someone explain to me how to make my script work in servers (I used a local script)
-- Starting stuff -- local tool = script.Parent local player = game.Players.LocalPlayer local mouse = player:GetMouse() local character = player.Character local Barrel = tool:WaitForChild("Barrel") local enabled = true local Ammo = tool:WaitForChild("Ammo") local ammo = Ammo.Value local SA = tool:WaitForChild("StoredAmmo") local StoredAmmo = SA.Value local Reloading = false --Shooting-- function Shoot() if ammo >= 1 and enabled == true and Reloading == false then enabled = false Firing = true isFiring() end end -- Damage Value -- local Damage = 10,15 local Accuracy = math.random(-5,5) function isFiring() repeat local Bullet = Instance.new("Part",workspace) ammo=ammo-1 print(ammo) function onTouched(hit) if hit.Parent.Name==(player.Name) then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0 elseif hit.Name == "Head" then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*2 Bullet:Destroy() elseif hit.Name == "Torso" then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*1 Bullet:Destroy() elseif hit.Name == "Right Leg" then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0.5 Bullet:Destroy() elseif hit.Name == "Left Leg" then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0.5 Bullet:Destroy() elseif hit.Name == "Right Arm" then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0.5 Bullet:Destroy() elseif hit.Name == "Left Arm" then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0.5 Bullet:Destroy() elseif hit:IsA("Hat") then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*math.random(0.25,2) Bullet:Destroy() else Bullet:Destroy() end end if ammo == 0 or Reloading then Firing=false end --Bullet-- game.Debris:AddItem(Bullet,1) Bullet.Name= "Bullet" Bullet.CanCollide= false Bullet.Shape = "Block" Bullet.Material = "Neon" Bullet.BrickColor = BrickColor.new ("Bright yellow")--Colour-- Bullet.Size=Vector3.new (0.1,0.1,2.5)--Size-- Bullet.CFrame = Barrel.CFrame*CFrame.new(-1.25,0,0)--Where-- Bullet.CFrame= CFrame.new(Bullet.Position,mouse.hit.p) local Velo = Instance.new ("BodyVelocity",Bullet) Velo.velocity = Bullet.CFrame.lookVector*200--Speed of Bullet-- Velo.MaxForce = Vector3.new(math.huge,math.huge,math.huge) Bullet.CustomPhysicalProperties = PhysicalProperties.new(0.01,0,0,0,0) local weld =Instance.new("Weld",Bullet) weld.C0= Bullet.CFrame:inverse()*Bullet.CFrame weld.Part0=Bullet weld.Part1=Bullet local a0 = Instance.new("Attachment") a0.Parent = Bullet a0.Position = Vector3.new(0,.5,0) a0.Name = "Trail Attachment 0" local a1 = Instance.new("Attachment") a1.Parent = Bullet a1.Position = Vector3.new(0,-.5,0) a1.Name = "Trail Attachment 1" local trail = Instance.new("Trail") trail.Parent = Bullet trail.Attachment0 = a0 trail.Attachment1 = a1 trail.Transparency = NumberSequence.new(0.5,1) trail.WidthScale=NumberSequence.new(0.1,0.1) trail.Color = ColorSequence.new(Color3.new(205,150,0)) trail.LightEmission=0.75 trail.Lifetime = 0.5 Bullet.Touched:Connect(onTouched) tool.GripForward = Vector3.new(0,-.05,-1) tool.Barrel.ParticleEmitter.Enabled = true tool.Barrel.PointLightA.Enabled = true tool.Barrel.Shot.PlaybackSpeed = 3 tool.Barrel.Shot:Play() local shell = Instance.new("Part") shell.CFrame = tool.Eject.CFrame * CFrame.fromEulerAnglesXYZ(2.5,1,1.25) shell.Size = Vector3.new(1,1,1) shell.BrickColor = BrickColor.new(24) shell.Reflectance = 0 shell.CanCollide = true shell.BottomSurface = 0 shell.TopSurface = 0 shell.Name = "Shell" shell.Velocity = tool.Eject.CFrame.lookVector * 10 + Vector3.new(math.random(0,0),10,math.random(0,0)) shell.RotVelocity = Vector3.new(-10,40,30) local shellmesh = Instance.new("SpecialMesh") shellmesh.Scale = Vector3.new(2,2,2) shellmesh.MeshId = "http://www.roblox.com/asset/?id=94295100" shellmesh.TextureId = "http://www.roblox.com/asset/?id=94287792" shellmesh.Parent = shell shell.Parent = game.Workspace game:GetService("Debris"):addItem(shell,2) wait() tool.Barrel.PointLightB.Enabled = true tool.GripForward = Vector3.new(0,-.15,-1) wait(.005)--FireRate-- tool.GripForward = Vector3.new(0,-.05,-1) wait() tool.Barrel.PointLightB.Enabled = false tool.GripForward = Vector3.new(0,0,-1) enabled= true tool.Barrel.ParticleEmitter.Enabled = false tool.Barrel.PointLightA.Enabled = false --Bullet-- until Firing == false or Ammo == 0 end function StopShoot() Firing = false tool.Barrel.NoShot:Play() end --Variable-- function On () Firing = false On = true end function Off () Firing = false On = false end --Reloding-- while Reloading do script.Disabled = true end mouse.KeyDown:connect(function(key) if key:lower() == "r" and On and StoredAmmo>=1 or key:upper() == "R" and On and StoredAmmo>=1 then local Difference = 30-ammo --If ammo changed change this to Ammo e.g. Ammo=20 then switch the 30 to 20 Reloading = true tool.Barrel.Reload:Play() wait(2) Reloading = false print("Reloaded") if StoredAmmo <30 then local FDif = StoredAmmo-ammo ammo = FDif + ammo StoredAmmo = StoredAmmo-FDif print(ammo) print(StoredAmmo) else ammo = Difference + ammo StoredAmmo = StoredAmmo-Difference print(ammo) print(StoredAmmo) end end end) --Events-- tool.Equipped:Connect(On) tool.Unequipped:Connect(Off) --Activated-- tool.Activated:Connect(Shoot) tool.Deactivated:Connect(StopShoot)
Because its in a local script it wont work with filtering enabled and all of the shooting part would have to be run through a server script. There's a few ways you could do it and I am new to this so i don't know the best way but a Remote event would most likely work for this.
For example:
--LocalScript --CODE mouse.KeyDown:Connect(function() game:GetService("ReplicatedStorage").[bulletRemoteEventName]:FireServer() --Fires the remote event and triggers it in the server script end) --ServerScript game:GetService("ReplicatedStorage").[bulletRemoteEventname].OnServerEvent:Connect(function() --Hears out for the event and connects to a function --Code for bullet shooting end)
You can research this a little more here- https://developer.roblox.com/api-reference/class/RemoteEvent