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:
1 | local p = game.Players.LocalPlayer |
3 | Item 1. MouseButton 1 Click:connect( function () |
4 | local tool = game:GetService( "ReplicatedStorage" ).Gun:Clone() |
5 | tool.Parent = p.Backpack |
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:
01 | local tool = script.Parent |
02 | local player = game:GetService( "Players" ).LocalPlayer |
03 | local mouse = player:GetMouse() |
04 | local sound = tool:WaitForChild( "Gunfire" ) |
06 | local reloading = false |
07 | local contextActionService = game:GetService( "ContextActionService" ) |
10 | local replicatedstorage = game:GetService( "ReplicatedStorage" ).Ranged.GunRemotes |
11 | local gungui = tool:WaitForChild( "GunGUI" ) |
12 | local bullets = tool:WaitForChild( "Bullets" ) |
15 | local equipAnimation = replicatedstorage:WaitForChild( "EquipAnimation" ) |
16 | local headshot = replicatedstorage:WaitForChild( "Headshot" ) |
17 | local reload 2 = replicatedstorage:WaitForChild( "Reload" ) |
18 | local shootevent = replicatedstorage:WaitForChild( "ShootEvent" ) |
19 | local unequipanimation = replicatedstorage:WaitForChild( "UnequipAnimation" ) |
21 | local checkBodyType = replicatedstorage:WaitForChild( "CheckBodyType" ) |
22 | local fetchBulletsLeft = replicatedstorage:WaitForChild( "FetchBulletsLeft" ) |
24 | function findBodyType() |
25 | bodytype = checkBodyType:InvokeServer(tool) |
31 | reload 2 :FireServer(tool.reload) |
33 | player.PlayerGui:WaitForChild( "GunGUI" ).Bullets.Text = "Recharging!" |
36 | player.PlayerGui:WaitForChild( "GunGUI" ).Bullets.Text = "Bullets: " ..bullets.Value |
38 | equipAnimation:FireServer(tool.shoot) |
42 | tool.Equipped:Connect( function (mouse) |
43 | gungui:Clone().Parent = player.PlayerGui |
45 | equipAnimation:FireServer(tool.shoot) |
47 | mouse.Button 1 Down:Connect( function () |
48 | if bullets.Value < = 0 or reloading = = true then |
51 | local head = game.Workspace [ player.Name ] .Head.CFrame.lookVector |
52 | local mouse = CFrame.new(game.Workspace [ player.Name ] .Head.Position,mouse.Hit.p).lookVector |
53 | difference = (head-mouse) |
54 | local ray = Ray.new(tool.Handle.CFrame.p,(player:GetMouse().Hit.p - tool.Handle.CFrame.p).unit* 300 ) |
55 | local part,position = game.Workspace:FindPartOnRay(ray,player.Character, false , true ) |
57 | if difference.magnitude < 1.33 then |
58 | shootevent:FireServer(tool,position,part) |
59 | bullets.Value = bullets.Value - 1 |
63 | local reloadMobileButton = contextActionService:BindAction( "ReloadBtn" ,reload, true , "r" ) |
64 | contextActionService:SetPosition( "ReloadBtn" ,UDim 2. new( 0.72 ,- 25 , 0.20 ,- 25 )) |
67 | tool.Unequipped:Connect( function () |
69 | unequipanimation:FireServer(tool.shoot) |
70 | player.PlayerGui.GunGUI:Destroy() |
71 | contextActionService:UnbindAction( "ReloadBtn" ) |
73 | headshot.OnClientEvent:Connect( function () |
74 | player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim 2. new( 0.5 ,- 100 , 0.5 ,- 25 ), "Out" , "Quint" , 0.3 ) |
76 | player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim 2. new(- 1 , 0 , 0.5 ,- 25 ), "In" , "Quint" , 0.4 ) |
78 | player.PlayerGui.GunGUI.Headshot.Position = UDim 2. new( 1.5 , 0 , 0.5 ,- 25 ) |
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):
01 | local serverStorage = game:GetService( "ServerStorage" ) |
02 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) [ "Ranged" ] [ "GunRemotes" ] |
03 | local hitSound = game.Workspace.HitSounds.BloodHit |
06 | replicatedStorage.ShootEvent.OnServerEvent:Connect( function (player,tool,position,part) |
09 | replicatedStorage.EquipAnimation.OnServerEvent:Connect( function (player,animation) |
10 | local newAnim = game.Workspace [ player.Name ] .Humanoid:LoadAnimation(animation) |
12 | replicatedStorage.UnequipAnimation.OnServerEvent:Connect( function (player,animation) |
14 | for i,v in pairs (game.Workspace:GetChildren()) do |
15 | if v.Name = = player.Name.. "'s Trajectory" then |
20 | replicatedStorage.Reload.OnServerEvent:Connect( function (player,animation) |
22 | local reloadAnim = game.Workspace [ player.Name ] .Humanoid:LoadAnimation(animation) |
29 | function checkBodyType(player,tool) |
30 | if game.Workspace [ player.Name ] :FindFirstChild( "LowerTorso" ) then |
35 | if game.Workspace [ player.Name ] :FindFirstChild( "Torso" ) then |
42 | replicatedStorage.CheckBodyType.OnServerInvoke = checkBodyType |
What I have tried so far:
In the local script, I have changed Line 25 from:
1 | bodytype = checkBodyType:InvokeServer(tool) |
to
1 | 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!