Why doesn't this work?

Hey Roblox devs. I’m trying to make it so that when you press e, dialog shows up and it says “hello” and your name. I’m not sure why this doesn’t work.

game.ReplicatedStorage.PlayerName.Value is a string value, and I made it so that when the player presses e the name of the player that pressed it gets passed to this string value. The string value changes, but not the dialog.
Screenshot 2022-07-26 204018

you might think that it’s a problem with my local script, but if it was then the PlayerName value wouldn;t change at all. I might be wrong though.

1 Like

this table is getting the current value of the stringValue and putting it in [1]
if the string value changes, the string value replaces the string, but [1] continues to be the old string

instead, I’d maybe store functions in [1], [2], and [3] to do things like get the string value each time when the function is called
e.g.,

t = {
   [1] = function()
      return "abc" .. SomeStringValue.Value
   end,
}
local DialogModule = {}
DialogModule.Dummy = {
[1] = "Hello "..game.ReplicatedStorage.PlayerName.Value,
[2] = "Message Two",
[3] = "Message Three"
}

return DialogModule

So you can make stuff inside the first table in a module script instead do what I did on top. (IDK HOW TO EXPLAIN)

Your code gave me an error. “Module code did not return exactly one value.”

is the script that you’re requiring the module from a script or a local script?

Your code didn’t work. Also, I don’t understand the point of making the table seperatly, or how DialogModule.Dummy works. Where is “Dummy” getting defined? The NPC is under a folder named “NPC” and the NPC itself is named Dummy. I’m sorry I don’t understand, I’m not that familiar with Roblox coding.

Local Script, because when you press e (I have a proximity prompt in the Dummy) the local script recognizes it, and it changes the game.ReplicatedStorage.PlayerName Value to the local player name. That works perfectly fine, the value changes and everything but what doesn’t change is the dialog text itself.

can you show me the localscript?
like the part where you’re requiring the table from the module.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local DialogModule = require(ReplicatedStorage.DialogModule)
local p = Players.LocalPlayer
local NPCs = workspace.NPCs
local DialogFrame = script.Parent.DialogFrame
local InputButton = DialogFrame.Input
local NPCName = DialogFrame.NPCName

local DIALOG_TWEENINFO = TweenInfo.new(0.25, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)

local dialogOpen = false
local dialogTween = nil
local dialogIndex = 0
local gradualTextInProgress = false


local function gradualText(text)
	if gradualTextInProgress then
		return
	end
	
	local length = string.len(text)
	
	for i = 1, length, 1 do
		gradualTextInProgress = true
		DialogFrame.DialogText.Text = string.sub(text, 1, i)
		wait()
	end
	
	gradualTextInProgress = false
end

local function onDialog(dialog, index, proximityPrompt)
	game.ReplicatedStorage.PlayerName.Value = game.Players.LocalPlayer.Name
	if dialog[index] then
		gradualText(dialog[index])
	else
		if dialogTween then
			dialogTween:Cancel()
			dialogTween = nil
		end
		local tween = TweenService:Create(DialogFrame, DIALOG_TWEENINFO, {
			Position = UDim2.new(0, 0, 2, 0)
		})
		dialogTween = tween
		dialogTween:Play()
		
		proximityPrompt.Enabled = true
		dialogOpen = false
		dialogIndex = 0
		p.Character.HumanoidRootPart.Anchored = false
	end
end


InputButton.MouseButton1Click:Connect(function()
	if gradualTextInProgress then
		return
	end
	
	local dialog = DialogModule[NPCName.Value]
	dialogIndex += 1
	onDialog(dialog, dialogIndex, NPCs[NPCName.Value].HumanoidRootPart.ProximityPrompt)
end)

for _, NPC in pairs(NPCs:GetChildren()) do
	local humanoidRootPart = NPC:FindFirstChild("HumanoidRootPart")
	local proximityPrompt = humanoidRootPart:FindFirstChild("ProximityPrompt")
	
	if humanoidRootPart and proximityPrompt then
		--proximityPrompt.ObjectText = NPC.Name
		proximityPrompt.ActionText = "Talk "
		
		local dialog = DialogModule[NPC.Name]
		
		proximityPrompt.Triggered:Connect(function()
			if dialogOpen then
				return
			end
			
			dialogOpen = true
			proximityPrompt.Enabled = false
			NPCName.Value = NPC.Name
			p.Character.HumanoidRootPart.Anchored = true
			
			if dialogTween then
				dialogTween:Cancel()
				dialogTween = nil
			end
			local tween = TweenService:Create(DialogFrame, DIALOG_TWEENINFO, {
				Position = UDim2.new(0, 0, 1, 0)
			})
			dialogTween = tween
			dialogTween:Play()
			
			dialogIndex = 1
			onDialog(dialog, dialogIndex, proximityPrompt)
		end)
	end
end

my bad, just this part

local function onDialog(dialog, index, proximityPrompt)
	game.ReplicatedStorage.PlayerName.Value = game.Players.LocalPlayer.Name
	if dialog[index] then
		gradualText(dialog[index])
	else
		if dialogTween then
			dialogTween:Cancel()
			dialogTween = nil
		end
		local tween = TweenService:Create(DialogFrame, DIALOG_TWEENINFO, {
			Position = UDim2.new(0, 0, 2, 0)
		})
		dialogTween = tween
		dialogTween:Play()
		
		proximityPrompt.Enabled = true
		dialogOpen = false
		dialogIndex = 0
		p.Character.HumanoidRootPart.Anchored = false
	end
end

i don’t see the part where you’re changing the dialog’s text to "Hello "…PlayerName.Value

that part is in a module script. It’s seperate.

yeah what i meant is

DiaglogTextLabel.Text = RequiredModule...Table..

Are you telling me to use that script or are you asking me where that is? I’m going to be honest, I copied most of this from a tutorial to save time for myself. I can find the video if you need it.

1 Like

First of all, a module script will return exactly one value if you fire a function inside it, for your module script its an entire table, and you know that?

Second, you cannot expect the module to work on its own, it will return an error, and I am not sure why other developers here haven’t noticed it yet?


Now the thing it won’t work is

Imagine this:

local avalue = 100
avalue = 200

Now the avalue will become 200 ok?
No string/int value is getting changed its a variable which is getting changed

The same thing your doing here:

local value = game.ReplicatedStorage.PlayerName.Value -- the first value

Now this above line is something like: (assuming the player name is hi by default)

local value = "hi"
value = "hello" --the variable changed but the value of the playername instance didnt

To achieve the value changing u should do this:

local valueinstance = game.ReplicatedStorage.PlayerName
valueinstance.Value = "hello" -- now it will change

Try to understand it

I’m not too familiar with module scripts, I didn’t know that. What your telling me to do is what im already doing, from what I see. Again, I don’t know nodule scripts that well so your probably right

Are you trying to achieve something like a dialog changer every second?
I could help you make a script with that, but I never use module scripts, they are meant for repetitive tasks, you aren’t doing a repetitive task here

No. I’m trying to make it so that when a player presses e they get dialog saying hello and that players name. Let’s say player A presses it. Since it’s a local script, if another player were to press it, the dialog wouldn’t say “hello player A” it would say “hello player B.” That should work, because it’s a local script. I may be mistaken since the player name value is on the server, but that’s briefly what im trying to do.

1 Like

So you need the player name thats all?

Yes.

Aaaaaaaaaaaaaqqqqqqaaaaaaaaaaaaaaaaa