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

Sword equip in a specific area not working?

Asked by 7 years ago
Edited by OldPalHappy 7 years ago

So I was given this script that supposedly allows you to equip and unequip a sword once you are in a specific area but it hasn't been working. They said to put the sword tool in the starterpack and it would work but all it did was put it in my starter pack for the whole time instead of spawning it in a specific area and then removing it once you walked out of that area.

Here is the script:

local IsTouching = false

script.Parent.Touched:connect(function(hit)
     local hum = hit.Parent:FindFirstChild('Humanoid')
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if hum and not IsTouching then
         IsTouching = true
         hit.Parent:FindFirstChild('Humanoid'):EquipTool(plr.Backpack.LinkedSword)
    end
end)

script.Parent.TouchEnded:connect(function(hit)
    local hum = hit.Parent.FindFirstChild('Humanoid')
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if hum and not IsTouching == false then
        IsTouching = false
         hit.Parent:FindFirstChild('Humanoid'):UnEquipTools()
    end
end)

Please help me. :(

1 answer

Log in to vote
0
Answered by 7 years ago

I am assuming that you want a script that will only make the sword appear in the player's inventory while in a particular area. In this case, you can modify the script like so:

local IsTouching = false
local Sword = script.LinkedSword

script.Parent.Touched:connect(function(hit)
    local hum = hit.Parent:FindFirstChild('Humanoid')
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if hum and not IsTouching then
        IsTouching = true
        local SwordClone = Sword:Clone() -- First we clone the sword.
        SwordClone.Parent = plr.Backpack -- Then place the sword in the player's backback.
        hit.Parent:FindFirstChild('Humanoid'):EquipTool(SwordClone)
    end
end)

script.Parent.TouchEnded:connect(function(hit)
    local hum = hit.Parent:FindFirstChild('Humanoid')
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if hum and not IsTouching == false then
        IsTouching = false
        hit.Parent:FindFirstChild('Humanoid'):UnequipTools()
        if plr.Backpack:FindFirstChild("LinkedSword") then
            plr.Backpack.LinkedSword:Destroy() -- Find the sword clone in the player's backpack and destroy it.
        end
    end
end)

(This script also fixes some bugs in the original that you posted so now it actually works)

Hopefully this helps.

Ad

Answer this question