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

[IMPORTANT] How do I make a script that makes an tool get replaced by an another tool?

Asked by 5 years ago

Hello, a beginner scripter, and I was wondering how this code would work properly, I have 2 items, item A and item B, if I go up to the item A giver, and take the item A, and then go to item B giver and take the item B, item A gets replaced with item B. (and no, I don't want any name changers or transparency changers"

This is what I have for now...

function hit(part)
    local h = game.Players.Player.Backpack["Raw Potato"]
    if h ~= false then
        end
    if h ~= true then
        h:remove()
    end
end


script.Parent.Touched:connect(hit)



^^ The one above doesn't work

local debounce = false

function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 

function onTouch(part) 

local human = part.Parent:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = getPlayer(human) 

if (player == nil) then return end 

script.Parent:clone().Parent = player.Backpack

wait(2)
debounce = false
end
end


script.Parent.Parent.Touched:connect(onTouch) 

1 answer

Log in to vote
0
Answered by 5 years ago

PLEASE REFRAIN FROM ASKING THE SAME QUESTION 3 DIFFERENT TIMES IN THE SPAN OF 45 MINUTES

I'm just going to rewrite your script and give you comments since that just seems easier. I have no clue what script.Parent is or script.Parent.parent, so this may be completely off.

Script 1

function hit(part)
    local character = part.Parent -- get player character
    if character:FindFirstChild("Humanoid") then -- gake sure is a player
        local player = game.Players:GetPlayerFromCharacter(charater) -- get player
        local item = player.Backpack:FindFirstChild("Raw Potato")  -- get item
        if item then -- does item exist
            item:Destroy() -- destroy item (use destroy, remove is deprecated)
        end
    end
end
script.Parent.Touched:Connect(hit)

Script 2

local debounce = false

function onTouch(part) 
    local character = part.parent
    local humanoid = character:FindFirstChild("Humanoid")

    if humanoid and not debounce then
        debounce = true

        local player = game.Players:GetPlayerFromCharacter(character)
            script.Parent:Clone().Parent = player.Backpack -- Add item to backpack
            wait(2)
            debounce = false
        end
    end

end
script.Parent.Parent.Touched:Connect(onTouch) 

Note - I am 99% sure this will not work how you want it initially, it is merely framework for you to build off of.

Ad

Answer this question