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.
local player = game.Players.LocalPlayer local leaderboard = player:WaitForChild("leaderstats") local button = script.Parent local price = button:WaitForChild("Price") local item = button:WaitForChild("ItemName") local rs = game:GetService("ReplicatedStorage") button.MouseButton1Click:connect(function() if leaderboard.Coins.Value >= price.Value then leaderboard.Coins.Value = leaderboard.Coins.Value - price.Value local item = rs:WaitForChild(item.Value) item:Clone().Parent = player.StarterGear item:Clone().Parent = player.Backpack end end) while wait() do button.image = item.Value.." - "..price.Value end
For some reason when the tool is moved the animations in the tool stop working. Here is the Animations script:
local function WaitForChild(parent, childName) while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end return parent[childName] end local Tool = script.Parent local Animations = {} local MyHumanoid local MyCharacter local function PlayAnimation(animationName) if Animations[animationName] then Animations[animationName]:Play() end end local function StopAnimation(animationName) if Animations[animationName] then Animations[animationName]:Stop() end end function OnEquipped(mouse) MyCharacter = Tool.Parent MyHumanoid = WaitForChild(MyCharacter, 'Humanoid') if MyHumanoid then Animations['EquipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'EquipAnim5')) Animations['IdleAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'IdleAnim3')) Animations['OverheadAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'OverheadAnim2')) Animations['SlashAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'SlashAnim2')) Animations['ThrustAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'ThrustAnim2')) Animations['UnequipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'UnequipAnim2')) end PlayAnimation('EquipAnim') PlayAnimation('IdleAnim') end function OnUnequipped() for animName, _ in pairs(Animations) do StopAnimation(animName) end end Tool.Equipped:connect(OnEquipped) Tool.Unequipped:connect(OnUnequipped) WaitForChild(Tool, 'PlaySlash').Changed:connect( function (value) PlayAnimation('SlashAnim') end) WaitForChild(Tool, 'PlayThrust').Changed:connect( function (value) PlayAnimation('ThrustAnim') end) WaitForChild(Tool, 'PlayOverhead').Changed:connect( function (value) PlayAnimation('OverheadAnim') end)
If anyone knows why the animations break when the tool is moved to StarterGear it will help me a ton. Thanks!
I think it’s not working because you’re cloning the tool locally. If you clone locally, the whole tool breaks, and other players can’t see you holding the tool. Instead, you should use a remote event and clone the tool in a server (normal) script.
Here is what you’d do :
local player = game.Players.LocalPlayer local leaderboard = player:WaitForChild("leaderstats") local button = script.Parent local price = button:WaitForChild("Price") local item = button:WaitForChild("ItemName") local rs = game:GetService("ReplicatedStorage") local remoteevent = rs:WaitForChild('RemoteEvent') -- Add a remote event into ReplicatedStorage button.MouseButton1Click:Connect(function() if leaderboard.Coins.Value >= price.Value then leaderboard.Coins.Value = leaderboard.Coins.Value - price.Value local item = rs:WaitForChild(item.Value) remoteevent:FireServer(item) end end) while wait() do button.Image = item.Value.." - "..price.Value end
local RS = game:GetService('ReplicatedStorage') local RemoteEvent = RS:WaitForChild('RemoteEvent') RemoteEvent.OnServerEvent:Connect(function(plr, item) item:Clone().Parent = plr.Backpack item:Clone().Parent = plr.StarterGear end)
Hope this helped!