Dialogue Script

Hello! Thank you for taking your time to read this!

I’m not a script, I’m learning, I’m a modeler though. So I am making a strange horror game. You know how when you play a game, you have the dialogue box that pops up giving you hints and what not, for example, you wake up from bed and everything is dark, you have a dialogue saying something like, “It’s dark in here, I better turn on the light.”

Well I don’t know how to go about doing something like that, maybe something simple where you hit a brick and dialogue comes up, I think that’s how I want it for now, something simple that I can practice and learn.

So, how would I do this?

2 Likes

There are a few ways to accomplish this though for the sake of simplicity I’m only going to deep-dive into one.

So, to execute this dialogue system we are going to be using a combination of Touched, FireClient, and some basic UI tools.

Place down a simple part and insert a script into it like so:
image

Let’s open up this script and see what we can do. First off, we need to establish when the part gets touched initially. To do this we can use [part].Touched, in this case it would be script.Parent.Touched, to identify when another part comes into contact with the main part.

script.Parent.Touched:Connect(function(hit)

end)

Now as you notice, I’ve paired in the argument “hit”, as Touched will always return the part that it detected. Next, we need to determine whether or not it was a player who touched this brick, to do so we can implement a simple sanity check.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		
	end
end)

Now since we have that code inside of the if/then statement will only run if the part has a parent, and the part’s parent has a child named “Humanoid”.

From there, you’ll need to add a RemoteEvent into ReplicatedStorage, for now let’s just name it DialogueEvent.
image

Back in the main script, we can now connect this event to coincide with our Touched event. To do this I’ve employed two functions, RemoteEvent:FireClient and game.Players:GetPlayerFromCharacter.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		game.ReplicatedStorage.DialogueEvent:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
	end
end)

It’s also important to add a debounce so that the part doesn’t spam the notification when a player walks on it, I’ve done that here:

local debounce = false
script.Parent.Touched:Connect(function(hit)
	if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		if not debounce then
			debounce = true
			game.ReplicatedStorage.DialogueEvent:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
			wait(5)
			debounce = false	
		end
	end
end)

Now with that side of the scripting finished, we need to look at altering the client. Let’s start by making a ScreenGUI with a TextLabel inside of it, this can always be changed later to fit your needs, then insert a LocalScript inside of that TextLabel.
image

Now, let’s start off this script by connecting it to the RemoteEvent we used earlier:

game.ReplicatedStorage.DialogueEvent.OnClientEvent:Connect(function()
	
end)

Since no parameters were sent through the FireClient function, we don’t need to add any arguments here.

From here it’s very simple, just editing the text in the textbox and then setting the visibility to false.

game.ReplicatedStorage.DialogueEvent.OnClientEvent:Connect(function()
	script.Parent.Text = "This is a dialogue label"
	wait(5)
	script.Parent.Visible = false
end)

You can make this more or less complex should you choose to, however this is barebones what you would need to create a dialogue system.

7 Likes

Hey, thank you so much! I really appreciate you!