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

Debounce wouldn't work in script for cloning onTouch?

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
1
Answered by 5 years ago

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.

```lua 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); ```

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. rochel89 42 — 5y
0
The code worked completely fine and smooth. Thanks! rochel89 42 — 5y
Ad

Answer this question