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

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

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

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

1 comment

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!

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