The title is pretty straight forward. How can I turn this simple script I had working as OnTouch into fully working as a ProximityPrompt.Triggered? There’s a sandwich you’ll have to hand to an NPC in my game and rather than touching the cage with the tool in your hand to feed them, I’d like the player to hold the ProximityPrompt instead while having the tool in your hand still. Here’s my script:
local Cage = script.Parent
local SandwichProp = game.Workspace.Givers.Sandwich
local Dialogue2Sound = Cage.Dialogue2
local HandSandwichProximity = Cage.HandSandwichProximity
HandSandwichProximity.Triggered:Connect(function(Player)
local sandwichkey = hit:findFirstChild("sandwichkey")
if sandwichkey then hit.Parent:Destroy()
SandwichProp:Destroy()
Dialogue2Sound:Play()
Dialogue2Sound.Ended:Wait()
end
end
I know that it’s not set up right but I left it like this right now to hopefully get some help with making these wanted changes. I tried looking up some possible solutions but since I’m still decent at scripting I struggle a bit haha. Once again though, I’d appreciate your help!!
local Cage = script.Parent
local SandwichProp = game.Workspace.Givers.Sandwich
local Dialogue2Sound = Cage.Dialogue2
local HandSandwichProximity = Cage.HandSandwichProximity
HandSandwichProximity.Triggered:Connect(function(Player)
local sandwichkey = hit:findFirstChild("sandwichkey")
if sandwichkey then hit.Parent:Destroy()
SandwichProp:Destroy()
Dialogue2Sound:Play()
Dialogue2Sound.Ended:Wait()
end
end
I tried turning it into a ProximityPrompt.Triggered as you see. I still want it to work with the same concept, if you’re equipping the item while triggering, it gets destroyed it from your inventory and the rest of the script remains as is.
Use the player variable that it gives you to get their character. Then, look for the sandwich in this character (assuming you’re using tools, the tool goes inside the character when equipped), and then look for the key in the sandwich.
Yeah I read something similar to this when I looked up how I can go about doing this but I couldn’t fully understand how the code would be adjusted for it.
You can keep the onTouch event. But also keep the proximityprompt function. In the proximity Prompt function you can call the function onTouch() For example:
function onTouch()
local sandwichkey = hit:findFirstChild("sandwichkey")
if sandwichkey then hit.Parent:Destroy()
SandwichProp:Destroy()
Dialogue2Sound:Play()
Dialogue2Sound.Ended:Wait()
end
local Cage = script.Parent
local SandwichProp = game.Workspace.Givers.Sandwich
local Dialogue2Sound = Cage.Dialogue2
local HandSandwichProximity = Cage.HandSandwichProximity
HandSandwichProximity.Triggered:Connect(function()
onTouch()
end)
Use :FindFirstChild on the character and pass into it the sandwich’s name as it is in workspace or wherever it is. And store that in a sandwich variable.
local sandwich = Player.Character:FindFirstChild("namegoeshere")
Then instead of using hit when looking for the sandwich key, use sandwich:FindFirstChild("sandwichkey"). Also replace hit.Parent with sandwich.
local Cage = script.Parent
local SandwichProp = game.Workspace.Givers.Sandwich
local Dialogue2Sound = Cage.Dialogue2
local HandSandwichProximity = Cage.HandSandwichProximity
HandSandwichProximity.Triggered:Connect(function(Player)
local sandwichkey = Player.Character:FindFirstChild("sandwichkey")
if sandwichkey then
Player.Character:Destroy()
SandwichProp:Destroy()
Dialogue2Sound:Play()
Dialogue2Sound.Ended:Wait()
end
end
That sandwich variable needs to go inside the Triggered event. Also, it needs to be Player.LocalPlayer, since Player in your script is the Players service .
Did these changes now, doesn’t seem to make the script work yet.
Yeah just did so, the script still isn’t working as wanted and it isn’t showing any errors now in the output at the same time? What could it be?
This is how the script is set up now:
local Cage = script.Parent
local SandwichProp = game.Workspace.Givers.Sandwich
local Dialogue2Sound = Cage.Dialogue2
local HandSandwichProximity = Cage.HandSandwichProximity
local Player = game.Players
HandSandwichProximity.Triggered:Connect(function(Player)
local sandwich = Player.Character:FindFirstChild("sandwich")
local sandwichkey = sandwich:FindFirstChild("sandwichkey")
if sandwichkey then sandwich:Destroy()
SandwichProp:Destroy()
Dialogue2Sound:Play()
Dialogue2Sound.Ended:Wait()
end
end)
Well, I suggest changing local Player = game.Players to local Players = game.Players, so that we avoid having two variables named the same name. Are you sure that the name you’re passing into the FindFirstChild method in local sandwich = Player.Character:FindFirstChild("sandwich") is the exact name of the sandwich? That method is case sensitive. That means if your sandwich is named “Sandwich” and you pass in “sandwich”, it will not find the sandwich. It needs to be the exact same as the actual object’s name.
Yes it’s the exact same name. I’m still confused on why this is happening though what I’m going for is simple. You’re holding said tool while triggering the ProximityPrompt? Destroy it from your inventory and the rest of the script stays as is. Here’s a further look into the setup with the tool and the scripts and stuff so you can get a much better understanding of what’s going on:
The sandwich prop that gives you the same sandwich but as a tool when holding its seperate ProximityPrompt
The Folder it’s inside of in Workspace
The code inside the “Script” script:
local itemname = "sandwich"
local item = game.ServerStorage.Items[itemname]
local trigger = script.Parent
local enabled = true
local function onClick(plyr)
if plyr.Backpack:FindFirstChild(itemname) == nil and plyr.Character:FindFirstChild(itemname) == nil and enabled == true then
enabled = false
local itemclone = item:Clone()
itemclone.Parent = plyr.Backpack
wait(2)
enabled = true
end
end
script.Parent.ProximityPrompt.Triggered:Connect(onClick)
The sandwich tool inside of the folder named “Items” in ServerStorage
The script I shown at first trying to change it from OnTouch to ProximityPrompt.Triggered so the tool can disappear from your inventory still with the same concept except a Proximity is how you’re triggering the script:
local Cage = script.Parent
local SandwichProp = game.Workspace.Givers.Sandwich
local Dialogue2Sound = Cage.Dialogue2
local HandSandwichProximity = Cage.HandSandwichProximity
local Player = game.Players
local sandwich = Player.Character:FindFirstChild("sandwich")
HandSandwichProximity.Triggered:Connect(function(Player)
local sandwichkey = sandwich:FindFirstChild("sandwichkey")
if sandwichkey then sandwich:Destroy()
SandwichProp:Destroy()
Dialogue2Sound:Play()
Dialogue2Sound.Ended:Wait()
end
end)
Nothing too crazy, hopefully this shows more of how I have it set up currently!
Try this. Not sure if it 100% works or not because I didn’t do it in studio.
local Cage = script.Parent
local SandwichProp = game.Workspace.Givers.Sandwich
local Dialogue2Sound = Cage.Dialogue2
local HandSandwichProximity = Cage.HandSandwichProximity
HandSandwichProximity.Triggered:Connect(function(Player)
local char = Player.Character or Player.CharacterAdded:Wait()
local sandwichkey
for i, v in pairs(Player.Backpack:GetChildren()) do
if v then
if v:IsA("Tool") then
if v.Name == "sandwich" then
sandwichkey = v
end
end
end
end
for i, v in pairs(Player.Character:GetChildren()) do
if v then
if v:IsA("Tool") then
if v.Name == "sandwich" then
sandwichkey = v
end
end
end
end
if sandwichkey then
SandwichProp:Destroy()
Dialogue2Sound:Play()
Dialogue2Sound.Ended:Wait()
else
print("No key?")
end
end)