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

Gun script i'm making wont work properly? Animations wont load.

Asked by 5 years ago
Edited 5 years ago

So i found this youtube video by AlvinBlox for a gun tutorial (https://www.youtube.com/watch?v=acJu605JNQE), i tried to follow that editing the script in some places but it wont work properly? in the script i will mark where some errors are, if you could help it would be amazing!

--// Local Script //--
--// Variable's //--
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local sound = tool:WaitForChild("GunFire")
local torso = ""
local reloading = false -- debounce for the reload.
local CAS = game:GetService("ContextActionService") -- allows for mobile players to shoot
local bodytype = nil
local difference = 0 -- Length between the head of the player shooting and the mouse.
local replicatedstorage = game:GetService("ReplicatedStorage")
local gungui = tool:WaitForChild("GunGui")
local bullets = tool:WaitForChild("Bullets")
local reloadtime = 8
--// Remote Events //--
local equipAnimation = replicatedstorage:WaitForChild("EquipAnimation")
local headshot = replicatedstorage:WaitForChild("Headshot")
local reload2 = replicatedstorage:WaitForChild("Reload")
local shootevent = replicatedstorage:WaitForChild("ShootEvent")
local unequipanimation = replicatedstorage:WaitForChild("UnequipAnimation")

--// Remote Functions //--
local checkBodyType = replicatedstorage:WaitForChild("CheckBodyType")
local fetchBulletsLeft = replicatedstorage:WaitForChild("FetchBulletsLeft")

--// Functions //--

function findBodyType() -- Checks if the player is R6 or R15
    bodytype = checkBodyType:InvokeServer(tool)
    print(bodytype) -- Remove this after testing.
end

function reload()
    reloading = true
    reload2:FireServer(tool.Reload)
    mouse.Icon = "http://www.roblox.com/asset?id=936489163"
    player.PlayerGui:WaitForChild("GunGui").Bullets.Text = "Reloading.."
    wait(reloadtime)
    bullets.Value = 30
    player.PlayerGui:WaitForChild("GunGui").Bullets.Text = bullets.Value
    mouse.Icon = "http://www.roblox.com/asset?id=936803874"
    equipAnimation:FireServer(tool.Shoot)
    reloading = false 
end

--// Main Script //--

tool.Equipped:Connect(function(mouse) -- Runs when the gun is equipped.
    gungui:Clone().Parent = player.PlayerGui -- clones the gun gui into the player gui.
    findBodyType() -- checks the players body type.
    equipAnimation:FireServer(tool.Shoot)
    mouse.Icon = "http://www.roblox.com/asset?id=936803874" -- Changes mouse icon to a customn one.
    mouse.Button1Down:Connect(function()
        if bullets.Value <= 0 or reloading == true then
        else
            local head = game.Workspace[player.Name].Head.CFrame.LookVector
            local mouse = CFrame.new(game.Workspace[player.Name].Head.Position, mouse.Hit.p).lookVector
            difference = (head-mouse)
            local ray = Ray.new(tool.Handle.CFrame.p,(player:GetMouse().Hit.p - tool.Handle.CFrame.p).unit * 300)
            local part,position = game.Workspace:FindPartOnRay(ray, player.Character, false, true)
            sound:Play()
            if difference.magnitude < 1.33 then
                shootevent:FireServer(tool, position, part)
                bullets.Value = bullets.Value - 1

            end
        end
    end)
    local reloadMobileButton = CAS:BindAction("ReloadBtn", reload, true, "r")
    CAS:SetPosition("ReloadBtn", UDim2.new(0.72,-25,0.2,-25))
    CAS:SetImage("ReloadBtn", "http://www.roblox.com/asset/?id=10952419")
end)

tool.Unequipped:Connect(function()
    mouse.Icon = ""
    unequipanimation:FireServer(tool.Shoot)
    player.PlayerGui.GunGui:Destroy()
    CAS:UnbindAction("ReloadBtn")
end)

headshot.OnClientEvent:Connect(function()
    player.PlayerGui.GunGui.Headshot:TweenPosition(UDim2.new(0.854,0,0.874,0),"InOut", "Back", .5) 
    wait(.2)
    for i = 1,5 do
        player.PlayerGui.GunGui.Headshot.TextTransparency = player.PlayerGui.GunGui.Headshot.TextTransparency - 0.01
        wait()
    player.PlayerGui.GunGui.Headshot:TweenPosition(UDim2.new(0.854,0,1.05,0),"InOut", "Linear", .001)
    wait(.01)
    end
end)
--// Server script //--
--// Variables //--
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage") 

--// Main Script //--
replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player, tool, position, part)
    local damage = tool:WaitForChild("Dmg").Value
    if game.Workspace[player.Name].Humanoid.Health <= 0 then
    else
        local distance = (tool.Handle.CFrame.p - position).magnitude
        if game.Workspace:FindFirstChild(player.Name.."'s Trajectory") then
            game.Workspace:FindFirstChild(player.Name.."'s Trajectory"):Destroy()
        end
        local trajectory = Instance.new("Part", game.Workspace)
        local smoke = serverStorage.SmokeParticle:Clone()
        smoke.Parent = tool.Handle
        trajectory.BrickColor = BrickColor.new("Institutional white")
        trajectory.Material = "SmoothPlastic"
        trajectory.Name = player.Name.."'s Trajectory"
        trajectory.Transparency = 0.6
        trajectory.Anchored = true
        trajectory.Locked = true
        trajectory.CanCollide = false
        trajectory.Size = Vector3.new(0.3,0.3,distance)
        for i = 0,distance,6 do
            trajectory.CFrame = CFrame.new(tool.Handle.CFrame.p,position) * CFrame.new(0,0,-distance / 2)
            wait(0.0001)
        end
        smoke:Destroy()
        if part then
            if part.Name == "Head" or part:IsA("Hat") and part.Parent:FindFirstChild("Humanoid").Health > 0 then -- add "or part:IsA("Hat")" after "Head" for a update ;)
                replicatedStorage.Headshot:FireClient(player)
                damage = tool:WaitForChild("HeadshotDmg").Value
            end
-------------- It says that part.Parent is nil as a error ----------------
            local humanoid = part.Parent:FindFirstChild("Humanoid")
            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            else
                humanoid:TakeDamage(damage)
                if humanoid.Health <= 0 then --Checks if the player is dead.
                    if game.Players[humanoid.Parent.Name]:WaitForChild("Criminal" or "Prisioner").Value == true then -- if the player is a criminal/prisioner then there backpack gets cleared.
                        game.Players[humanoid.Parent.Name].Backpack:GetChildren():Destroy()
                        else -- if not then nothing happens (for cops)
                    end
                end
            end
            wait(.5)
            if trajectory then
                trajectory:Destroy()
            end
        end
    end
end)

replicatedStorage.EquipAnimation.OnServerEvent:Connect(function(player, animation)
    local newAnim = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
    newAnim:Play()
    replicatedStorage.UnequipAnimation.OnServerEvent:Connect(function(player, animation)
        newAnim:Stop()
        for i,v in pairs(game.Workspace:GetChildren()) do
            if v.Name == player.Name.."'s Trajectory" then
                v:Destroy()
            end
        end
    end)
    replicatedStorage.Reload.OnServerEvent:Connect(function(player, animation)
        newAnim:Stop()
        local reloadAnim = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
        reloadAnim:Play()
    end)
end)
-----------Wont load the animations when the gun is equipped :/ ---------- 
function checkBodyType(player,tool)-- checks players body type.
    if game.Workspace[player.Name]:FindFirstChild("LowerTorso") then -- checks if it is R15
        tool.Shoot.AnimationId = "rbxassetid://1963059578"
        tool.Reload.AnimationId = "rbxassetid://1963120847"
        return "R15"
    end
    if game.Workspace[player.Name]:FindFirstChild("Torso") then -- checks if it is R6
        tool.Shoot.AnimationId = "rbxassetid://1963840586"
        tool.Reload.AnimationId = "rbxassetid://1963842638"
        return "R6"
    end
end

replicatedStorage.CheckBodyType.OnServerInvoke = checkBodyType

if you could go through and see if there are any more errors and possibly fix them that would be amazing! please tell me if you need more explaining on some problems, thanks!

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Local Script Line 57. It’s lookVector, not LookVector.

Server Script Line 43. Line 43 should look like this:

if game:GetService("Players")[humanoid.Parent.Name].Team.Name == "Criminal" then
    game:GetService("Players")[humanoid.Parent.Name].Backpack:ClearAllChildren()
end
0
changed that but it really didnt effect anything :/ cruizer_snowman 117 — 5y
Ad

Answer this question