This is my code:
--//Service local ReplicatedStorage = game:GetService('ReplicatedStorage') local Players = game:GetService('Players') --//Variables local Player = Players.LocalPlayer local Keycards = ReplicatedStorage.Keycards:GetChildren() --//MainScript script.Parent.MouseButton1Down:Connect(function() if Player.Backpack:FindFirstChild('Keycard') == nil then if Player.Character:FindFirstChild('Keycard') == nil then local GetKeycard = Keycards[math.random(1, #Keycards)] GetKeycard.Parent = Player.Backpack end end end)
The problem with this code is that another person who clicks the GUI will get the card even if it is owned. I would only like the card available to one person in the whole server, basically just one different card per person.
Any help please?
Try using a for loop to go through all players to see if they have the card. Hopefully this is what you were looking for. To better understand how these work here's the wiki article on them.
script.Parent.MouseButton1Click:connect(function() local availableCard == true for i,v in pairs(Players:GetChildren()) do --"v" would represent each player if v.BackPack:FindFirstChild('Keycard') then availableCard == false end end if availableCard == true then local GetKeycard = Keycards[math.random(1, #Keycards)] GetKeycard.Parent = Player.Backpack else print("No card for you! >:O") end end)