How to make a proximity prompt that checks int value on activation (Key Door System)

Hi there. In my game I have a locked door system that checks how many keys you have, and if the key’s int value is greater than or equal 1, the door will open and subtract 1 from the key’s int. For some reason the script detects that the value is 0, even though I have collected a key in game adding 1 to the value. Strangely enough if I manually set the int value to 1 before running the game the code works. If someone could help solve this issue that would be awesome!

local keys = game.ServerStorage.Keys.Value

script.Parent.Triggered:Connect(function()
	if keys >= 1 then
		script.Parent.Parent["Lock Unlock Door 4 (SFX)"]:Play()
		script.Parent.Enabled = false
		script.Parent.Parent.Anchored = false
		script.Parent.Parent.Parent.Part.Anchored = false
		script.Parent.Parent.Parent.Parent.Door.ProximityPrompt.Enabled = true
		keys = keys - 1
		
	elseif keys == 0 then
		script.Parent.Parent.Parent.Parent.Door["Door Locked Sound"]:Play()
	end

end)

You should not get the value of the IntValue in the variable.
The variable would only contain the value of the IntValue when the script loads, and would not update.
Try this:

local keys = game.ServerStorage.Keys

script.Parent.Triggered:Connect(function()
	if keys.Value >= 1 then
		script.Parent.Parent["Lock Unlock Door 4 (SFX)"]:Play()
		script.Parent.Enabled = false
		script.Parent.Parent.Anchored = false
		script.Parent.Parent.Parent.Part.Anchored = false
		script.Parent.Parent.Parent.Parent.Door.ProximityPrompt.Enabled = true
		keys.Value -= 1
		
	elseif keys.Value == 0 then
		script.Parent.Parent.Parent.Parent.Door["Door Locked Sound"]:Play()
	end

end)

Ok! I’ll give it a try. I thought that might have been the issue

It works! Thanks for the help!

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