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:
local r = game.ReplicatedStorage.remote_event;
r:FireServer("Join");
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:
local r = game.ReplicatedStorage.remote_event;
local my_tool = game.ReplicatedStorage.Tool;
r.OnServerEvent:connect(function(plr, info)
if info == "Join" then
plr:WaitForChild("Backpack");
my_tool:Clone().Parent = plr.Backpack;
end
plr.Changed:connect(function(prop)
if prop == "Character" then
my_tool:Clone().Parent = plr.Backpack;
end
end)
end)
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:
local player = game.Players.LocalPlayer
local r = game.ReplicatedStorage.remote_event;
local attackanim = script:WaitForChild("SlashAnim2")
local tool = script.Parent
local damage = 30
local current_track;
local debounce = false;
local attacking = false
local attack_debounce = false;
tool.Activated:connect(function()
if not debounce then
debounce = true;
local hmd = player.Character:FindFirstChild("Humanoid");
current_track = hmd:LoadAnimation(attackanim);
attacking = true;
current_track:Play();
local event;
event = current_track.Stopped:connect(function()
attacking = false;
wait(0.2);
debounce = false;
event:disconnect();
end);
end
end)
tool.Handle.Touched:connect(function(part)
if not attack_debounce then
attack_debounce = true;
if attacking then
local plr_hmd = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid");
if plr_hmd ~= nil then
local plr_name = plr_hmd.Parent.Name;
if player.Name ~= plr_name then
r:FireServer("Hit", plr_hmd.Parent, damage);
end
end
end
attack_debounce = false;
end
end)
tool.Unequipped:connect(function()
if current_track ~= nil then
current_track:Stop();
current_track:Destroy();
current_track = nil;
end
attack_debounce = false;
attacking = false;
debounce = false;
end)
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:
local r = game.ReplicatedStorage.remote_event;
local my_tool = game.ReplicatedStorage.Tool;
r.OnServerEvent:connect(function(plr, ...)
local inf_table = {...};
if inf_table[1] == "Join" then
plr:WaitForChild("Backpack");
my_tool:Clone().Parent = plr.Backpack;
elseif inf_table[1] == "Hit" then
if game.Players:GetPlayerFromCharacter(inf_table[2]) then
inf_table[2].Humanoid:TakeDamage(inf_table[3]);
end
end
plr.Changed:connect(function(prop)
if prop == "Character" then
my_tool:Clone().Parent = plr.Backpack;
end
end)
end)
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.
3 comments
Sorry it took me a while to answer, but this answer is a pretty long one.
@laczka You really seem to know what you're doing. Thanks! ~Loughdough
I am always glad to help. :)