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

How do I reload on Mobile?

Asked by 6 years ago

How do I create a button to Reload a weapon on a Mobile device? I know from the Wikis it has to do with UserInputService and API:Class/InputObject , but I'm really confused how to get this to work right, the wiki pages confuse me there. Do I insert the script with the Reload event after:

mouse.KeyDown:connect(function(key) ??

I'd appreciate any help. My gun script is below:

repeat wait() until script.Parent.Parent.Config
--//Variables
local plr = game.Players.LocalPlayer
local tool = script.Parent.Parent
local hole = tool.Hole
local handle = tool.Handle
local debounce = true
local config = tool.Config
local range = config.Range
local dmg = config.Damage
local coolDown = config.CoolDown
local clips = config.Clips
local ammo = config.Ammo
local maxAmmo = config.MaxAmmo
local allowTracing = config.AllowTracing
local reloadTime = config.ReloadTime
local isReloading = false
local inLoop = false
local burstSize = 1 -- How many bullets shot each burst
--//Sounds
local reloadSound = tool.Handle.ReloadSound
local fireSound = tool.Handle.FireSound
local equipSound = tool.Handle.EquipSound
local emptySound = tool.Handle.EmptySound

--//Events
tool.Equipped:connect(function(mouse)
    equipSound:Play()
    tool.Activated:connect(function()
        if inLoop == false then
            inLoop = true
            for i = 1, burstSize do
                if not isReloading then
                    if ammo.Value > 0 then
                        if debounce then
                            --//Play sound
                            fireSound:Play()
                            --//Doesn't allow spamming
                            debounce = false
                            --//Ray Get, Set
                            local ray = Ray.new(hole.CFrame.p, (mouse.Hit.p - hole.CFrame.p) * range.Value)
                            local hit, position = workspace:FindPartOnRay(ray, plr.Character, false, true)

                            --//Bullet Tracing
                            if allowTracing.Value == true then
                                --//Make part
                                local trace = Instance.new("Part", workspace)
                                trace.Material = Enum.Material.Neon
                                trace.BrickColor = BrickColor.new("Black")
                                trace.CanCollide = false
                                trace.Anchored = true
                                trace.Transparency = 0.5

                                --//Show Direction
                                local distance = (hole.CFrame.p - position).magnitude
                                trace.Size = Vector3.new(0.2, 0.2, distance)
                                trace.CFrame = CFrame.new(hole.CFrame.p, position) * CFrame.new(0, 0, -distance/2)

                                --//Remove debris
                                game:GetService("Debris"):AddItem(trace, 0.1)
                            end

                            --//Hit Detection
                            if hit then
                                local humanoid =  hit.Parent:FindFirstChild("Humanoid")
                                if humanoid then
                                    if hit.Name == "Head" then
                                        --//Double damage on headshots
                                        humanoid:TakeDamage(dmg.Value*2)
                                    else
                                        --//Normal Damage on body shots
                                        humanoid:TakeDamage(dmg.Value)
                                    end
                                end
                            end
                            ammo.Value = ammo.Value - 1
                            debounce = true
                        end
                    else
                        --//Out of ammo
                        emptySound:Play()
                    end
                end
                wait(coolDown.Value)
            end
            inLoop = false
        end
    end)

    --//Reload Event
    mouse.KeyDown:connect(function(key)
        key:lower()
        if key == "r" then
            if not isReloading then
                if clips.Value > 0 then
                    reloadSound:Play()
                    isReloading = true
                    ammo.Value = maxAmmo.Value
                    clips.Value = clips.Value - 1
                    wait(reloadTime.Value)
                    isReloading = false
                end
            end
        end
    end)
end)
0
you're better off not worrying about mobile/tablet devices. They will cause more confusion and problems than solutions abnotaddable 920 — 6y
0
Maybe so, but I want as close to compatibility between mobile/tablet players and PC players as I can get. I've already got a button that changes/simplifies the user interface, just need the reload button now. :) Never2Humble 90 — 6y
0
ContextActionService has simple API to create a mobile button connected to a function. cabbler 1942 — 6y
0
Thanks for the suggestion, do you know how I would implement this? Never2Humble 90 — 6y

Answer this question