Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make a mobile button that replicates a mouse click?

Asked by 4 years ago

Hi, so I have swords in my game and I want it so that when you press a button on a phone, it will make the sword attack. The tutorials I have seen for mobile buttons is always replicating a keyboard key and not the a mouse click. Please help.

Sword Script:

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:WaitForChild("ScytheBlade")

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.75)
            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)

SwordServer script (In ServerScriptService)

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) -- this will destroy that indicator after 2 seconds
                    CanAttack = true 
            end
        end
        end
        end
end)

1 answer

Log in to vote
0
Answered by
emervise 123
4 years ago

I'd reccomend seeing this tutorial:

https://developer.roblox.com/en-us/articles/ContextActionService-Creating-Mobile-Buttons

Ad

Answer this question