Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Sword equip in a specific area not working?

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 comment

1 answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.