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

??? 17:58:04.496 Connect cannot be assigned to - Server - BackendScript:83

Asked by 2 years ago

Whenever I try to make the AK-47 work after it reloads, no matter WHAT I do, it always gives me an error

--//FrontEndScript
--//Handles back end checking + sounds
--//Variables\\--
local player
local tool = script.Parent.Parent
local handle = tool:WaitForChild("Handle")
local lastShot = tick()
local equipped = false
local reloading = false
local uis = game:GetService("UserInputService")

--//Folders\\--
local configs = tool:WaitForChild("Configs")
local remotes = tool:WaitForChild("Remotes")

--//Configs\\--
local ammo = configs:WaitForChild("Ammo")
local reserveAmmo = configs:WaitForChild("ReserveAmmo")
local magSize = configs:WaitForChild("MagSize")
local damage = configs:WaitForChild("Damage")
local fireRate = configs:WaitForChild("FireRate")
local headMultiplier = configs:WaitForChild("HeadMultiplier")
local range = configs:WaitForChild("Range")
local reloadTime = configs:WaitForChild("ReloadTime")

--//Remotes\\--
local canShootRemote = remotes:WaitForChild("CanShoot")
local canReloadRemote = remotes:WaitForChild("CanReload")
local reloadRemote = remotes:WaitForChild("Reload")
local hitRemote = remotes:WaitForChild("Hit")
local shootRemote = remotes:WaitForChild("Shoot")

--//Sounds\\--
local emptySound = handle:WaitForChild("EmptySound")
local reloadSound = handle:WaitForChild("ReloadSound")
local shootSound = handle:WaitForChild("ShootSound")
local headshotSound = handle:WaitForChild("HeadshotSound")

--//Functions\\--
local function canReload(plr)
    --//Return true if gun can be reloaded, else return false
    if ammo.Value < magSize.Value then --//Not full ammo
        if reserveAmmo.Value > 0 then --//Have any ammo to reload with
            if not reloading then --//Not currently reloading
                if equipped then --//Gun is equipped in the first place
                    return true
                end
            end
        end
    end

    --//If script got here, it means that the above wasn't ful-filled so return false
    return false
end


local function reload(plr)
    --//Initialize
    if not canReload() then return end

    --//Reload
    reloading = true
    reloadSound:Play()



    wait(reloadTime.Value)
    reloading = false
end

local function canShoot(plr, playSound)
    --//Return true if gun can be shot else return false
    if math.abs(lastShot - tick()) > (60/fireRate.Value) then --//Fire rate math/debounce
        if not reloading then --//Gun currently isn't reloading
            if equipped then --//Gun is equipped
                if ammo.Value > 0 then --//Ensure we have ammo in the mag
                    return true
                else
                    --//Play sound if needed
                    if playSound then
                        emptySound:Play()
                    end
                    function uis.InputBegan:Connect(inputObject, gameProcessEvent)
                        if inputObject.KeyCode == Enum.KeyCode.R then
                            return true
                        end
                    end

                end
            end
        end
    end

    --//If script got here, it means that the above wasn't ful-filled so return false
    return false
end

local function shoot()
    --//Initialize
    if not canShoot(player, true) then return end
    shootSound:Play()

    --//Handling
    lastShot = tick()
    ammo.Value = ammo.Value - 1
end

local function hit(plr, part)
    --//Initialize || Checks
    if not canShoot() then return end
    if not part then return end

    --//Get humanoid root part
    local character = player.Character or player.CharacterAdded:Wait()
    local rootPart = character:WaitForChild("HumanoidRootPart")

    if rootPart then
        --//Check distance
        if (rootPart.CFrame.p - part.CFrame.p).magnitude <= range.Value then --//Check range
            --//Get humanoid
            local humanoid

            --//Check if part was a hat/acessory
            if part.Name == "Handle" then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            else
                humanoid = part.Parent:FindFirstChild("Humanoid")
            end


            if humanoid and humanoid.Health > 0 then --//Do nothing if humanoid is dead
                local newDamage = damage.Value

                --//Check hit
                if part.Name == "Head" then
                    newDamage = newDamage * headMultiplier.Value

                    --//Headshot related handling
                    local soundCopy = headshotSound:Clone()
                    soundCopy.Parent = part
                    soundCopy:Play()
                end

                --//Apply damage
                humanoid:TakeDamage(newDamage)
            end
        end
    end
end

local function equip()
    equipped = true
    player = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent)
end

local function dequip()
    equipped = false
end

--//Event listeners\\--
tool.Equipped:Connect(equip)
tool.Unequipped:Connect(dequip)
hitRemote.OnServerEvent:Connect(hit)
shootRemote.OnServerEvent:Connect(shoot)
reloadRemote.OnServerEvent:Connect(reload)
canShootRemote.OnServerInvoke = canShoot
canReloadRemote.OnServerInvoke = canReload
--//FrontEndScript
--//Front end handling, animations, ray casting, hit detection.
--//Variables\\--
local tool = script.Parent.Parent
local uis = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local hole = tool:WaitForChild("Hole")
local handle = tool:WaitForChild("Handle")
local actionService = game:GetService("ContextActionService")
local mouseDown = false
local equipped = false

--//Folders\\--
local animations = tool:WaitForChild("Animations")
local configs = tool:WaitForChild("Configs")
local remotes = tool:WaitForChild("Remotes")

--//Animations\\--
local recoilAnim = animations:WaitForChild("Recoil")
local reloadAnim = animations:WaitForChild("Reload")
local holdAnim = animations:WaitForChild("Hold")

--//Animation Tracks\\--
local recoilTrack
local reloadTrack
local holdTrack

--//Configs\\--
local allowTracing = configs:WaitForChild("AllowTracing")
local range = configs:WaitForChild("Range")

--//Remotes\\--
local canShoot = remotes:WaitForChild("CanShoot")
local canReload = remotes:WaitForChild("CanReload")
local hitRemote = remotes:WaitForChild("Hit")
local reloadRemote = remotes:WaitForChild("Reload")
local shootRemote = remotes:WaitForChild("Shoot")

--//Assets\\--
local flashGui = hole:WaitForChild("FlashGui")

--//Functions\\--
local function equip()
    equipped = true

    --//Get humanoid
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")

    if humanoid then
        --//Load animations
        pcall(function()
            --//Hold animation
            holdTrack = humanoid:LoadAnimation(holdAnim)
            holdTrack:Play()
        end)

        pcall(function()
            --//Reload animation
            reloadTrack = humanoid:LoadAnimation(reloadAnim)
        end)

        pcall(function()
            --//Recoil animation
            recoilTrack = humanoid:LoadAnimation(recoilAnim)
        end)
    end
end

local function unequip()
    equipped = false

    --//Stop animations
    if holdTrack then
        holdTrack:Stop()
    end

    if reloadTrack then
        reloadTrack:Stop()
    end

    if recoilTrack then
        recoilTrack:Stop()
    end
end

local function reload()
    if canReload:InvokeServer() then
        --//Reload
        reloadRemote:FireServer()

        if reloadTrack then
            reloadTrack:Play()
        end
    end
end

local function fire()
    --//Checks
    if canShoot:InvokeServer() then
        --//Initialize
        if recoilTrack then
            recoilTrack:Play()
        end
        flashGui.Enabled = true

        --//Cast ray
        local ray = Ray.new(hole.CFrame.p, (mouse.hit.p - hole.CFrame.p).unit * range.Value)
        local touch, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        --//Hit detection
        if touch then
            hitRemote:FireServer(touch)
        end
        shootRemote:FireServer()

        --//Trace
        if allowTracing.Value then
            --//Create
            local trace = Instance.new("Part")
            trace.Anchored = trace
            trace.CanCollide = false
            trace.Transparency = 0.3
            trace.BrickColor = BrickColor.new("Bright yellow")
            trace.Material = Enum.Material.Neon

            --//Calculate
            local distance = (hole.CFrame.p - position).magnitude
            trace.Size = Vector3.new(0, 0, distance)
            trace.CFrame = CFrame.new(hole.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
            trace.Parent = workspace

            --//Clean-up
            game:GetService("Debris"):AddItem(trace, 0.1)

            wait(0.1)

            flashGui.Enabled = false
        end
    end
end

--//Event listeners\\--
tool.Equipped:Connect(function()
    equip()

    --//Automatic fire
    mouse.Button1Up:Connect(function()
        mouseDown = false
    end)

    tool.Activated:Connect(function()
        mouseDown = true
        repeat
            wait(0.01)
            fire()
        until not mouseDown
    end)
end)
tool.Unequipped:Connect(unequip)
actionService:BindAction("Reload", reload, false, Enum.KeyCode.R) --//Reload on R key press

1 answer

Log in to vote
0
Answered by
Puppynniko 1059 Moderation Voter
2 years ago

Why are is line 83 function uis.inputbegan:connect() userInputService works without function

0
Could you please show me how to do it without a function? Do I just remove the function? fakedudeforvideo 38 — 2y
0
yes just remove the function and the "End" that comes with it Puppynniko 1059 — 2y
Ad

Answer this question