How can i detect if a player is holding an item when they trigger a prox prompt

so im making a gasa4 like game and i want one of the endings is to give an item to an npc

so i have a talk promt that makes a dialogue ui apear how can i make it so when the player talks to the npc that if there holding a certain item lets say cheese then the npc will have different dialogue and then gives you a badge
it would also have to make a ui apear but this doesnt need to be in 1 script it can do each thing like award badge change dialogue in multiple scripts

heres the interaction script for when you talk to the npc if you dont have cheese

local RP = game:GetService("ReplicatedStorage")
local Events = RP.Events

local text = "Gimme cheese"
local triggered = false

game.Players.PlayerAdded:Connect(function(player)
	script.Parent.TalkPrompt.Triggered:Connect(function()
		if triggered == false then
			Events.Talking:FireClient(player,"Rat",text,"1, 1, 1")
			triggered = true
		else
			wait(5)
			triggered = false
		end
	end)
end)


heres the hanlder script for all the dialogue thats inside a frame in startergui

local RP = game:GetService("ReplicatedStorage")
local Events = RP.Events

local function soundPlay()
	local sound = script.Sound:Clone()
	sound.Parent = script
	sound.Name = "ClonedSound"
	
	sound:Play()
	
	wait(1)
	
	sound:Remove()
end

Events.Talking.OnClientEvent:Connect(function(name,text)
	script.Parent.Visible = true
	script.Parent.npcName.Text = name
	
	for i=1, #text do
		script.Parent.npcText.Text = string.sub(text,1,i)
		coroutine.wrap(soundPlay)()
		
		wait(0.04)
	end
	
	wait(1)
	script.Parent.Visible = false
end)

so i just want to detect if the player is holding an item when they talk to the npc

2 Likes

You can check if there is a child in the player character of class BackpackItem using the character’s FindFirstChildWhichIsA method and then do whatever is required…

Here is an example:

local Character = Player.Character

if Character:FindFirstChildWhichIsA("BackpackItem") then
    -- a tool is being held
else
    -- no tool..
end

1 Like

so this detects if there is an item in the players backpack but how can I detect if its a certain tool?

also how can I change the dialogue I would need a spot to put the code so I could also make it award a badge

Do you have a list of the “certain” tools’ names or something unique between each other?

I don’t know the structure of your code, but you can make two different functions, one for someone who is not equipping a tool and another otherwise.

hmm ok I put the dialogue script interaction and handler script on the post
also the tools have different names
I only have 1 tool so far its just cheese so if you can detect if the tool name is cheese then that would work

You can add a second criteria for the tool name to the if statement.

local Character = Player.Character
local ToolFound = Character:FindFirstChildWhichIsA("BackpackItem")

if ToolFound and ToolFound.Name == "cheese"  then
    ...
else
    ...
end

If you are going to add more tools and dialogs based on the equipped one (in the future), I’d prefer to use a mapping table in this case like the following:

local Character = Player.Character

local ToolDialog_Mapping = {
	["Cheese"] = function()
		...
	end;
}

local Tool = Character:FindFirstChildWhichIsA("BackpackItem")
if Tool then
	local DialogFunc = ToolDialog_Mapping[Tool.Name]
	if DialogFunc then
		DialogFunc()
	end
else
	...
end
1 Like

a = game.Workspace.DialogHandler

Players = game.Players:GetChildren()

for i, v in pairs(Players) do
a[“Dialog_”…v.Name].Dialog1.Visible = false
end

a.Dialog_Player.Dialog1.Visible = true

game.Workspace.DialogHandler.Dialog_Player.Dialog1.Visible = false

local RP = game:GetService(“ReplicatedStorage”)
local Events = RP.Events

Events.Talking.OnServerEvent:Connect(function(player,npc,dialog,color)
if game.Players:FindFirstChild(player.Name) then
if game.Workspace.DialogHandler:FindFirstChild(“Dialog_”…player.Name) then
game.Workspace.DialogHandler[“Dialog_”…player.Name].Dialog1.Visible = true
game.Workspace.DialogHandler[“Dialog_”…player.Name].Dialog1.Color = Color3.fromRGB(color[1],color[2],color[3])
game.Workspace.DialogHandler[“Dialog_”…player.Name].Dialog1.DialogName.Text = npc
game.Workspace.DialogHandler[“Dialog_”…player.Name].Dialog1.DialogText.Text = dialog
end
end
end)

You can use the Player.Backpack and InventoryItem classes to easily find out if a player has a certain item.

local Events = RP.Events

local text = "Gimme cheese"
local triggered = false

game.Players.PlayerAdded:Connect(function(player)
    script.Parent.TalkPrompt.Triggered:Connect(function()
        if triggered == false then
            local cheese = false
            for i,v in pairs(player.Backpack:GetChildren()) do
                if v:IsA("InventoryItem") and v.Name == "Cheese" then
                    cheese = true
                    break
                end
            end
            if cheese then
                Events.Talking:FireClient(player,"Rat","Yay! You gave me cheese!","1, 1, 1")
                triggered = true
            else
                Events.Talking:FireClient(player,"Rat",text,"1, 1, 1")
                triggered = true
            end
        else
            wait(5)
            triggered = false
        end
    end)
end)
1 Like

Ill try this when I can

local toolName = 'TOOL NAME HERE'

script.Parent.TalkPrompt.Triggered:Connect(function(plr)
if plr.Character:FindFirstChild(toolName) then
-- detected that they're holding the tool. now you can put the rest of the script here.
end
end)
2 Likes