Turning proximity trigger into toggle kills object or player

So I have scripted a simple object pickup script within a crate, using proximity trigger prompt, however I am having trouble with figuring out how to turn it into a toggle. As in reversing the actions to make it drop the crate

What is the issue? Right now it just kills the player upon pressing the trigger button or disappears the crate

this is a code sample

local ProxCar = script.Parent

ProxCar.Triggered:Connect(function(player)
	local Character = player.Character
	local UpperTorso = Character.UpperTorso
	local carry = game.ServerStorage.carry
	local crate = ProxCar.Parent
	local carried = script.Parent.Parent.isPicked
	carried = true
	crate.Parent = player.Character
	crate.CanCollide = false
	
	local MT6D = Instance.new("Motor6D", UpperTorso)
	MT6D.Part0 = UpperTorso
	MT6D.Part1 = crate
	MT6D.C0 = CFrame.new(0,-0.25,-1.75)
	
	local humanoid = player.Character:WaitForChild("Humanoid")
	local animationTrack = humanoid:LoadAnimation(carry)
	animationTrack.Looped = true
	animationTrack.Priority = Enum.AnimationPriority.Action	
	animationTrack:Play()
	
	ProxCar.Triggered:Connect(function(player)
		MT6D.Part1:Destroy()
		MT6D:Destroy()
		crate.CanCollide = true
		crate.Parent = workspace

	end)
end)
1 Like

The reason the crate disappears when released is because you’re calling MT6D.Part1:Destroy() even though a few lines earlier you set MT6D.Part1 to the crate. However, while this will prevent the crate from disappearing, instead of making a toggle you connected a entirely new .Triggered function inside of the original one. I recommend giving the crate a boolean attribute called IsCarried and and an object attribute called carrier. When the prompt is triggered, first check if IsCarried is equal to false. If it is, set IsCarried to true, set Carrier to the player who picked up the crate, and then attach the crate to the player. If IsCarried is true, first check if the player who triggered it is equal to the Carrier. If so, set IsCarried to false, Carrier to nil, and destroy the Motor6D. Remember not to destroy MT6D.Part1 this time :).

Hope I could help!

1 Like

haha thank you. i wrote to destroy part1 from frustration after trying to just destroy “mt6d” did nothing

I did what you said but i am having a hard time running the full script because of
“attempt to call missing method ‘SetAttribute’ of string”
I wrote it in 2 different ways, yet it is not changing it and running the script fully

local carrier = crate:GetAttribute("Carrier")
local isCarried = crate:GetAttribute("isCarried")
ProxCar.Triggered:Connect(function(player)
	if isCarried == false then
		isCarried = true
		carrier:SetAttribute("Carrier",player.Character.Name) 
		if ProxCar.Triggered:Connect(player.Character.Name) == carrier.Name then

here is a sample. Thank you so much inadvance. I did not think of using attributes, they are still new to me

The problem here seems to be that you’re calling carrier:SetAttribute, even though carrier is earlier defined as crate:GetAttribute(“Carrier”). I believe you want to change that to crate.

1 Like

it took a bit of tinkering but i managed to make everything work flawlessly. Thank you so much!!!
i followed your instructions but rather than using only if statement i used
proximity.triggered:connect
if
–code
else
–code
end

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