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:

1local p = game.Players.LocalPlayer
2 
3Item1.MouseButton1Click:connect(function()
4local tool = game:GetService("ReplicatedStorage").Gun:Clone()
5tool.Parent = p.Backpack
6end)

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:

01local tool = script.Parent -- Getting the tool
02local player = game:GetService("Players").LocalPlayer -- Getting the player
03local mouse = player:GetMouse() -- Getting the mouse
04local sound = tool:WaitForChild("Gunfire")
05local torso = "" -- Nothing for now.
06local reloading = false -- Variable to check if we are currently reloading
07local contextActionService = game:GetService("ContextActionService") -- Allow us to cater for Mobile players
08local bodytype = nil -- Nil for now but will check whether player is R6 or R15
09local difference = 0 -- Difference between position of head and mouse
10local replicatedstorage = game:GetService("ReplicatedStorage").Ranged.GunRemotes
11local gungui = tool:WaitForChild("GunGUI")
12local bullets = tool:WaitForChild("Bullets")
13local reloadtime = 1
14-- Remote Events
15local equipAnimation = replicatedstorage:WaitForChild("EquipAnimation")
View all 79 lines...

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):

01local serverStorage = game:GetService("ServerStorage")
02local replicatedStorage = game:GetService("ReplicatedStorage")["Ranged"]["GunRemotes"]
03local hitSound = game.Workspace.HitSounds.BloodHit
04 
05 
06replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part)
07-- Gun Shooting/Damage portion, no errors here
08 
09replicatedStorage.EquipAnimation.OnServerEvent:Connect(function(player,animation)
10    local newAnim = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
11    newAnim:Play()
12    replicatedStorage.UnequipAnimation.OnServerEvent:Connect(function(player,animation)
13        newAnim:Stop()
14        for i,v in pairs(game.Workspace:GetChildren()) do
15            if v.Name == player.Name.."'s Trajectory" then
View all 42 lines...

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

1bodytype = checkBodyType:InvokeServer(tool)

to

1bodytype = 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