Proximity Prompt Triggering only once and then never working again

What do you want to achieve?
I have a Part that contains a Proximity Prompt and a Server Script, When you press “E” you will get a “Soda” Tool, if you already have one, then you won’t be able to get another one… The problem is that if i reset my character and i go back to try and get another “Soda” tool, the Proximity Prompt won’t give me the tool like it did before… i checked if the tool was in my backpack, but isn’t there so dunno why i can’t get another one if it’s not in my backpack…

Sorry if i didn’t explain this correctly ;-;

What solutions have you tried so far?
I tried looking in DevForums and in Youtube, but i couldn’t find any solution to this…

Code:

local SS = game:GetService('ServerStorage').Tools
local Tool = SS.Soda
local Obj = script.Parent
local Owned = false

Obj.ProximityPrompt.Triggered:Connect(function(player)
	if player.Backpack:FindFirstChild('Soda') then
		print('You already own this item!')
	else
		local CopyTool = Tool:Clone()
		if CopyTool then
			if not Owned then
				Owned = true
				CopyTool['Soda'].BrickColor = BrickColor.random()
				CopyTool.Parent = player.Backpack
			end
		end
	end
end)

Hope that someone can be able to help me with this… Thanks in advance!

The problem might be that your ‘Owned’ variable has no way of going back to false. I’d add another event listener to listen for when a child is removed from the backpack, and check if that child is the soda, then setting Owned to false.

And how could i add that into my code? Would you help me with that if it isn’t too much asking?

Make it true under the print statement.

local SS = game:GetService('ServerStorage').Tools
local Tool = SS.Soda
local Obj = script.Parent

Obj.ProximityPrompt.Triggered:Connect(function(player)
	if player.Backpack:FindFirstChild('Soda') then
		print('You already own this item!')
	elseif not player.Backpack:FindFirstChild('Soda') then
		local CopyTool = Tool:Clone()
		if CopyTool then
			CopyTool['Soda'].BrickColor = BrickColor.random()
			CopyTool.Parent = player.Backpack
		end
	end
end)
1 Like

Something like:

player.Backpack:ChildRemoved:Connect(function(obj)
if obj.Name == "Soda" then

Owned = false

end

end)

A more optimised way of checking it would be

local SS = game:GetService('ServerStorage').Tools
local Tool = SS.Soda
local Obj = script.Parent
local Owned = false


Obj.ProximityPrompt.Triggered:Connect(function(player)
	if Owned then return end

	Owned = true
	CopyTool['Soda'].BrickColor = BrickColor.random()
	CopyTool.Parent = player.Backpack

end)

function BackpackChildAddedRemoved(Obj)
Owned = not not player:FindFirstChild("Soda")

end)

player.Backpack:ChildAdded:Connect(BackpackChildAddedRemoved)
player.Backpack:ChildRemoved:Connect(BackpackChildAddedRemoved)

Haven’t tested this out yet, but this is around how I’d do it.

1 Like

I’ll keep this in mind and see if I can make an optimized version in the future, using your example. Thanks!

This one is working like i wanted to, Thanks for helping out!

1 Like