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)
001 | -- Starting stuff -- |
002 | local tool = script.Parent |
003 | local player = game.Players.LocalPlayer |
004 | local mouse = player:GetMouse() |
005 | local character = player.Character |
006 | local Barrel = tool:WaitForChild( "Barrel" ) |
007 | local enabled = true |
008 | local Ammo = tool:WaitForChild( "Ammo" ) |
009 | local ammo = Ammo.Value |
010 | local SA = tool:WaitForChild( "StoredAmmo" ) |
011 | local StoredAmmo = SA.Value |
012 | local Reloading = false |
013 |
014 | --Shooting-- |
015 | function Shoot() |
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:
01 | --LocalScript |
02 | --CODE |
03 | mouse.KeyDown:Connect( function () |
04 | game:GetService( "ReplicatedStorage" ). [ bulletRemoteEventName ] :FireServer() --Fires the remote event and triggers it in the server script |
05 | end ) |
06 |
07 | --ServerScript |
08 | game:GetService( "ReplicatedStorage" ). [ bulletRemoteEventname ] .OnServerEvent:Connect( function () --Hears out for the event and connects to a function |
09 | --Code for bullet shooting |
10 | end ) |
You can research this a little more here- https://developer.roblox.com/api-reference/class/RemoteEvent