So, I want to only allow the player to exchange their tool for another one if they already have it in their inventory. I made a simple script which works, but you can just make the tool appear out of no where without having the thing you need to craft it. I don't know if i'm just being stupid because it sounds so simple but im new to programming and having difficulty.
01 | script.Parent.Triggered:Connect( function (player) |
02 |
03 | local Pack = player.Backpack |
04 | local rs = game:GetService( "ReplicatedStorage" ) |
05 | local Item = rs:WaitForChild( "Copper" ) |
06 | local ItemClone = Item:Clone() |
07 | ItemClone.Parent = Pack |
08 | local RemoveItem = "Malachite Chunk" |
09 |
10 | if player.Character and player.Character:FindFirstChild(RemoveItem) and player.Character [ RemoveItem ] :IsA( "Tool" ) then |
11 | player.Character [ RemoveItem ] :Destroy() |
12 | end |
13 | end ) |
This script is inside a proximity prompt, and maybe there is a way for the proximity prompt to just not show up if they don't have it in their inventory i'm thinking?
You would check the player's backpack instead of the player's character.
1 | player.Character:FindFirstChild(RemoveItem) --instead of this |
2 |
3 | player.Backpack:FindFirstChild(RemoveItem) --you would do this |
Let me know if this helps
Since you want to disable the prompt if they don't have the item in their inventory, I made this localscript.
01 | local player = game:GetService( "Players" ).LocalPlayer |
02 | local char = player.Character |
03 | local prompt = game.Workspace.test.Attachment.ProximityPrompt --change this to your proximity prompt |
04 | while wait( 0.1 ) do |
05 | if player.Backpack:FindFirstChild( "Flash Light" ) or player.Character:FindFirstChild( "Flash Light" ) then -- checking the player's backpack incase they dont have it equipped, and the character incase they do |
06 | prompt.Enabled = true --enabling the prompt if they have the item |
07 | else |
08 | prompt.Enabled = false -- disabling if they dont |
09 | end |
10 | end |
Change the flash light to the item you want, the localscript goes in starterplayerscripts. Hope this helps!