Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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)
0
doesn't help :/ itadakimasu_Kurepu 29 — 5y
Ad

Answer this question