Proximity Prompt to Equip/Unequip Outfit

As the title says, I’m trying to make a script that will equip a certain set of clothes and return the player’s normal clothes when triggered again. It does work but only halfway; the player is given the clothes but can’t unequip them once on. I’m not receiving any errors in the output, so I’m not sure if something is going wrong or if I messed something up in the script somewhere. Any n all help is appreciated.

Here’s a video of the script in action.

And here’s the script, it’s simply put on a part with a proximity prompt.

local part = script.Parent
local ProximityPrompt = part.ProximityPrompt 


local Pants = part.Pants
local Shirt = part.Shirt

EquipPants = ("http://www.roblox.com/asset/?id=8680362033")
EquipShirt = ("rbxassetid://10640992390")

debounce = true

ProximityPrompt.Triggered:Connect(function(player)
	local Character = player.Character or workspace:FindFirstChild(player.name)
	DefaultPants = Character.Pants.PantsTemplate
	DefaultShirt = Character.Shirt.ShirtTemplate
	
	if Character then

		if debounce then debounce = false
			if Character.Pants.PantsTemplate == DefaultPants then
				Character.Pants.PantsTemplate = EquipPants
			else Character.Pants.PantsTemplate = DefaultPants
			end
			if Character.Shirt.ShirtTemplate == DefaultShirt then
				Character.Shirt.ShirtTemplate = EquipShirt
			else Character.Shirt.ShirtTemplate = DefaultShirt
			end	task.wait(0.33)
			debounce = true
		end
	end
end)

if debounce then debounce = false

The reason you can’t use the prompt again is because you nested the rest of the code to switch clothing inside of that if statement. Basically saying, only select the clothing if debounce is true.

You could either end the if statement in the same line like so:

if debounce then debounce = false end

Or just do this; it’s better and doesn’t require an if statement.

debounce = not debounce

^ this will (obviously) just invert the debounce variable ^

2 Likes

It seems to still not be working the way I want it to, so there’s probably something else going on within the script that’s breaking it.

Change that first

debounce = true

to

local debounce = true

Without the “local”, debounce is a static, not a variable, and will never change.

Also do what the above commenter said and swap for debounce = not debounce inside the function

1 Like

It doesn’t become static, it just changes from a local variable to a global one

2 Likes

I’m not sure if you have to put these in () so it’d be better if you removed them
And also prompt.Triggered only works in server scripts

1 Like

Preformatted text

The issue is that the DefaultPants and the DefaultShirt is called when its triggered. Which means every time its triggered, will replace the default value.
At the first trigger won’t be an issue however in the second will replace the default and so in the third time triggering will will replace the EquipedPants with the default one - the same.

local part = script.Parent
local ProximityPrompt = part.ProximityPrompt

local Pants = part.Pants
local Shirt = part.Shirt

EquipPants = (“http://www.roblox.com/asset/?id=8680362033”)
EquipShirt = (“rbxassetid://10640992390”)

debounce = true

– A table to store the default clothes for each player
local DefaultPants = {}
local DefaultShirt = {}

ProximityPrompt.Triggered:Connect(function(player)
local Character = player.Character or workspace:FindFirstChild(player.name)

– Checking if the default pants and shirt is already initialized
– If not yet doing it
if(DefaultPants[player.Name] == nil) then
DefaultPants[player.Name] = Character.Pants.PantsTemplate
end

if(DefaultShirt[player.Name] == nil) then
DefaultShirt[player.Name] = Character.Shirt.ShirtTemplate
end

if Character then

  if debounce then debounce = false
  	if Character.Pants.PantsTemplate == DefaultPants[player.Name] then
  		Character.Pants.PantsTemplate = EquipPants
  	else Character.Pants.PantsTemplate = DefaultPants[player.Name]
  	end
  	if Character.Shirt.ShirtTemplate == DefaultShirt[player.Name] then
  		Character.Shirt.ShirtTemplate = EquipShirt
  	else Character.Shirt.ShirtTemplate = DefaultShirt[player.Name]
  	end	task.wait(0.33)
  	debounce = true
  end

end
end)

In this example the DefaultPants and the DefaultShirt is stored in a table and at the first time triggering it, the default will be replaced and the player’s clothing to be changed. In the second trigger, the default won’t be replaced since its not nil anymore.

I hope this helps. If you have questions please reply.

1 Like

Thank y’all for the help! It works exactly as intended now and I learned a bit more about scripting.

1 Like

And here’s a collection version of the script in case anyone will find it useful. It has the added effect of playing a small equip animation as well as a sound. It’ll get the Shirt/Pants ID from the part itself, as well as the action/object text for the added proximity prompt. Let me know if there’s any issues with it!

local CollectionService = game:GetService("CollectionService")

local DefaultPants = {}
local DefaultShirt = {}
local Connection = {}
local Debounce = {}

function applyCode(Inst)
	if not Inst:IsA("BasePart") then return end
	Connection[Inst] = {}
	local EquipPants = Inst:GetAttribute("PantsID")
	local EquipShirt = Inst:GetAttribute("ShirtID")
	local ProximityPrompt = Instance.new("ProximityPrompt")
	ProximityPrompt.ActionText = Inst:GetAttribute("ActionText")
	ProximityPrompt.ObjectText = Inst:GetAttribute("ObjectText")
	ProximityPrompt.Parent = Inst
	
	table.insert(Connection[Inst], ProximityPrompt.Triggered:Connect(function(Player)
		Debounce[Player] = true
		local Sound = Instance.new("Sound")
		Sound.SoundId = "rbxassetid://5989298194"
		Sound.Parent = Inst
		Sound:Play()
		Sound.Ended:Wait()
		Sound:Destroy()
		local Animation = Instance.new("Animation")
		Animation.AnimationId = "rbxassetid://16820156457"
		Player.Character.Humanoid:LoadAnimation(Animation):Play()
		
		local Character = Player.Character or workspace:FindFirstChild(Player.name)


		if(DefaultPants[Player.Name] == nil) then
			DefaultPants[Player.Name] = Character.Pants.PantsTemplate
		end

		if(DefaultShirt[Player.Name] == nil) then
			DefaultShirt[Player.Name] = Character.Shirt.ShirtTemplate
		end

		if Debounce[Player] then Debounce[Player] = false
				if Character.Pants.PantsTemplate == DefaultPants[Player.Name] then
					Character.Pants.PantsTemplate = EquipPants
				else Character.Pants.PantsTemplate = DefaultPants[Player.Name]
				end
				if Character.Shirt.ShirtTemplate == DefaultShirt[Player.Name] then
					Character.Shirt.ShirtTemplate = EquipShirt
			else Character.Shirt.ShirtTemplate = DefaultShirt[Player.Name]
				end
			end
					task.wait(.33)
				Debounce[Player] = true
			end))
	end
task.spawn(function()
	local Tagged = CollectionService:GetTagged("ClothesEquip")
	for count=1, #Tagged do
		local Inst = Tagged[count]
		task.spawn(function()
			applyCode(Inst)
		end)
	end
end)

CollectionService:GetInstanceAddedSignal("ClothesEquip"):Connect(function(Inst)
	task.spawn(function()
		applyCode(Inst)
	end)
end)

CollectionService:GetInstanceRemovedSignal("ClothesEquip"):Connect(function(Inst)
	if not Connection[Inst] then return end
	for count=1, #Connection[Inst] do
		local con = Connection[count]
		con:Disconnect()
	end
	Connection[Inst] = nil
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.