How to change response dialogue through script

i made a script that u can give a npc an apple, and i want to make it so that if u give the apple, it fires an event and he says thx, and if u dont have the apple, it fires and event and he says, get an apple. how to do it?

You can learn about it here. It works local-side only so you have to make an event.

i alr read that page and i dont understand how to change what he says after giving the apple

As you can see in the image,

obraz_2024-09-04_171211613

you can add multiple dialog choices. To the one you select to get the apple, add more choices.

its not that, when i choose the give apple one, if i have an apple tool ,it gives the apple and i want it to respond thanks, if i dont have the apple tool and select that option, i want him to say get an apple

If the apple is a tool you can do something like this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Backpack = Player.Backpack

local Apple = game.ReplicatedStorage.Apple

for I,v in Backpack:GetChildren() do 
if v:IsA("Tool") and v.Name == Apple.Name then
-- if player does have the apple
else
--If the player doesn't have the apple
end
end

--Do the same with the character

for I,v in Character:GetChildren() do 
if v:IsA("Tool") and v.Name == Apple.Name then
-- if player does have the apple
else
--If the player doesn't have the apple
end
end

Not the most optimised but it should work.

where will i put this? and also i want it to say the dialogues like thx or get an apple

Oh, you can add to the script “game:GetService(“Chat”):Chat(YourCharacter, “Message”)” This will add a chat bubble to the player’s character.

i want the npc to say it not the player

You can make this a function and fire it after a specific dialog, and I think you can script dialog, Im not exerienced with dialog, so look into the api.

im also not experienced with dialog, so idk how to make him say something after i give him the apple

Then add to the dialog choice “thanks” for the npc.

but if i add a choice then that would make the player say it right? and also i want it so like the script makes him say thx and then a event is fired as i want to spawn something when he says thx

You can do :DialogChoiceSelected()

this is the apple giving script ( server )

wait(1)

local Dummy = script.Parent.Parent.Parent
local sound = script.EatSound
local RemoveAppleEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoveApple")
local ToolName = "Apple"

RemoveAppleEvent.OnServerEvent:Connect(function(plr)
	if plr.Backpack:FindFirstChild(ToolName) then
		-- Find on backpack
		local tool = plr.Backpack:FindFirstChild(ToolName)
		tool:Destroy()
		sound:Play()
	elseif plr.Character:FindFirstChild(ToolName) then
		-- Find on character
		local tool = plr.Character:FindFirstChild(ToolName)
		tool:Destroy()
		sound:Play()
	end
end)

local

wait(2)

local dialog = game:GetService("Workspace").Dummy:WaitForChild("Head").apple
local RemoveAppleEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoveApple")

dialog.DialogChoiceSelected:Connect(function(player, choice)
	if choice == dialog.give then
		RemoveAppleEvent:FireServer()
		print("Choiced!")
	else
		print("Choice is not 'give'")
	end
end)

so from these how do i make him say thx when the apple is destroyed

Use the script I wrote for you?

Make it a function and fire it

where do i add this script in those

Make it on the server side and destroy the apple after the function has fired.