Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

1 comment

0

Anyone help, please?

2 answers

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)

2 comments

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.

0

Anyone?

This discussion is preserved as it was. New answers, comments, and votes are closed.