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

[FE] Gun works when in StarterPack, but not when Cloned from ReplicatedStorage?

Asked by 7 years ago

Hello! I got a gun that works when the tool is in the StarterPack, but not when its cloned from ReplicatedStorage to Backpack.

For reference, this is a slightly modified script from the one AlvinBloxx provides in his 3 hour tutorial - ( https://www.youtube.com/watch?v=acJu605JNQE&t=1s ).

I feel this is probably an easy solution that I am overlooking, and apologies for all the scripts, I wanted to be as thorough as I could here.

When the player wants to equip an item, the item is cloned from the ReplicatedStorage to the Backpack with this script that fires when the user clicks a button inside their custom Inventory:

local p = game.Players.LocalPlayer

Item1.MouseButton1Click:connect(function()
local tool = game:GetService("ReplicatedStorage").Gun:Clone()
tool.Parent = p.Backpack
end)

And it clones the item just fine to the Backpack, appear on the bar in the bottom-center of the screen for the player to Equip.

I receive two errors, one from the Local Script inside the tool

This is the error I receive: "attempt to index local 'tool' (a nil value) " this line refers to the "tool" in the following Local Script inside the Gun/Tool on Line 25:

local tool = script.Parent -- Getting the tool
local player = game:GetService("Players").LocalPlayer -- Getting the player
local mouse = player:GetMouse() -- Getting the mouse
local sound = tool:WaitForChild("Gunfire")
local torso = "" -- Nothing for now.
local reloading = false -- Variable to check if we are currently reloading
local contextActionService = game:GetService("ContextActionService") -- Allow us to cater for Mobile players
local bodytype = nil -- Nil for now but will check whether player is R6 or R15
local difference = 0 -- Difference between position of head and mouse
local replicatedstorage = game:GetService("ReplicatedStorage").Ranged.GunRemotes
local gungui = tool:WaitForChild("GunGUI")
local bullets = tool:WaitForChild("Bullets")
local reloadtime = 1
-- 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")
--FindBodyType
function findBodyType() -- Used to determine whether a player is R6 or R15
    bodytype = checkBodyType:InvokeServer(tool) -- Invoking the Remotefunction to do a check on the server
    print(bodytype)
end
-- Reloading function
function reload()
    reloading = true
    reload2:FireServer(tool.reload)
    mouse.Icon = "http://www.roblox.com/asset?id=936489163"
    player.PlayerGui:WaitForChild("GunGUI").Bullets.Text = "Recharging!"
    wait(reloadtime)
    bullets.Value = 6
    player.PlayerGui:WaitForChild("GunGUI").Bullets.Text = "Bullets: "..bullets.Value -- REWORD THIS FOR MAGIC POINTS
    mouse.Icon = "http://www.roblox.com/asset?id=936803874"
    equipAnimation:FireServer(tool.shoot)
    reloading = false
end
-- When the tool is equipped, the following event will run
tool.Equipped:Connect(function(mouse)
    gungui:Clone().Parent = player.PlayerGui -- We are cloning the Gun GUI into the player's PlayerGUI
    findBodyType() -- Calling the function above to check the body type.
    equipAnimation:FireServer(tool.shoot) -- Calling the equip animation remoteevent so that the server can play the animation
    mouse.Icon = "http://www.roblox.com/asset?id=936803874"
    mouse.Button1Down:Connect(function()
        if bullets.Value <=0 or reloading == true then
            -- Don't do anything
        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) -- Last number is how many studs the bullet will travel.
            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 = contextActionService:BindAction("ReloadBtn",reload,true,"r")
    contextActionService:SetPosition("ReloadBtn",UDim2.new(0.72,-25,0.20,-25))
    contextActionService:SetImage("ReloadBtn","http://www.roblox.com/asset/?id=10952419") -- CHANGE THIS IF YOU WANT TO CHANGE THE RELOAD BUTTON FOR MOBILE.
end)
tool.Unequipped:Connect(function()
    mouse.Icon = ""
    unequipanimation:FireServer(tool.shoot)
    player.PlayerGui.GunGUI:Destroy()
    contextActionService:UnbindAction("ReloadBtn")
end)
headshot.OnClientEvent:Connect(function()
    player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim2.new(0.5,-100,0.5,-25), "Out","Quint",0.3)
    wait(1.5)
    player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim2.new(-1,0,0.5,-25), "In","Quint",0.4)
    wait(0.5)
    player.PlayerGui.GunGUI.Headshot.Position = UDim2.new(1.5,0,0.5,-25)
end)

And when it fires, it gets hung on the animation portion from the Script inside ServerScriptStorage.

The Script inside ServerScriptStorage is below, the script will break on Line 31/36, depending if the Player is a R6 or R15 model over with the error saying: attempt to index local 'tool' (a nil value):

local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")["Ranged"]["GunRemotes"]
local hitSound = game.Workspace.HitSounds.BloodHit


replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part)
-- Gun Shooting/Damage portion, no errors here

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()
        wait(1)
        reloadAnim:Stop()
    end)
end)

function checkBodyType(player,tool)
    if game.Workspace[player.Name]:FindFirstChild("LowerTorso") then -- R15
        tool.shoot.AnimationId = "rbxassetid://1101572628"
        tool.reload.AnimationId = "rbxassetid://1101580893"
        return "R15"
    end
    if game.Workspace[player.Name]:FindFirstChild("Torso") then -- R6
        tool.shoot.AnimationId = "rbxassetid://1101542983"
        tool.reload.AnimationId = "rbxassetid://1101549236"
        return "R6"
    end
end

replicatedStorage.CheckBodyType.OnServerInvoke = checkBodyType


What I have tried so far: In the local script, I have changed Line 25 from:

bodytype = checkBodyType:InvokeServer(tool)

to

bodytype = checkBodyType:InvokeServer(player,tool)

At which point the ServerScriptStorage says that "shoot" from the is not a valid member of Player.

I'm still not sure why the Tool works just fine if its in the StarterPack but not when cloned from ReplicatedStorage. =/ I would appreciate any/all ideas. Thanks!

1
You cannot pass instances through remotes. Pass the name of the tool, and search for it on the server. Goulstem 8144 — 7y
0
My apologies if I'm missing something basic here. Except it does work when its in the player's StarterPack, the only time these problems occur is when they're cloned from ReplicatedStorage to the Backpack. =/ Never2Humble 90 — 7y
1
@Goulstem You are able to pass instances as long as they are accessible by both sides ie in the workspace User#5423 17 — 7y
0
@kingdom5 Thanks for the response. :) Do you have any thoughts as to the problem? Never2Humble 90 — 7y

1 answer

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

local tool = script.Parent -- Getting the tool this bit is wrong where is your tool it needs to be in replicated storage then you say local tool =script.Parent.Parent.(WaitForChild)'<toolname> that should work as long as the tool is in replicated storage and script is in server script service

Ad

Answer this question