Tools breaking when moved from ReplicatedStorage to StarterGear?
I made this script that gives a player an item when bought with in-game currency. The way it works is when the player has enough coins and clicks the "Buy" button the game clones a tool from ReplicatedStorage to the players StarterGear, backpack.
01 | local player = game.Players.LocalPlayer |
02 | local leaderboard = player:WaitForChild( "leaderstats" ) |
03 | local button = script.Parent |
04 | local price = button:WaitForChild( "Price" ) |
05 | local item = button:WaitForChild( "ItemName" ) |
06 | local rs = game:GetService( "ReplicatedStorage" ) |
08 | button.MouseButton 1 Click:connect( function () |
09 | if leaderboard.Coins.Value > = price.Value then |
10 | leaderboard.Coins.Value = leaderboard.Coins.Value - price.Value |
11 | local item = rs:WaitForChild(item.Value) |
12 | item:Clone().Parent = player.StarterGear |
13 | item:Clone().Parent = player.Backpack |
18 | button.image = item.Value.. " - " ..price.Value |
For some reason when the tool is moved the animations in the tool stop working.
Here is the Animations script:
01 | local function WaitForChild(parent, childName) |
02 | while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end |
03 | return parent [ childName ] |
06 | local Tool = script.Parent |
13 | local function PlayAnimation(animationName) |
14 | if Animations [ animationName ] then |
15 | Animations [ animationName ] :Play() |
19 | local function StopAnimation(animationName) |
20 | if Animations [ animationName ] then |
21 | Animations [ animationName ] :Stop() |
26 | function OnEquipped(mouse) |
27 | MyCharacter = Tool.Parent |
28 | MyHumanoid = WaitForChild(MyCharacter, 'Humanoid' ) |
30 | Animations [ 'EquipAnim' ] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'EquipAnim5' )) |
31 | Animations [ 'IdleAnim' ] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'IdleAnim3' )) |
32 | Animations [ 'OverheadAnim' ] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'OverheadAnim2' )) |
33 | Animations [ 'SlashAnim' ] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'SlashAnim2' )) |
34 | Animations [ 'ThrustAnim' ] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'ThrustAnim2' )) |
35 | Animations [ 'UnequipAnim' ] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'UnequipAnim2' )) |
37 | PlayAnimation( 'EquipAnim' ) |
38 | PlayAnimation( 'IdleAnim' ) |
41 | function OnUnequipped() |
42 | for animName, _ in pairs (Animations) do |
43 | StopAnimation(animName) |
47 | Tool.Equipped:connect(OnEquipped) |
48 | Tool.Unequipped:connect(OnUnequipped) |
50 | WaitForChild(Tool, 'PlaySlash' ).Changed:connect( |
52 | PlayAnimation( 'SlashAnim' ) |
55 | WaitForChild(Tool, 'PlayThrust' ).Changed:connect( |
57 | PlayAnimation( 'ThrustAnim' ) |
60 | WaitForChild(Tool, 'PlayOverhead' ).Changed:connect( |
62 | PlayAnimation( 'OverheadAnim' ) |
If anyone knows why the animations break when the tool is moved to StarterGear it will help me a ton. Thanks!