Help with locker script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i made an locker, and if an player choosed their own locker combination then it will stay like that, and it opens/closes until they stop using the locker then another player can claim it

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes i tried, but nothing worked.
    i am now trying to script an locker system rn but it’s hard because i am a beginner:(

4 Likes

My quality is kinda bad but if someone can’t see the code let me know

1 Like

Why do you want a combination for every player?

Well, what you’re gonna have to do is check a player interacts with the locker, when they do, make a Text box pop up that allows you to put a code in it, then send a remoteFunction/Event.

Send the inputted combination to the server and keep a table of players combinations in a server script then compare the inputted one to the one in the table (not valueInstances cause exploiters can read those)

And only after that, actually open the locker.

well more like they can make an code combination, and etc. my point is that every time i try an method for the locker it’s either an error or the yellow one(inifte vli idk how to say it)

i can’t see anything the video quality is horrendous

i can give the code if you would like? (i been coding for at least 2-4 hours rn)

100% blunt, 100% honest (character limit lmao)

I’m gonna assume the yellow one is you trying to do :WaitForChild() with no timeout on an instance that is not guaranteed to be there

For the player’s to make a combination you need a different text box when they go to claim the locker and then you save the combination and define the player as the owner with a Object value or something like that

i suppose (the textbox) is suppose to be in startergui and i need to make an script with it? which i litterally am doing for hours(not to be mean btw)

StarterGui clones the Guis into player’s when they respawn and a Gui is set to ResetOnSpawn=false, Remember that to get the actual playerGui you need to do game.Players.LocalPlayer.PlayerGui, but yeah you’re correct on that.

You’ll need connections to the Click Detector on the lockers so that would be something like

local lockers= --Preferably a Folder containing every locker

for _,locker in lockers:GetChildren() do
 locker.ClickDetector.MouseClick:Connect()
end

Then when you show the text bow have a GuiButton to fire the event sending the combination and locker (player is send by default in remote events, no variable needed for them)

i am gonna try and update y’all

1 Like

i tried, it does let me click but the part (aka the doorpart) won’t be able to close/open whenever i click it or the gui won’t pop up?( i am currently watching youtube to see if something will help)

What did you code so far, paste the code

local Locker = script.Parent
local ClickDetector = Locker:WaitForChild(“ClickDetector”)
local CombinationPrompt = Locker:WaitForChild(“CombinationPrompt”)
local CombinationInput = CombinationPrompt:WaitForChild(“TextBox”)
local Opened = Locker:WaitForChild(“Opened”)
local PlayerClaimed = nil

local function onLockerClicked(player)
if PlayerClaimed == nil or PlayerClaimed == player then
– Player hasn’t claimed the locker or is the current owner
if Opened.Value then
– Locker is open, so close it
Opened.Value = false
else
– Locker is closed, so open it
Opened.Value = true
end
else
– Another player is currently using the locker
print(“This locker is currently in use by another player.”)
end
end

local function onCombinationChanged(player)
if PlayerClaimed == nil then
– Player hasn’t claimed the locker yet, so claim it
PlayerClaimed = player
print(player.Name … " has claimed the locker.")
else
– Player has already claimed the locker, so notify
print(“You’ve already claimed this locker.”)
end
end

ClickDetector.MouseClick:Connect(function(player)
if CombinationInput.Text ~= “” then
onCombinationChanged(player)
CombinationInput.Text = “” – Clear the input after setting the combination
else
onLockerClicked(player)
end
end)

Locker.ChildAdded:Connect(function(child)
if child.Name == “CombinationPrompt” then
child.ChildAdded:Connect(function(innerChild)
if innerChild.Name == “TextBox” then
innerChild.FocusLost:Connect(function(enterPressed)
if enterPressed then
onCombinationChanged(game.Players:GetPlayerFromCharacter(Locker))
end
end)
end
end)
end
end)

1 Like

also i did put the locker in an folder-

1 Like

You never added the part where the UI pops up, or when the door opens/closes. There’s also loads of weird characters, which you can’t use when coding. For example “” and –. Instead of using these characters, use ‘’ and – otherwise your script won’t work.

okay- i am gonna correct the code :sob: kinda annoyed at myself

1 Like

Wrap your code with " ``` " at the start and end to make it more readable, you don’t want a script for every locker you have, that will just be horrible performance wise, impossible to edit all of them at once and frustrating, you can put a script on ServerScriptService and get every locker from there

local lockers --A folder containing every locker for organization and simplicity sake
local lockerEvent--A RemoteEvent/UnreliableRemoteEvent

local function lockerClicked(player,locker)
	if not locker.Owner.Value then --Make a object/string value to store the Owner, this means the value is nil
		return --and we return, aka finish the function, player will be prompted to get ownership clientside
	end
	if locker.Owner.Value==player then--or Player.Name if you use a string value
		if locker:GetAttribute("Open")==true then
			--Logic to close it
			locker:SetAttributte("Open",false)
		else --Locker is closed
			--Logic to open it
			locker:SetAtributte("Open",true)
		end
	else
		--Player is not the owner
	end
end

for _,locker in pairs(lockers:GetChildren()) do
	locker.ClickDetector.MouseClick:Connect(function(player)--We need an anonymus function because we have to pass the locker value
		lockerClicked(player,locker)
	end)
end

lockerEvent.OnServerEvent:Connect(function(player,locker)
	if locker.Owner.Value then return end --Locker is already claimed, just a sanity check
	locker.Owner.Value=player --or Player.Name if string value
end)

Now on a local script (for this example i do like it’s under the Gui):

local lockerEvent --Again get you event here
local lockers --Locker folder again

local Gui=script.Parent:WaitForChild("CombinationFrame") --Asumming this is the ScreenGui, and getting the container of the Gui elements , name is whatever you name it

for _,locker in pairs(lockers:GetChildren()) do
	locker.ClickDetector.MouseClick:Connect(function()
		if not locker.Owner.Value then
			Gui.TextBox.Visible=true
			
			Gui.TextButton.Activated:Connect(function()
				lockerEvent:FireServer(locker)
				Gui.TextBox.Visible=false
			end)
		end
	end)
end

This is not all you need but this should let you claim lockers

does the gui(the screengui) needs to set invisible or not? if not, then i suppose i can do this and see if it works

Set to invisible by default yes, but not necesarilly the screen Gui, you can also set the container frame to be not visible