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

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

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

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.

1 comment

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.

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