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

Card that can clone into player's backpack if unowned (Intermediate)?

Asked by 6 years ago

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?

0
Anyone help, please? Florian27 76 — 6y

2 answers

Log in to vote
0
Answered by
Jellyfosh 125
6 years ago

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)
0
The problem is that only one player can get the keycard when it's available, but the other player couldn't get the keycard even when it was available (the keycard owner reset). I tested this just recently. Florian27 76 — 6y
0
Anyone? Florian27 76 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

I found out! Thanks for helping.

Answer this question