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

Debounce wouldn't work in script for cloning onTouch?

I've made a brick that when being touched, clones the player's character on command.

The cloning part worked successfully, but it won't stop cloning once it's first clone. I plan to only made one clone.

I used a normal script parented by the part that does the function.

script.Parent.Touched:Connect(function(hit)

local db = false

local plr = hit.Parent

local hum = plr:FindFirstChildWhichIsA('Humanoid')

plr.Archivable = true

if ( hum ) and not db then

db = true

print("hi")

local clone = plr:Clone()

clone.Parent = workspace

wait(7)

db = false

clone:Destroy()

end

end)

I have used some guidance from the wiki.

1 answer

✓ Accepted answer

db is declared in the function.

That means it will always start off as false each time the part is touched. You meant to declare it outside the function.


plr is a misleading identifier, hit.Parent will never be a player, the most it will be is a player's character.

local Players = game:GetService("Players");
local Workspace = game:GetService("Workspace");
local debounce = false;

script.Parent.Touched:Connect(function(part)
	if (debounce) then
		--// return out the function if cant touch
		return;
	end
	
	local client = Players:GetPlayerFromCharacter(part.Parent);
	--// using the humanoid to check if player is unreliable, anything can have a humanoid
	
	if (client) then
		debounce = true;
		local character = client.Character;
		character.Archivable = true;
		character:Clone().Parent = Workspace;
		wait(7);
		debounce = false;
	end
end);

2 comments

0

So instead of checking the term 'Humanoid' is present. The client variable finds for the player touching the brick instead of other object, I didn't really consider that.

0

The code worked completely fine and smooth. Thanks!

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