Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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.

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!

4 comments

0

(Also just wanted to add. When I test it out in Studio it works but not in game. And yes the Animations Script and the Purchase Script are Local)

0

maybe try refreshing the script, by setting the Disabled to true then wait() to false

0

and also why it doesn't work online is probably, because of FE, which has a bunch of remote events and stuff

0

and u cant turn it off

1 answer

✓ Accepted answer

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 script

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

##Server script in ServerScriptService

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!

1 comment

0

It works! It also helped me out a ton! Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.