Well i want to make a napoleonic period RP but this happens when i use the tool giver morph and i don’t know how to fix it! maybe someone knows how to not let it repeat the tool?
Too many axes
Make sure when you give the tool, its not in a loop.
This is the script im using
wait(5)
function onTouch(hit)
local ptt = game.Players:playerFromCharacter(hit.Parent)
if ptt == nil then return end
local wfp = game.ServerStorage:findFirstChild("Axe"):clone()
if wfp == nil then return end
wfp.Parent = ptt.Backpack
end
script.Parent.Touched:connect(onTouch)
Add a debounce to the tool, avoiding the case of accidentally hitting the Touched event in a rapid succession by accident.
local debounce = false
local function onTouch(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local axe = game.ServerStorage:FindFirstChild("Axe")
if player and axe and not debounce then
axe:Clone().Parent = player.Backpack
debounce = true
-- optional code for reactivating
wait(5)
debounce = false
end
end
script.Parent.Touched:Connect(onTouch)
Extras:
- Do not use
playerFromCharacter, due to deprecation.GetPlayerFromCharacteris canonical. - Do not use
findFirstChild(), due to deprecation.FindFirstChild()is canonical. - Do not use
clone(), due to deprecation.Clone()is canonical. - According to this page,
Connectshould be used rather thanconnect. - Nil and false are always false in an
ifstatement.
On touch will fire every time a players part touches it. You need to only add the tool if the player doesn’t already have it.
wait(5)
function onTouch(hit)
local ptt = game.Players:PlayerFromCharacter(hit.Parent)
if ptt == nil or ppt.Backpack:FindFirstChild(“Axe”) then return end
local wfp = game.ServerStorage:FindFirstChild("Axe"):clone()
if wfp == nil then return end
wfp.Parent = ptt.Backpack
end
script.Parent.Touched:connect(onTouch)
i’m confused, clone() and Clone() seem so simillar, could you explain their differences
clone() Is deprecated, therefore it does not work correctly. Though, I think it automatically replaces it when the game is run.
Lowercase clone was deprecated in favour of PascalCase Clone for conventional reasons. The deprecated tag means that the item should not be used for newer work and that it is prone to being removed at any given time. Deprecation doesn’t implicitly equal non-working.
There’s no difference between the methods, but you should be using proper name casing for the sake of having consistent conventions in code and using supported methods.
Ah. Thank you for clearing that up for me.