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

How could I fix this specific part spawner code?

Asked by 7 years ago

My problem is that I can spawn fastpart 1 time but when I retry after, it doesn't work..

Heres the code :

local fastpart = game.ServerStorage.fastpart:Clone()

function onActivated()
if script.Parent.Handle.ready.Value == true then
    script.Parent.Handle.ready.Value = false

    fastpart.Parent = game.Workspace
    fastpart.Position = script.Parent.Handle.Position

    wait(10)

    fastpart:Destroy()

    script.Parent.Handle.ready.Value = true
end


end
script.Parent.Activated:connect(onActivated)

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Problem

You're only cloning your object once. You want to clone a new instance of the object every time your onActivated function is called.

Clean up

You should also use variables for anything you'll end up using more than once, or for anything that acts as a container for other objects. Here's an example:

-- Creating initial variables
-- It's also good practice to use WaitForChild to wait for something to load that you can't guarantee already exists. 

-- Game services
local serverStorage = game:GetService("ServerStorage")

-- Tool components
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local readyVal = handle:WaitForChild("ready")
local partClass = serverStorage:WaitForChild("fastpart") -- Define this since when you clone it, it's essentially making a new part instance from this blueprint.

-- Using Connect instead of connect. Lowercase event methods has been deprecated recently, in favor of ROBLOX's desierable syntax.

-- Passing an Anonymous function to Connect instead of defining one. This is mostly preference, but for your case I'd consider it good practice when necessary.
tool.Activated:Connect(function()

    -- Comparing the value to a boolean is technically unnecessary here, since Lua can interpret them as true or false by default, by just giving the boolean as the condition.
    if readyVal.Value then
        readyVal.Value = false
        local fastPart = partClass:Clone() -- Clone the part within the function
        fastPart.Parent = workspace
        -- I'd suggest using CFrame when moving parts around. If you don't know what CFrame is, feel free to ask in another question, it's a bit much to cover here.
        fastPart.CFrame = CFrame.new(handle.CFrame.p)

        wait(10)

        fastPart:Destroy()
        readyVal.Value = false
    end
end)

So there would be a slightly cleaner approach to your attempt. If you have any questions about anything mentioned in the commented text, just let me know and I'll get back to you as soon as possible.

Ad
Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago

Put line one inside your if statement, else it will only clone the part once and never again.

Answer this question