FindFirstChild not working

I already made a post about this earlier today but I just realised that there are still problems which I can’t fix myself and I already set the solution there. So I’m making my own admin gui because I want to improve. I have 3 textboxes set up so if I type a player’s name, a value and a number inside the 3 textboxes it would give the player 1 money for example. Now the problem is that the 2nd print doesn’t get printed. I have no idea why it doesn’t go through the findfirstchild because it shows no errors. This is all inside a serverscript with a remote event so datasave can save the data.

local rs = game.ReplicatedStorage
local re = rs:WaitForChild("DeveloperMenu"):WaitForChild("ValueSetterSubmit")
re.OnServerEvent:Connect(function(player)
	local players = game:GetService("Players")
	local numberbox = player.PlayerGui.DeveloperMenu.MainFrame.ValueSetter.NumberBox.Text
	local valuebox = player.PlayerGui.DeveloperMenu.MainFrame.ValueSetter.ValueBox.Text
	local playerbox = player.PlayerGui.DeveloperMenu.MainFrame.ValueSetter.PlayerBox.Text
	print("locals defined")
	if players:FindFirstChild(playerbox) then
		print("found player")
		player = players:FindFirstChild(playerbox)
		if player.Stats:FindFirstChild(valuebox) then
			local value = player.Stats:FindFirstChild(valuebox)
			value.Value = numberbox
		end
	end
end)

Help is really appreciated!

2 Likes

You cannot use findfirstchild with instances, it needs to be a string, here is the fix:
FindFirstChild(player.name)

I’m not sure where player is defined (line 9), but I know you can’t get input from textboxes from the server. In an admin GUI I made, I used a local script that fires a remoteevent with the input values, then the functionality is done by the server. I hope this helps!

Edit: it looks like the player issue was a typo fixed. Disregard the first bit

FindFirstChild is supposed to find a children of a thing.
Such as

Object:FindFirstChild("Object Name Here")

You are trying to find a child thats already been navigated. FindFirstChild supposed to be for objects under the name/Object/Player (players in this case)

Use a string instead of an instance

1 Like

I understand what you are saying but this perfectly worked when it was in a local script.
And how exactly would I use a string instead of an instance?

I think that would work but I can’t really imagine how the script would look like.

I assume the script is server-sided because of re.OnServerEvent:Connect(function(player). I believe Hyperspace got it right about the TextBoxes being client sided, so the best thing to do would be to use both a LocalScript and a server-sided Script. The LocalScript should look something like this:

local rs = game.ReplicatedStorage
local re = rs:WaitForChild("DeveloperMenu"):WaitForChild("ValueSetterSubmit")

local player = game.Players.LocalPlayer

function executecommand() -- Connect this to a button click or the enter key
	local valuesetter = player.PlayerGui.DeveloperMenu.MainFrame.ValueSetter
	
	re:FireServer(valuesetter.PlayerBox.Text, valuesetter.ValueBox.Text, valuesetter.NumberBox.Text)
end

And the server-sided Script should look something like this instead:

local admins = {304374806} -- A list of admins' user id's

local rs = game.ReplicatedStorage
local re = rs:WaitForChild("DeveloperMenu"):WaitForChild("ValueSetterSubmit")

local players = game:GetService("Players")

re.OnServerEvent:Connect(function(player, targetplayer, value, number)
	if not table.find(admins, player.UserId) then return end -- Check if player is admin
	
	local player = players:FindFirstChild(targetplayer)
	if player then -- Check if the player exists
		print("found player")
		local valueinst = player.Stats:FindFirstChild(value)
		if valueinst then -- Check if the value exists
			valueinst.Value = number -- Change the value
		end
	end
end)
2 Likes

Damn bro. Yeah this works perfectly fine as I can see. I should definitely start putting text in the fireserver column from now on. Thank you very much! This helps a lot.

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