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

How do I stop cloning from repeating constantly? [clone()]

Asked by 9 years ago

I have created a sort of coin in Roblox, and it works PERFECTLY! My only problem is the regeneration. The overall function of the coin is this: You touch the part, and it adds five points onto a leaderboard. That part works perfectly. When you touch it, the part disappears, which is part of the plan to keep the player from constantly grabbing points from the coin. Then I have a regeneration script to bring the coin back after a set amount of time, currently ten seconds for testing. This works amazing, all of it. My only problem is that the part (coin) that is being cloned and pasted into the workspace is pasted EVERY ten seconds. My question is this: is there any way that I can test if the coin is in the workspace and block the pasting of it into the workspace if it already exists there?

--REGENERATION SCRIPT

while true do
    piece = game.Lighting.GivePointsBrick:clone()
    wait(10)-- How long before the the player must wait for a new coin
    piece.Parent = game.Workspace
    piece.Position = Vector3.new (0, 0, 0)
end

--LEADERBOARD

function onPlayerAdded (player)
    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local points = Instance.new ("IntValue", stats)
    points.Name = "Points"
end
game.Players.PlayerAdded:connect (onPlayerAdded)

--POINT DISTRIBUTER

part = script.Parent
enabled = false
function onTouched(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil and enabled == false then
        enabled = true
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local points = player.leaderstats.Points
        points.Value = points.Value + 5--The number of points you get from the coin
        wait (3)
        enabled = false
        end
end

script.Parent.Touched:connect (onTouched)

--REMOVE COIN

part = script.Parent
function onTouched(hit)
    part:Destroy()
end

script.Parent.Touched:connect (onTouched)

1 answer

Log in to vote
1
Answered by 9 years ago

Just use an "if" line in your script.

while true do
    if game.Workspace.GivePointsBrick == nil then
        piece = game.Lighting.GivePointsBrick:clone()
        wait(10)-- How long before the the player must wait for a new coin
        piece.Parent = game.Workspace
        piece.Position = Vector3.new (0, 0, 0)
end

That may work. Tweak the syntax if it does not

0
That was right in front of my nose. I did use an if statement, but I over complicated it and crashed studio. Thank you for pointing that out! Tremelert 0 — 9y
Ad

Answer this question