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)
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)
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)