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

Script for tool doesn't work the way I want it to, and doesn't generate errors, how to fix?

Asked by 4 years ago

So I wrote a localscript for a tool, the way that it works is that it spawns an object at a certain position, and the handle "disappears", then the spawned instance is deleted, and the handle regenerates. The instance spawns whenever I equip the tool and left-click, but it doesn't regenerate.

This is my code(it's a bit messy and long):

local tool = script.Parent         -- Variable for tool
local handle = tool:WaitForChild("Handle") -- Variable for handle

tool.Activated:Connect(function(plr)  -- If tool is activated, spawn clone of the tool, then

    local handleHasSpawned = nil  -- Handle hasn't spawned yet

    local spawnedHandle = handle:Clone()  --Spawns new copy of handle

    wait(0.5)
    spawnedHandle.Transparency = 1   -- Makes it invisible to the player
    spawnedHandle.CanCollide = nil  -- Makes it so that the player can't interact with the new clone
    spawnedHandle.Parent = workspace  -- Parents the spawned object to workspace, so it can be rendered


    handleHasSpawned = true -- Handle has been cloned

    if handleHasSpawned == true then -- If the handle was cloned then

        wait(0.5)
        handle.Transparency = 1 -- Makes tool transparent, gives illusion of tool being dropped
        spawnedHandle.Transparency = 0    -- Makes it visible to the player
        spawnedHandle.CanCollide = true   -- Makes it so that the spawned object can collide with other objects
        spawnedHandle.Anchored = true -- Anchors object
        spawnedHandle.Position = Vector3.new(-25, 9.5, -30)  -- Spawns it at location

        while spawnedHandle.Transparency == 0 do -- While the object is still visible to the player, do

            local spawnedTime = 0  -- How long the spawned object should exist

            wait(0.5)
            spawnedTime = spawnedTime + 1 -- Increases spawned time

            local yes = spawnedTime

            if yes == 3 then -- If spawned time reaches 10, then

                spawnedHandle:Destroy() -- destroys spawned object

                wait(0.1)

                handle.Transparency = 0 -- Regenerate the original tool


                break  -- stop the loop
            end
        end
    end
end)

I'm a beginner when it comes to scripting so there's probably something wrong with my code that I don't notice, and I've been trying to fix the problem for like an hour now.

Any help would be appreciated

Answer this question