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

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

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
lmk if that works Stylxus 1 — 3y
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 FrigidusIncendium 24 — 3y
0
oh ok Stylxus 1 — 3y
0
try it now i update it Stylxus 1 — 3y
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 FrigidusIncendium 24 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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)

Answer this question