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

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

Asked by 3 years ago

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

Log in to vote
1
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

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)
0
i feel this is a lot simpler NGC4637 602 — 3y
0
yeah thanks anyways! the other guy helped me already. but if his doesn't work ever, i'll use yours superbubba2020 6 — 3y
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. ArsonMcFlames 363 — 3y
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! superbubba2020 6 — 3y
0
np NGC4637 602 — 3y
Ad
Log in to vote
1
Answered by 3 years ago

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)
0
this is hard to understand, but I'll try. thanks for helping? superbubba2020 6 — 3y
0
Where do i put this script? superbubba2020 6 — 3y
0
you put the script inside your KeyPart NGC4637 602 — 3y
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 superbubba2020 6 — 3y

Answer this question