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

Tool Help Please! How to disable players from picking up more than one tool?

Hello! I have a door that opens by the access of a key card. I have the card laying next to the door for a player to be able to choose their preferred room and pick up their card accordingly.

I am wondering how I can make it so that players can only pick up one card and say something like "you already own a card!" when they try to pick up another door's card. (It doesn't NEED to say anything - the function just needs to work that they can't pick up another card)

The reason I do not just put these in the starter pack is because they need to be able to pick their own door, not randomly assign it. In order for the card to work, it is specific in which door it works with. (ex. Card1 works with Door1, not Door2)

I also would like to put down 2 cards for each door as they are meant to be shared rooms. I think this should be an easy part but I wanted to specify just in case this clashes with something.

The card also needs to be regenerated upon a player leaving a server for a new player to be able to pick up, and not losing the card upon death once picked up. But they can't spawn in with the same card the next time they play the game.

So I am looking for if it is possible and what the script would be to not allow players to pick up more than one card (tool) and how to regen it after the player leaves the server?

I would appreciate your help so much. If you need any further details to understand please just ask. Thank you so much!!!

This also may help:

  1. Player selects a card. The card is moved to the player's StarterGear, which is like a per-player version of StarterPack. If that player dies they won't lose it.

  2. Make a script in every card that checks if the player clicking on it already has a card. You need to check two places - the backpack, and the character (which is where tools go when you're holding them). Deny them if it finds a card in either of those two places.

  3. Regenerating the card is the hardest part. There's an event called PlayerRemoving that tells you when a player is leaving and lets you do whatever you want before then, but if the player loses connection that isn't guaranteed to fire off properly. The best way to do it would be to keep track of the player who owns the card using another script and regenerate it if the player no longer exists.

Door's script (I am sorry I do not know how to properly paste a script) :( :

print("Advanced SpecialDoor Script loaded")



permission = { "Card1" }
Door = script.Parent.Parent.PersonDoor
DoorMesh = script.Parent.Parent.PersonDoor.Mesh
Sensor = script.Parent

function checkOkToLetIn(name)
	for i = 1,#permission do
		
		if (string.upper(name) == string.upper(permission[i])) then return true end
	end
	return false
end

function onTouched(hit)
		print("Sensor Hit")
		local human = hit.Parent:findFirstChild("Humanoid")
		if (human ~= nil ) then
			-- 
			print("Human touched door")
			
			if (checkOkToLetIn(human.Parent.Name)) then
				print("Human passed test")
				Wait(0.001)
                                script.Parent.Parent.Dot1.Transparency = 0
                                script.Parent.Parent.Beep:Play()
                                wait(0.4)
                                script.Parent.Parent.Dot2.Transparency = 0
                                script.Parent.Parent.Beep:Play()
				wait(0.4) 
                                script.Parent.Parent.Dot3.Transparency = 0
                                script.Parent.Parent.Beep:Play()
				wait(0.4) 
                                script.Parent.Parent.Open.Transparency = 0
                                script.Parent.Parent.Close.Transparency = 1
                                script.Parent.Parent.Beep2:Play()
                                wait(0.2)
                                script.Parent.Parent.OpenSound1:Play()
                                wait(0.1)
                                script.Parent.Parent.OpenSound2:Play()
                                script.Parent.Parent.HandlePart3.Transparency = 0
                                script.Parent.Parent.HandlePart4.Transparency = 0
                                script.Parent.Parent.HandlePart.Transparency = 1
                                script.Parent.Parent.HandlePart2.Transparency = 1
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.9, 0.5, 1.01)
                                Door.Transparency = 0
                                wait(0.02)
				Door.CanCollide = false
                                script.Parent.Parent.Dot1.Transparency = 1
                                script.Parent.Parent.Dot2.Transparency = 1
                                script.Parent.Parent.Dot3.Transparency = 1
				wait(3) 

                                script.Parent.Parent.Open.Transparency = 1
                                script.Parent.Parent.Close.Transparency = 0
                                script.Parent.Parent.HandlePart3.Transparency = 1
                                script.Parent.Parent.HandlePart4.Transparency = 1
                                script.Parent.Parent.HandlePart.Transparency = 0
                                script.Parent.Parent.HandlePart2.Transparency = 0
                                script.Parent.Parent.OpenSound2:Play()
                                wait(0.1)
                                script.Parent.Parent.OpenSound1:Play()
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.01, 0.01, 0.01)
				Door.CanCollide = true
				Door.Transparency = 0 
			end
		end

end

connection = Sensor.Touched:connect(onTouched) 

2 comments

0

This is possible but how are you giving the tool to the player, is this using a click detector? If you have any code pls include it.

0

Thank you for your response! It is of touch interest, but we can switch it to click if that is how it will work. Above I edited the original post to include the door script - I'm sorry I don't know how to properly post the script (I tried messing with the buttons I am new :( ). Thank you so much for your help I appreciate it so much!

1 answer

For this kind of task I would use a ModuleScript to track who has what key.

Example (Module):-

-- stores all of the tools
local toolList = {
	Card1 = [ path to this tool],
	Card2 = [ path to this tool]
}

-- a list to store who owns what keys
local keyList = {}

-- store the max keys per door
local maxKeyList = {
	Card1 = 2,
	Card2 = 2
}

-- return the functions
local f = {}

-- count the number of keys of this name stored
function f.chkKey(keyName)
	local keyCount = 0
	for plrNam, keyListName in pairs(keyList) do
		if keyName == keyListName then
			keyCount = keyCount + 1
		end
	end
	-- get the key limit and compare it to the keys in the list
	return keyCount < maxKeyList[keyName]  -- returns true of false if the count is smaller
end

-- finds if the player can get a key ie they have no key
function f.canGetKey(plrName)
	if keyList[plrName] then -- player is in the key list so they have a key
		return false
	end
	return true
end

-- clones the key into the players backpack using our stored tools in the list
function f.getKeys(plr)
	if keyList[plr.Name] then
		local tmp = toolList[keyList[plr.Name]]:Clone() -- clines the key in toolList
		tmp.Parent = plr:WaitForChild('Backpack')
	end
end

-- add a player to the tool list so they will get the tool
function f.giveKey(plr, keyName)
	keyList[plr.Name] = keyName
end

-- remove a the player from our tool list so they will not get the tool
function f.removeKey(plr)
	local curKey = keyList[plr.Name]
	keyList[plr.Name] = nil
	return curKey
end

return f

The idea of this module it to keep track of who has what key and the number of keys in use.

Example usage:-

local keyModule = require([path to key module])

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(charModel)
		keyModule.getKeys(plr) -- gives the key to the player when they spawn
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local key = keyModule.removeKey(plr) -- removes the key for the player
	
	if key then
		-- remake the key in the game
	end
end)

[some click detector].MouseClick:Connect(function(plr)
	if keyModule.canGetKey(plr) and keyModule.chkKey('Card2') then -- give the player the key if the do not own a key and the key limit has not been met
		keyModule.giveKey(plr, 'Card2')
	end
end)

All of this code is just an example to show how this can be done, this method does not use the StarterPack as all of the keys are give out in the module script.

As for your door script it would be best to rethink the script as there is a lot of duplicate code that can be simplified.

I hope this example code helps, please comment if you do not understand how / why this works but also check the wiki before hand.

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