Script problem!

fro some reasons this script makes 2 or more boxes when I step on the button can someone please help me?

system = script.Parent

model = system.Box --

backup = model:Clone()

regen = system.Regen

local gamepassId = 148550940
local market = game:GetService("MarketplaceService")

script.Parent.Button.Touched:Connect(function(hit)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if plr then
		if market:UserOwnsGamePassAsync(plr.UserId, gamepassId) then
			if regen.Value == 1 then
				wait(1)
				model = backup:Clone()
				model.Parent = system
				model:MakeJoints()
			end
		else
			market:PromptGamePassPurchase(plr, gamepassId)
		end
	end
	wait(1)
end)

:Touched() is called every frame I believe when something touches the part. Implement a Debounce

print("Hello world, from client!")

system = script.Parent

model = system.Box --

backup = model:Clone()

regen = system.Regen

local gamepassId = 148550940
local market = game:GetService("MarketplaceService")
local Debouce = true
local Interval = 3
script.Parent.Button.Touched:Connect(function(hit)
    if (Debouce) then
        
        Debouce = false

        local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if plr then
            if market:UserOwnsGamePassAsync(plr.UserId, gamepassId) then
                if regen.Value == 1 then
                    wait(1)
                    model = backup:Clone()
                    model.Parent = system
                    model:MakeJoints()
                end
            else
                market:PromptGamePassPurchase(plr, gamepassId)
            end
        end

        wait(Interval)

        Debouce = true

    end
end)

but I dont know how can you show me

Script is above. Made edits to the Post.

it doesnt do anything for some reason do you knwo why

I’m not sure, but you have:

model = system.Box --

backup = model:Clone()

at the top, but inside the loop you set

model = backup:Clone()

Isn’t that using the same variable (model) and changing it back and forth? Seems like it may be part of your issue (or it’s just confusing me to read).

What your saying is it cloned once and wont do anything, which may be the issue. Ill re-write the script here in a minute

I tested it and it worked. Im not sure what is going on on your end. Try removing

model = system.Box --

backup = model:Clone()

and just create var inside the script to clone system.Box Should fix it

From my perspective, the script is fine, but the problem is that the Touched event is triggered many times and also, beforehand, you should check if the person who touched the piece is a player:

system = script.Parent
model = system.Box --
backup = model:Clone()
regen = system.Regen
local gamepassId = 148550940
local market = game:GetService("MarketplaceService")
local debounce = true --important !!!!!!!!!!!!!!!!!!!!!!!!!!
script.Parent.Button.Touched:Connect(function(hit)
if debounce  then
if hit.Parent:FindFirstChild("Humanoid") then
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if plr then
if market:UserOwnsGamePassAsync(plr.UserId, gamepassId) then
			if regen.Value == 1 then
debounce = false--IMPORTANT!!!!!!!!!!!!!!!!!!1
				wait(1)
				model = backup:Clone()
				model.Parent = system
				model:MakeJoints()
			end
		else
			market:PromptGamePassPurchase(plr, gamepassId)
		end
	end
	wait(1)
debounce = true--me viole a tu madre
end
 end
end)

Actually I have no idea. Not a pro scripter, but it seemed odd to go through all that renaming.

(:slight_smile: also just noticed that you added a debouce to the script without the ‘n’)

			if regen.Value == 1 then
                debounce = false --IMPORTANT!!!!
				wait(1)

Shouldn’t the debounce be after the wait so if a player hits it continuously it won’t refire?
Also Roblox now recommends task.wait(1) instead.

1 Like

thanks for telling me but because roblox sends to use task.wait, should I modify my scripts? or what is the difference? is that I use a lot of wait and I don’t want you to tell me to avoid errors in my scripts

The wait() function doesn’t error, but task.wait() is more effecient + wait() is going to get deprecated soon

2 Likes

ok there when it will be obsolete?

Who knows, whenever Roblox decides to deprecate wait() and spawn() for the task library

1 Like

Deprecated doesn’t mean it won’t work. They find better and more efficient ways of doing things so they use them.
Most deprecated items never get removed, otherwise older games wouldn’t keep working.

Some deprecated items like TorsoMeshes got removed entirely though. Not game breaking, but visually turned them into regular blocks in older builds that used them.

1 Like