Ok; here I go:
The first thing you need to understand is that tools like swords, and guns are not very functional in a filtering enabled environment, unless they are replicated over both the server, and the cleint(s). To achieve this, you should usually store tools inside ReplicatedStorage
, and clone the tool in replicated storage into a certain player's backpack down the road instead, however do not store your remote event inside of the tool; instead make sure your remote event is stored directly in ReplicatedStorage (i.e. so you could say game.ReplicatedStorage.my_remote_event
)
From this point on, I will refer to your sword tool as 'Tool', and a standard remote event as 'r'
Typically to start a game, you would want to get the recent player that joined, and you typically do that with game.Players.PlayerAdded, however with filtering enabled, getting the player that joined is a bit different.
Insert a local script into game.StarterPlayer.StarterPlayerScripts
; It's code should be something along the lines of:
1 | local r = game.ReplicatedStorage.remote_event; |
this script is the script that fires to the server whenever its client joins the game. (this works because all local scripts are replicated to each client when they join a server)
Insert a server script (a script) into game.ServerScriptService
that can manage cloning and giving out the Tool to every player when they join, or are re-spawned; its code should be something along the lines of:
01 | local r = game.ReplicatedStorage.remote_event; |
02 | local my_tool = game.ReplicatedStorage.Tool; |
03 | r.OnServerEvent:connect( function (plr, info) |
04 | if info = = "Join" then |
06 | plr:WaitForChild( "Backpack" ); |
07 | my_tool:Clone().Parent = plr.Backpack; |
10 | plr.Changed:connect( function (prop) |
11 | if prop = = "Character" then |
12 | my_tool:Clone().Parent = plr.Backpack; |
I will be referring to this script as 'main' from this point on.
So now that the basics of Filtering enabled are all out of the way, lets get down to analyzing the code inside of the local script of your Tool; change its code to:
01 | local player = game.Players.LocalPlayer |
02 | local r = game.ReplicatedStorage.remote_event; |
04 | local attackanim = script:WaitForChild( "SlashAnim2" ) |
06 | local tool = script.Parent |
11 | local debounce = false ; |
12 | local attacking = false |
13 | local attack_debounce = false ; |
16 | tool.Activated:connect( function () |
19 | local hmd = player.Character:FindFirstChild( "Humanoid" ); |
20 | current_track = hmd:LoadAnimation(attackanim); |
24 | event = current_track.Stopped:connect( function () |
33 | tool.Handle.Touched:connect( function (part) |
34 | if not attack_debounce then |
35 | attack_debounce = true ; |
37 | local plr_hmd = part.Parent:FindFirstChild( "Humanoid" ) or part.Parent.Parent:FindFirstChild( "Humanoid" ); |
38 | if plr_hmd ~ = nil then |
39 | local plr_name = plr_hmd.Parent.Name; |
40 | if player.Name ~ = plr_name then |
41 | r:FireServer( "Hit" , plr_hmd.Parent, damage); |
45 | attack_debounce = false ; |
49 | tool.Unequipped:connect( function () |
50 | if current_track ~ = nil then |
52 | current_track:Destroy(); |
55 | attack_debounce = false ; |
We are using the same remote event for Tool, as we did for our join script.
So now we can go back and edit main's code to something more like this so that it can also handle whenever a player attacks another player:
01 | local r = game.ReplicatedStorage.remote_event; |
02 | local my_tool = game.ReplicatedStorage.Tool; |
03 | r.OnServerEvent:connect( function (plr, ...) |
04 | local inf_table = { ... } ; |
05 | if inf_table [ 1 ] = = "Join" then |
07 | plr:WaitForChild( "Backpack" ); |
08 | my_tool:Clone().Parent = plr.Backpack; |
09 | elseif inf_table [ 1 ] = = "Hit" then |
10 | if game.Players:GetPlayerFromCharacter(inf_table [ 2 ] ) then |
11 | inf_table [ 2 ] .Humanoid:TakeDamage(inf_table [ 3 ] ); |
15 | plr.Changed:connect( function (prop) |
16 | if prop = = "Character" then |
17 | my_tool:Clone().Parent = plr.Backpack; |
I am too tired to explain any more, if you are still having problems with it, send me details and I will see what I can do to help.
I hope this helps you.