GetCurrentPlayers returning nil?

  1. What do you want to achieve?
    I’m making an NPC for my game that players talk to when they want to change their loadout. I need the backpack of the player talking to the NPC to be reset each time they talk to the NPC
    (one person at a time can speak to the NPC)
  2. What is the issue?
    My problem is that when I use dialog:GetCurrentPlayers, it returns nil. and I don’t know why.
  3. What solutions have you tried so far?
    I’ve tried printing, and it indeed returns nil. I’ve also tried asking the roblox AI assistant, but it was as useless as ever, and I already looked for similar problems, but found nothing. any help is much appreciated!
    Here’s my function.
dialog:GetPropertyChangedSignal("InUse"):Connect(function()
	if dialog.InUse == true then
	local player = dialog:GetCurrentPlayers()
	print(player.Name)
		player.character:FindFirstChild("Humanoid"):UnequipTools()
		player:FindFirstChild("Backpack"):ClearAllChildren()
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

:GetCurrentPlayers() doesnt return just one player. It returns a list of players that are currently usng the dialog.

oh that makes sense. how do i get the one player using it then?

dialog:GetCurrentPlayers()[1], check if this is nil, but that will return you just one player from it.

But, for what you are doing, I would just loop through each one:

for _, player in ipairs(dialog:GetCurrentPlayers()) do
...
end
1 Like

I was wrong, either im doing something wrong or what you said wasn’t going to work. I’m not sure what was wrong or how to fix it, but here’s the whole script

dialog = workspace:FindFirstChild("shop"):FindFirstChild("Rdite"):FindFirstChild("Head"):FindFirstChild("Ask item")
local myplayer = game.Players:GetPlayerFromCharacter(script.Parent)

game.ReplicatedStorage:FindFirstChild("AddToBackpack").OnServerEvent:Connect(function(player, itemName)
	local tool = game.ServerStorage:FindFirstChild("tools"):FindFirstChild(itemName)
	local toolClone = tool:Clone()
	toolClone.Parent = player:FindFirstChild("Backpack")
end)

dialog:GetPropertyChangedSignal("InUse"):Connect(function()
	if dialog.InUse == true then
		for _, v in ipairs(dialog:GetCurrentPlayers()) do
		if v == myplayer then
		player = v
		end
	end
		player.character:FindFirstChild("Humanoid"):UnequipTools()
		player:FindFirstChild("Backpack"):ClearAllChildren()
	end
end)

local (in starterplayerscripts)

local dialog = workspace:FindFirstChild("shop"):FindFirstChild("Rdite"):FindFirstChild("Head"):FindFirstChild("Ask item")

local function onChoiceSelected(player, choice)
	local itemName = choice.Name
	game.ReplicatedStorage:FindFirstChild("AddToBackpack"):FireServer(itemName)
end

dialog.DialogChoiceSelected:Connect(onChoiceSelected)
1 Like

Are you allowing multiple people to use the dialog or only one?

If it’s only one you can just do dialog:GetCurrencyPlayers()[1] like I suggested earlier, but if multiple people can use the dialog, you should move the player related code into the loop as well.

Also do print(dialog:GetCurrentPlayers()) and make sure that it is even returning anything. Always debug first before you come back saying it doesn’t work!

1 Like

Oh I’m sorry, I didn’t actually say what the new problem was. the new problem isn’t that its removing the tools for everyone, but even though only one person can use the dialog, but its giving the tools once per player in the server to the person using it. I edited my last reply to include the local stuff aswell

1 Like

Ok next time, don’t make a post starting off with “this didn’t work” if the problem isn’t that the code didn’t fix the issue you posted about, but instead the fact that fixing it revealed another problem with your code! Removing the solution checkmark on a post that fixes the problem only to add it back on another post that is irrelevant to what you originally posted about is not great for people who may run into this issue in the future.

It’s perfectly fine to ask for more help, but if your original problem is solved, don’t un checkmark it, just leave it and continue the conversation after it.

It’s possible that the DialogChoiceSelected is fired anytime a player - and not just the LocalPlayer - chooses a dialog option. ProximityPrompts work the exact same way.

I would recommend that you verify the player argument equals the LocalPlayer.

So player == Players.LocalPlayer

My mistake. will correct in the future

this actually didn’t work. it just did not effect the issue, not sure if I did it wrong, or if theres a problem in the code that isn’t because of me. the second client also doesn’t print anything

local dialog = workspace:FindFirstChild("shop"):FindFirstChild("Rdite"):FindFirstChild("Head"):FindFirstChild("Ask item")
myPlayer = game.Players.LocalPlayer

local function onChoiceSelected(player, choice)
	local itemName = choice.Name

	if player == myPlayer then
	game.ReplicatedStorage:FindFirstChild("AddToBackpack"):FireServer(itemName)
	end
end

dialog.DialogChoiceSelected:Connect(onChoiceSelected)

trying to check the player before calling the onChoiceSelected()
results in the event never firing

edit: fixed it myself by placing the remote event in starter character scripts. here is new code

local dialog = workspace:FindFirstChild("shop"):FindFirstChild("Rdite"):FindFirstChild("Head"):FindFirstChild("Ask item")
myPlayer = game.Players.LocalPlayer

local function onChoiceSelected(player, choice)
	local itemName = choice.Name
	if player == myPlayer then
	myPlayer.Character:FindFirstChild("AddToBackpack"):FireServer(itemName)
	end
end

dialog.DialogChoiceSelected:Connect(onChoiceSelected)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.