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

How to only allow a player to have one of a certain item if given by use of a proximityprompt?

Hey all! I have a pretty good giver script utilizing a Proximityprompt attached to an attachment. The part is in a vending machine that dispenses soda, however, I want players to not have multiple versions of the drink in their inventory. I tried using "player.Backpack:ClearAllChildren() " before the drink is given but if the player holds the can before interacting, they will still get a second soda. The script prevents them from gathering more than two, but how do I fix this?

Soda is held in ReplicatedStorage and cloned, then placed in the players inventory if that makes any difference Thank you! Here is the current script with no changes

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local drinkTool = ReplicatedStorage.Starjuice

local drinkPart = script.Parent

drinkPart.Attachment.ProximityPrompt.Triggered:Connect(function(player) --ProximityPrompt cleared
	local drinkCopy = drinkTool:Clone() --Clones drink
	player.Backpack:ClearAllChildren() --What is supposed to delete inventory! Problem Area!
	drinkCopy.Parent = player.Backpack --Puts drink in inventory
end)

2 answers


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local drinkTool = ReplicatedStorage.Starjuice
 
local drinkPart = script.Parent
 
drinkPart.Attachment.ProximityPrompt.Triggered:Connect(function(player: Player) --ProximityPrompt cleared
    local drinkCopy = drinkTool:Clone()
    local character = player.Character --Clones drink
    local Item = player.Backpack:FindFirstChild(drinkTool.Name) 
    if not Item or character:FindFirstChild(drinkTool.Name) then
        drinkCopy.Parent = player.Backpack
    else
        return
        -- uncomment the lines below if you want to remove the old and add a new
     --   Item:Destroy()
    --  drinkCopy.Parent = player.Backpack
    end

end)

5 comments

0

lmk if that works

0

The script still functions good if I don't hold the drink, but it still allows me to get two of them if Im holding the first one that I grab. Using the "clearallchildren" command doesnt seem to work with items you are holding either, so Im unsure how to continue

0

oh ok

0

try it now i update it

0

Hey there! Sorry I didnt see the update till now, it gives syntax error for expecting to close at line 12. Messin with the code atm

Hey guys! I finally figured this out after quite a while of putting it off! Here is the full script if anyone ever needs it! Thanks you all anyhow! Hope this helps!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local drinkTool = ReplicatedStorage.Starjuice
local ToolName = "Starjuice"
local drinkCopy = drinkTool:Clone() --Clones drink
local drinkPart = script.Parent

drinkPart.Attachment.ProximityPrompt.Triggered:Connect(function(player) --ProximityPrompt cleared
	if player.Backpack:FindFirstChild(ToolName) then
		print("Player has tool in backpack!")

	else
		print("Player does not have tool in backpack!")
		drinkCopy.Parent = player.Backpack --Puts drink in inventory
	end
end)

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