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

how to make a clicking cool down when you click the sky ?

Asked by 3 years ago
Edited 3 years ago

A few hours ago i've made a pistol script (keep in mind that for the pistol script i used mouse.Target) but i met a error along the way in which i can't figure out a solution, so basically a pistol has a cool down, right ? it won't just shoot out bullets like a rifle so i made a cool down IF the mouse.Target is either terrain or a player, but what about the sky, the sky is equal to nil, how do i add a cool down if my mouse is pointing at the sky ? (debounce just messes up the script)

local handle = script.Parent.Handle
local tool = script.Parent
local player = game.Players.LocalPlayer
local gui = player:WaitForChild("PlayerGui")
local screenGui = gui:WaitForChild("ScreenGui")
local TextLabel = screenGui:WaitForChild("Ammo")
local mouse = player:GetMouse()
local mxAm = 10
local Ammo1 = mxAm
local serivce = game:GetService("UserInputService")
local oldIcon = mouse.Icon

local function ammo2()
    Ammo1 = mxAm
end

tool.Equipped:Connect(function()
    mouse.Icon = "http://www.roblox.com/asset/?id=79658449"
    TextLabel.Visible = true    
end)

tool.Activated:Connect(function()
    if tool.Enabled == true then
        if Ammo1 > 0 then
            Ammo1 = Ammo1 - 1
            if mouse.Target.Parent:FindFirstChild("Humanoid") then
                script.Parent.Damage:FireServer(mouse.Target)
                tool.Enabled = false
                print("target hit")
                wait(0.2)
                tool.Enabled = true
            else 
                tool.Enabled = false
                print("miss")
                wait(0.2)
                tool.Enabled = true
            end 
        end     
    end
end)    

while wait() do
    TextLabel.Text = Ammo1.."/"..mxAm

tool.Unequipped:Connect(function()
    mouse.Icon = oldIcon
    TextLabel.Visible = false
end)

serivce.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.R and Ammo1 ~= mxAm then
        wait(1)
        ammo2()
        end
    end)
end

1 answer

Log in to vote
1
Answered by 3 years ago

Here's a script with the cooldown applied, I also rewrote it for you.

if not game:IsLoaded() then
    repeat
        wait()
    until game:IsLoaded()
end

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character
local PlayerGui = LocalPlayer:FindFirstChildOfClass("PlayerGui")
local Mouse = LocalPlayer:GetMouse()

local ScreenGui = PlayerGui:FindFirstChildOfClass("ScreenGui")
local TextLabel = ScreenGui:FindFirstChild("Ammo")


local Tool = (script.Parent:IsA("Tool") and script.Parent) or script:FindFirstAncestorOfClass("Tool")
local Handle = Tool:FindFirstChild("Handle")

local MaxAmmo = 10
local OldMouseIcon = Mouse.Icon
local Bind = nil
local CooldownProgression = false

Tool.Equipped:Connect(function()
    Mouse.Icon = ("http://www.roblox.com/asset/?id=79658449");
    TextLabel.Visible = true
    Bind = Tool.Activated:Connect(function()
        if not Tool.Enabled or Ammo1 <= 0 or CooldownProgression == true then
            return
        end
        CooldownProgression = true
        Ammo1 = Ammo1 - 1
        local S = pcall(function()
            local TargetModel = (Mouse.Target:IsA("Model") and Mouse.Target) or (Mouse.Target.Parent:IsA("Model") and Mouse.Target.Parent) or Mouse.Target:FindFirstAncestorOfClass("Model")
            local TargetHumanoid = TargetModel:FindFirstChildOfClass("Humanoid")
            Tool.Enabled = false
            print("Target hit");
            wait(.2)
            Tool.Enabled = true
        end)
        if not S then
            Tool.Enabled = true
            print("Missed");
            wait(.2)
            Tool.Enabled = false
        end
        coroutine.wrap(function()
            wait(.25) -- Cooldown time.
            CooldownProgression = false
        end)()
    end)
    Tool.Unequipped:Wait(function()
        Bind:Disconnect()
    end)
end)

Tool.Unequipped:Connect(function()
    if Bind ~= nil and typeof(Bind) == "RBXScriptConnection" then
        Bind:Disconnect()
    end
    Mouse.Icon = OldMouseIcon
    TextLabel.Visible = false
    return
end)

UserInputService.InputBegan:Connect(function(InputObject,GameProcessedEvent)
    if GameProcessedEvent then
        return
    end
    if InputObject.KeyCode == Enum.KeyCode.R and Ammo1 ~= MaxAmmo then
        CooldownProgression = true -- Make this true so you can't fire while reloading...
        print("Reloading...")
        wait(1)
        Ammo1 = MaxAmmo
        print("Reloaded!")
        CooldownProgression = false -- Reloading complete, allow the user to fire.
    end
end)

coroutine.resume(coroutine.create(function()
    while true do
        RunService.Stepped:Wait()
        TextLabel.text = tostring(Ammo1) .. (" / ") .. tostring(MaxAmmo)
    end
end))
0
thanks but i am getting errors saying: attempt to index nil with 'Text' and attempt to index nil with 'Visible' Random_Haxer5 10 — 3y
0
i respect what you did but the gun breaks when you shoot at the sky, it just stops working :/ Random_Haxer5 10 — 3y
Ad

Answer this question