Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make it so that only 1 player can get an item?

Asked by 2 years ago

What I am talking about and trying to figure out is, first, let's start off with some information. I am newly remaking my game into a hotel, and I am currently in the process of scripting check-in systems self-made by me. What I am also trying to figure out is how to make it so that only 1 player can get an item in their inventory and that multiple players can't have that item all at once, for example, the room keycard. The keycard is scripted to open the door and all the other stuff, but how do I make it so that only 1 player can have the keycard. I have a GUI in StarterGui for self check-in, which allows to check into listed rooms, and whenever they click this button I made called "Room 101", it gives them a handle/part inside their backpack/inventory, named "Room 101". How do I make it so that if the handle is already in somebody else's inventory, whenever anybody else clicks the "Room 101" button, it doesn't give them anything, instead pops out with a frame, that has a textlabel inside of it that says "This room is taken!"? If any other information is needed to answer this, here are the scripts I used.


REPLICATED STORAGE

Folder named "cardStorage"

cardStorage

6 tools: Room101 Room102 Room103 Room104 Room105 Room106


STARTERGUI

ScreenGUI named "sfcGui" which stands for "selfcheckinGui"


sfcGui

1 main local script: sfcHandler(IMPORTANT) 5 frames: mainFrame(Frame with button that says "Get Started") (NOT IMPORTANT) ticketFrame(List with all classes including Presidental, Suite and standard) (NOT IMPORTANT) standardFrame(List of all the rooms, buttons that say "ROOM#" on it) (NOT IMPORTANT) topBar(Title) (NOT IMPORTANT) endFrame(GUI that pops up when you check-in) (NOT IMPORTANT)

Here's the script for the sfcHandler(STILL IN PROGRESS):

local plr = game:GetService("Players").LocalPlayer local gui = script.Parent local mainFrame = gui:WaitForChild("mainFrame") local ticketFrame = gui:WaitForChild("ticketFrame") local endFrame = gui:WaitForChild("endFrame") local standardFrame = gui:WaitForChild("standardFrame") local scRE = game:GetService("ReplicatedStorage"):WaitForChild("scRE") local suiId = 1 local psId = 1

--// functions mainFrame:WaitForChild("startBtn").MouseButton1Click:connect(function() mainFrame.Visible = false ticketFrame.Visible = true end)

ticketFrame:WaitForChild("stBtn").MouseButton1Click:connect(function() ticketFrame.Visible = false standardFrame.Visible = true end)

standardFrame:WaitForChild("Room101").MouseButton1Click:connect(function() scRE:FireServer("giveRoom101", plr) standardFrame.Visible = false endFrame.Visible = true end)

standardFrame:WaitForChild("Room102").MouseButton1Click:connect(function() scRE:FireServer("giveRoom102", plr) standardFrame.Visible = false endFrame.Visible = true end)

standardFrame:WaitForChild("Room103").MouseButton1Click:connect(function() scRE:FireServer("giveRoom103", plr) standardFrame.Visible = false endFrame.Visible = true end)

standardFrame:WaitForChild("Room104").MouseButton1Click:connect(function() scRE:FireServer("giveRoom104", plr) standardFrame.Visible = false endFrame.Visible = true end)

standardFrame:WaitForChild("Room 105").MouseButton1Click:connect(function() scRE:FireServer("giveRoom105", plr) standardFrame.Visible = false endFrame.Visible = true end)

standardFrame:WaitForChild("Room106").MouseButton1Click:connect(function() scRE:FireServer("giveRoom106", plr) standardFrame.Visible = false endFrame.Visible = true end)

endFrame:WaitForChild("closeBtn").MouseButton1Click:connect(function() gui:Destroy() end)


scRE is a remote event in replicated storage by the way.

Please tell me soon how to make this work.

0
Please... Use a code block. It hurts to read these codes without the code block. NotThatFamouss 605 — 2y
0
Do you want it to be random, or a certain person? NotCasry 0 — 2y
0
random NotShloakRBLX 0 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Hey there! After a bit of experimenting, (I'm not that good with dictionaries haha had to mess around a bit) I have figured out the solution.

What you wanna do is use a ModuleScript for a dictionary to carry your Room values. This is so all scripts and localscripts and access this dictionary to check its values.

This ModuleScript has the dictionary known as "Rooms" and also has 1 key and 1 unknown key.

1 key will be if it's taken, the other is what player owns it. Now of course you can add in your own keys but I just set up these 2 important keys for ya.

Set up your module script to have a dictionary and return that dictionary. It should look something like this.

local Rooms = {

    ["Room 101"] = {
        Taken = false,
    }

}

return Rooms

After that, you want to edit your SERVER script to get the plr and Room from the Remote Event!

local Rooms = require(script.Parent.RoomsM)
local Rep = game:GetService("ReplicatedStorage")

Rep.scRE.OnServerEvent:Connect(function(plr, RoomN) -- Gets plr and the Room that the plr wants to claim!
    local Room = nil -- Room that we don't know yet

    for RoomN2, Values in pairs(Rooms) do -- This for loop will run through the dictionary to find the Room.
        if RoomN2 == RoomN then
            Room = RoomN2 -- Found the room that the player wants! Now it sets the Room variable to this string.
        end
    end

    Room = Rooms[Room] -- Uses the string variable to find the actual room inside of the dictionary.

    if Room then -- Makes sure the room isn't nil.
        if Room["Taken"] == nil or Room["Taken"] == false then -- Checks to see if the Taken key exists or is false.
            print("Not taken")
            Room["Taken"] = true -- Sets the Taken key to true
            Room["Plr"] = plr.Name -- Makes a new key that has the player's name. You can make this key nil if the plr leaves or decides to not use the room anymore.

            print(Room.Taken)
            print(Room.Plr)
        else
            print("Taken") -- You can fire the remote event again to tell the local script that it was taken BUT I would use a remote function for this instead since the local script needs to know if the room was taken.
        end
    end

end)

That's basically it, you will be ready to go soon BUT there are some major flaws in your code I noticed and also some things you should change to make it work with this code.

  1. You DON'T need to send the plr in the localscript, localscripts do that AUTOMATICALLY for the first argument!
  2. Once you have gotten rid of the plr argument, change your Room argument you're sending to be the Room name so the Server Script can find it properly in the module script. BUT this IS only so it works with how I set it up, otherwise you can change it up if you don't like how its set.

Other than you're ready to go! Have an amazing time and wish you good luck. If you need any help you can comment down on my answer. :)

PSSSSS: Use codeblocks next time, your killin' people's eyes here haha.

Ad

Answer this question