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

How to prevent players from picking up more than 1 tool?

ibb.co/Tt3V5Xq

I have a script that let's you pick a tool up from the replicated storage by holding e on a model. but you can get tons of that tool if you keep repeatedly pressing e, which can give you more than that 1 tool. i want to make it so you only can have 1 of that tool and can't get no more.

this is the script for the ProximityPrompt

local ReplicatedStorage = game:GetService ("ReplicatedStorage") local keyTool = ReplicatedStorage.Keycard

local KeyPart = script.Parent

KeyPart.ProximityPrompt.Triggered:Connect (function(player) local keyCopy = keyTool:Clone() keyCopy.Parent = player.Backpack

end)

please help!

2 answers

✓ Accepted answer

heres my take on the script. Put this script (not local script) inside the KeyPart

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KeyTool = ReplicatedStorage.Keycard

local KeyPart = script.Parent
local Prompt = KeyPart.ProximityPrompt

Prompt.Triggered:Connect(function(player)
	local bp = player.Backpack
	if bp:FindFirstChild("Keycard") or player.Character:FindFirstChild("Keycard") then -- checks if the player already has Keycard
		return -- does nothing if player already has Keycard
	else -- if player doesn't have keycard
		local KeyCopy = KeyTool:Clone()
		KeyCopy.Parent = bp
	end
end)

if you want the player to keep the keycard until they leave, do this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KeyTool = ReplicatedStorage.Keycard

local KeyPart = script.Parent
local Prompt = KeyPart.ProximityPrompt

Prompt.Triggered:Connect(function(player)
	local sg = player.StarterGear
	if sg:FindFirstChild("Keycard") then -- checks if the player's startergear contains Keycard
		return -- does nothing if player already has Keycard
	else -- if player doesn't have keycard
		local KeyCopy = KeyTool:Clone()
		KeyCopy.Parent = sg
	end
end)

5 comments

0

i feel this is a lot simpler

0

yeah thanks anyways! the other guy helped me already. but if his doesn't work ever, i'll use yours

0

You should probably use his solution, it's much more simple and can work across multiple parts in case you have multiple prompts that involve giving out your keycard.

0

on second thought, i tested your solution and it works the same, and it's a lot simpler like you said, and this guy^^^ is right. thanks!

0

np

This may require some knowledge on dictionaries, but here is a script with some explanations.

local Part = script.Parent
local ProximityPrompt = Part.ProximityPrompt

--The Tool
local Tool = Part.Tool

--Use A Dictionary To Record Whoever Already Has the Tool
local playerWhoHasTools = {
	
}

--Create Event
ProximityPrompt.Triggered:Connect(function(player)
	--Check dictionary to see if player already has tool
	if not playerWhoHasTools[player.Name] then
		local toolClone = Tool:Clone()
		toolClone.Parent = player.Backpack
		--Record player in dictionary so they don't get the tool again.
		playerWhoHasTools[player.Name] = true
	end
	
	--Create Event So When Human Dies, They Are Removed from the Dictionary
	local humanPart = player.Character:FindFirstChildOfClass("Humanoid")
	if humanPart then
		local humanEvent
		humanEvent = humanPart.Died:Connect(function()
			--Remove from Dictionary
			playerWhoHasTools[player.Name] = nil
			--Disconnect Event
			humanEvent:Disconnect()
		end)
	end
end)

4 comments

0

this is hard to understand, but I'll try. thanks for helping?

0

Where do i put this script?

0

you put the script inside your KeyPart

0

OMG BRO I LOVE YOU (no homo) THANKS SO MUCH I UNDERSTAND IT AND IT WORKSS, YOU SAVED ME SO MUCH TIME RIGHT NOW AND FOR MY FUTURE KNOWLEDGE, now i know what dictionaries are! thank you

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