Hi, I have this sword which every time I click it will hit it will do an attacking animation and deal damage to a player. I was wondering how to make a phone button that replicates the mouse click function. I have looked through various youtube tutorials but they all seem to not help me in any way. I want the button to replicate the actions of when you click and not replicate the actions of when you press a key on a keyboard. Please help.
Sword Script (inside the tool):
wait(1)
script.Parent.Equipped:Connect(function()
local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animations.Idle)
wait(0.1)
idle:Play()
script.Parent.Unequipped:Connect(function()
idle:Stop()
end)
end)
--//Attacking Animations\\--
local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local CanAttack = true
local Anim_Name = "Attack"
local Audio = script.Parent:WaitForChild("Handle"):WaitForChild("Swing")
local Attack_Animations = script.Parent:WaitForChild("Animations")
local Attack_Num = 1
local blade = script.Parent.DaggerBlade
blade.Touched:Connect(function(hit)
if CanAttack == false then
game.ReplicatedStorage.Damage:FireServer(hit)
end
end)
UserInputService.InputBegan:Connect(function(InputObject)
if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
local Animation = script.Parent.Parent.Humanoid:LoadAnimation(Attack_Animations[Anim_Name..Attack_Num])
if CanAttack == true then
CanAttack = false
Animation:Play()
Audio:Play()
wait(0.6)
CanAttack = true
if Attack_Num == 1 then
Attack_Num = Attack_Num + 1
else if Attack_Num == 2 then
Attack_Num = 1
end
end
end
end
end)
SwordServerScript (inside ServerScriptStorage)
local CanAttack = true
local weaponsAndDamages = {}
weaponsAndDamages ["StarterSword"] = 45
weaponsAndDamages ["Hammer"] = 85
weaponsAndDamages ["Scythe"] = 55
weaponsAndDamages ["TripleBladeScythe"] = 220
weaponsAndDamages ["HeavySword"] = 160
weaponsAndDamages ["Dagger"] = 35
weaponsAndDamages ["GoldenDagger"] = 75
weaponsAndDamages ["Spear"] = 60
weaponsAndDamages ["NightmareBlade"] = 80
local weaponsAndRates = {}
weaponsAndRates ["StarterSword"] = 0.75
weaponsAndRates ["Hammer"] = 1
weaponsAndRates ["Scythe"] = 0.75
weaponsAndRates ["TripleBladeScythe"] = 1.3
weaponsAndRates ["HeavySword"] = 1.5
weaponsAndRates ["Dagger"] = 0.6
weaponsAndRates ["GoldenDagger"] = 0.8
weaponsAndRates ["Spear"] = 0.85
weaponsAndRates ["NightmareBlade"] = 0.75
local lastAttack = {}
game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(player, hit)
local weapon = player.Character:FindFirstChildWhichIsA("Tool") -- obtain the sword of the player attacking
if hit.Parent:FindFirstChild("Humanoid") ~= nil and player.Character ~= nil then
if hit.Parent.Humanoid.Health > 0 and player.Character.Humanoid.Health > 0 then -- check if the users are alive, we don't want dead players dealing damage, or players dealing damage to dead players.
if lastAttack[player.Name] == nil or tick() - lastAttack[player.Name] > weaponsAndRates[weapon.Name] then
if (hit.Parent.HumanoidRootPart.Position-player.Character.HumanoidRootPart.Position).magnitude < 40 then -- went with 40 just in case of serious latency. You can tweak this if wanted.
lastAttack[player.Name] = tick()
if weapon ~= nil then -- make sure it's not nil
hit.Parent.Humanoid:TakeDamage(weaponsAndDamages[weapon.Name])
weapon.Handle.Swing:Play() -- You want to play the hit noise on the sword of the player who is attacking, not the sword in the StarterPack.
end
end
end
end
end
end)
game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(player, hit)
local weapon = player.Character:FindFirstChildWhichIsA("Tool")
if hit.Parent:FindFirstChild("Humanoid") ~= nil and player.Character ~= nil then
if hit.Parent.Humanoid.Health > 0 and player.Character.Humanoid.Health > 0 then
local damage = (weaponsAndDamages[weapon.Name]) -- we create a variable which stores the damage inside of it, i randomized the damage between 5 and 20
if CanAttack == true then
CanAttack = false
local indicator = game:GetService("ReplicatedStorage"):FindFirstChild("DmgIndicator") -- we put the dmgindicator which in in the replicatedstorage inside a variable
if indicator then -- we check whether the indicator exists, if so then...
local gui = indicator:Clone() -- we clone it and store the cloned version inside a variable
gui.Parent = hit.Parent:FindFirstChild("HumanoidRootPart") -- we put the cloned version inside the enemy's rootpart
gui.Damage.Text = "-"..tostring(damage) -- we set the damage text to the damage we'll deal
gui.StudsOffset = Vector3.new(math.random(-2, 2), math.random(-2, 2), math.random(-2, 2)) -- we position our gui randomly on the enemy's body (between -2 and 2 studs)
wait(weaponsAndRates[weapon.Name])
game.Debris:AddItem(gui,0.1)
CanAttack = true -- we will destroy that indicator after 2 seconds
end
end
end
end
end)

1 comment
In line 7 and 8 in the first script, I did it so that when you click with the mouse it will attack and I did not use the tool.Activated function