I'm making a sword fighting game, and there's a part which is a platform for the sword fighting area. When you touch that part, you get a sword. I tried to use the TouchEnded function to delete your sword when you leave. It gives you the sword, but when you try to equip it you instantly unequip but its still in your hotbar (i think it just deletes it). How can I make it only delete when you step out of the sword fighting area?

Code:

local hasSword = false
script.Parent.Touched:Connect(function(hit)
 	-- Detects if the part that 'hit' the brick contains a Humanoid
    if hasSword == false then
		if hit.Parent.Humanoid then
        -- Function GetPlayerFromCharacter returns the Character associated with the player (hit.Parent)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        -- Clones tool into player's backpack
    sword = game.ReplicatedStorage.ClassicSword:Clone()
    sword.Parent = plr.Backpack
	hasSword = true
    end
	end
end)
script.Parent.TouchEnded:Connect(function()
	hasSword = false
	sword:Destroy()
	wait(5)
end)

Thank you.

3 comments

0

Don't use TouchEnded, use 2 parts, one for enter and one for exit. Or, use region3

0

Just disable the sword my man

0

Are you also getting errors that the humanoid is not a valid member?