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

Fix this script to un-equip once activated...?

So I'm having trouble making this lucky block script un-equip once you use it. So what happens is once you use the lucky block, it deletes the current gears, gives a random, and automatically equips it. The problem is, you have to re-equip the gear, and this is a bit troublesome to the new players as they don't know what to do...

TL;DR Can someone fix it to where it will un-equip then re-equip once "activated"

local Tool = script.Parent

local gears = {173755801, 169669671, 220288991, 170897263, 98253592, 12547976, 45201977, 78730532}

function onActivated()
 -- play sound
	magicSound = Tool.Handle:FindFirstChild("MagicSound")
	if magicSound == nil then return end
	magicSound:Play()

-- GOOD RANDOMIZATION CODE!!
	numLoops = math.random(0,math.floor(1000*time()))
	numLoops = numLoops - (math.floor(numLoops / 10000)*10000)
	for i = 1,numLoops do
		whichNum = math.random(1,#gears)
	end
-- END OF GOOD RANDOMIZATION CODE!

	print("Picked: ")
	print(whichNum)
	local root = game:GetService("InsertService"):LoadAsset(gears[whichNum])

	local instances = root:GetChildren()
	if #instances == 0 then
		root:Remove()
		return
	end

	root = root:GetChildren()
	if #root == 1 then
		root = root[1]
	else return end
	print(root)
	root.Parent = Tool.Parent
	Tool:remove()
end

function onEquipped()
	magicSound = Tool.Handle:FindFirstChild("MagicSound")
	if magicSound == nil then
		magicSound = Instance.new("Sound")
		magicSound.Parent = Tool.Handle
		magicSound.Volume = 1
		magicSound.SoundId = "http://www.roblox.com/asset/?id=144007386"
		magicSound.Name = "MagicSound"
	end
end

Tool.Activated:connect(onActivated)
Tool.Equipped:connect(onEquipped)


1 answer

Since this is your code, I will not deal with it.

However, there is a Deactivated event where it is fired when you release your left mouse button. I suggest you do:

function onDeactivated()
	-- your code here
end

Tool.Deactivated:connect(onDeactivated)

If that is not what you mean, there is also the Unequippedevent, which is fired when you unequip the tool.

There's the EquipTool function which you can use, after you delete the old tool, as follows:

local myHumanoid = game.Players.LocalPlayer.CharacterAdded:wait():WaitForChild("Humanoid")

myHumanoid:EquipTool(game.Players.LocalPlayer.Backpack.Item)

1 comment

0

doesn't help :/

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