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

How Do I Make This Script Only Give and Remove 1 Single Sword?

Asked by 3 years ago

I made a script that Gives a sword when you enter an area (touch the part that fills the whole area) and remove it when you arent touching anymore and it seems to be giving me another sword once I select the sword. And when I leave the area only 1 sword gets removed leaving players with another weapon in an area where there shouldnt be a weapon.

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
        game.ReplicatedStorage.Sword:clone().Parent = plr.Backpack
    end
end)

script.Parent.TouchEnded:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild('Humanoid')
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if hum and IsTouching then
        IsTouching = false
        plr.Backpack.Sword:Destroy()
    end
end)

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

That's because you are only checking for the sword in the player's backpack. When he equips the sword, it gets put inside the player's character.

0
The conditional is not reliant on the sword at all actually because all it checks for is the humanoid and the bool, however, that is also true. SteamG00B 1633 — 3y
Ad

Answer this question