#cloning from replicated storage .touch issue

1 messages · Page 1 of 1 (latest)

cunning ingot
#

intention: every time you click and you are in contact with a part called "CoalOre" it clones the tool: "Coal" from replicated storage once and gives the 1 coal to you by parenting it to your backpack
bug: idk how to explain. first click = gives you one coal, second gives two, third gives 3 etc etc so after 3 clicks you have 6 coal..

local handle = tool.Handle
local hitbox = tool.Hitbox
local repstorage = game:GetService("ReplicatedStorage")
local coal = repstorage.Coal
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local debounce = false

tool.Activated:Connect(function(on)
    if not debounce then
        debounce = true
        if debounce == true then
            hitbox.CanTouch = true
            hitbox.Touched:Connect(function(hit)
                if hit.Name == "CoalOre" then
                    print("MINE")
                    local clone = coal:Clone(1)
                    clone.Parent = player:FindFirstChild("Backpack")
                    hitbox.CanTouch = false
                    task.wait(1)
                    debounce = false
                else
                    return false;
                end
            end)
        end
    end
end)```
#

will send clip in a second

spark yacht
#

you never disconnect the touched connection, so the first time it will run once, next two, next three...

cunning ingot
#

i think i fixed it wai

upbeat etherBOT
#

studio** You are now Level 8! **studio

cunning ingot
#

nvm

#

still dont know 😭

#

gng im cooked

#

help 🙏

cunning ingot
#

@spark yacht

cunning ingot
#

ts pmo deepsigh

dusk sapphire
#

do you still need help

cunning ingot
#

😭

dusk sapphire
cunning ingot
dusk sapphire
#

oh yeah

cunning ingot
#

so when activated the touch will give me 1 coal first time, 2 coal second, 3 the 3rd and so on and so forth

dusk sapphire
#

ok

#

send me the code

cunning ingot
#

i basically want it to just give me 1 every time

#
local tool = script.Parent
local handle = tool.Handle
local hitbox = tool.Hitbox
local repstorage = game:GetService("ReplicatedStorage")
local coal = repstorage.Coal
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local debounce = false

tool.Activated:Connect(function(on)
    if not debounce then
        debounce = true
        if debounce == true then
            hitbox.CanTouch = true
            hitbox.Touched:Connect(function(hit)
                if hit.Name == "CoalOre" then
                    print("MINE")
                    local clone = coal:Clone(1)
                    clone.Parent = player:FindFirstChild("Backpack")
                    hitbox.CanTouch = false
                    task.wait(1)
                    debounce = false
                else
                    return false;
                end
            end)
        end
    end
end)```
#

im not good with debounce if that might be what it is

#

cause im tryna learn how to use it and stuff

dusk sapphire
#

ok let me clean this up first

#
local tool = script.Parent
local handle = tool.Handle
local hitbox = tool.Hitbox
local repstorage = game:GetService("ReplicatedStorage")
local coal = repstorage.Coal
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local debounce = false

local hitboxTouchedConnection = nil

tool.Activated:Connect(function(on)
    if debounce then return end
    debounce = true
    hitbox.CanTouch = true

    if (hitboxTouchedConnection~=nil) then
      hitboxTouchedConnection:Disconnect()
    end
    
    hitboxTouchedConnection = hitbox.Touched:Connect(function(hit)
        if hit.Name == "CoalOre" then
            print("MINE")
            local clone = coal:Clone(1)
            clone.Parent = player:FindFirstChild("Backpack")
            hitbox.CanTouch = false
            task.wait(1)
            debounce = false
        else
            return false;
        end
    end)

end)
#

ok

#

i think this is good enough @cunning ingot

#

im really just guessing the issue

#

but im like 78 percent sure its the connection being made multiple times

cunning ingot
#

ill test it in a sec

cunning ingot
dusk sapphire
# cunning ingot btw what did you change / howd u change it?

so basically, everytime you activated your tool, it does the hitbox.Touched. roblox doesnt really mention this alot to the new developers but when you listen to an event, it returns a connection. connections can be disconnected for memory and also to just disconnect it

upbeat etherBOT
#

studio** You are now Level 5! **studio

dusk sapphire
#

so basically

#

everytime you activated the tool

#

it made a new connection

#

and the old ones never deleted

#

so basically one tool had like 8 listeners to when it gets touched

cunning ingot
#

ooohh

cunning ingot
#

so that it was just 1

dusk sapphire
#

if the connection exissts

#
:Disconnect()
#

you can cache any event your listneing to

#

returns a RBXCONNECTION

#
local plradded = game.Players.PlayerAdded:Connect(function(plr)
  print('a player named {plr.Name} joined you') --idk if this is how you concatonate with curly 
end)

-- wiaitng for people that joined in first 10 seconds
task.wait(10)

-- ok no more people
plradded:Disconnect()

cunning ingot
#

oooh

#

wait

#

howd u make it so urs has color

#

also tysm that makes sense

cunning ingot
#
print("Thank you")
#

bru

#

the coal i picked up is the same thing as the one from repstorage and yet

#

only the one i picked up works

#

its the same exact thing

#
local part = script.Parent
local folder = game.Workspace:FindFirstChild("Generator")
local signal1 = folder.Powersignal1
local signal2 = folder.Powersignal2
local signal3 = folder.Powersignal3
local signal4 = folder.Powersignal4
local fire = part.Parent.Flame
local x = 0
local debounce = false

part.Touched:Connect(function(hit)
    print(hit)
    if hit.Parent.Name == "Coal" then
        hit.Parent:Destroy()
        part.Flame:Play()
        fire.FireParticles.Enabled = true
        fire.Transparency = 0.7
        x = x+1
        print(x)
        task.wait(2)
        part["Flames Loop"]:Play()
        fire.FireParticles.Rate = 15
        fire.SpotLight1.Enabled = true
        fire.SpotLight2.Enabled = true
        fire.embers.Enabled = true
        fire.Transparency = 0.3
        if x>=5 then
            print("x >= 5")
            signal1.Material = "Neon"
            signal2.Material = "Neon"
            signal3.Material = "Neon"
            signal4.Material = "Neon"
        elseif x>=4 then
            print("x >= 4")
            signal4.BrickColor = BrickColor.new("Lime green")
        elseif x>=3 then
            print("x >= 3")
            signal3.BrickColor = BrickColor.new("Lime green")
        elseif x>=2 then
            print("x >= 2")
            signal2.BrickColor = BrickColor.new("Lime green")
        elseif x>=1 then
            print("x >= 1")
            signal1.BrickColor = BrickColor.new("Lime green")
        end
    end
end)

ts pmo deepsigh

#

ill fix that another day tho icl

#

tysm slimey for the help, lowkey had no idea what to do