How to find which value has the player's name?

I’ll try to explane what I am trying to do. I started learning lua about a month ago, so sorry for any missunderstanding I might have.

I have this local script for a local button. Local script sends the player’s name and a string value to a server script (via RemoteEvent).

I already wrote the script for the event. Might look a bit messy but here it is (function fires when button is clicked or touchTapped):

local function giveItem(player)
getitemButton.Visible = false
Event:FireServer(itemValue.Value)
end

I have this model in workspace that stores multiple parts, and every part has a stringvalue in them. When the part is clicked, stringvalue’s value changes to the player’s name (player who clicked the part). It’s an ownership system.

Here is what I want to do:
I want a server script to look inside the model after getting the remoteEvent signal, check which stringvalue has the player’s name in it and do a function with value.Parent (part that is the parent of the string value).

How do I check which value has the player’s name in the model? Thank you for your time. ^^

Something like this?

local model = --the model

function getItem(name)
	for i, part in pairs(model:GetChildren()) do
		if part:FindFirstChildOfClass("StringValue") then
			if part:FindFirstChildOfClass("StringValue").Value == name then
				return part
			end
		end
	end
	
	return nil
end
1 Like

Yeah, something like it. I’m still trying to understand how FindChild works so I’ll try this script tommorow. Thank you so much ^^

Ah, I can explain that to you. :FindFirstChild() returns the child of an object (the one you called FindFirstChild on) with a certain name. If there is no child matching that name, then it will return nil. :FindFirstChildOfClass() returns a child of an object but instead with a certain class (ex: StringValue, Player, Model), and once again returns nil if there is no object found.

1 Like

So if I understand it correctly, with script you write you first find the ChildOfClass which is a stringvalue, and when stringvalues are found you search for the specific one that has the value = plr.Name. I’m guessing after the “then” in line 6 I can write a function for it?

Also thank you so much for explaning the :FindFirstChild, I really appreciate it.

1 Like

This script works perfectly! Thank you so much again. If I finish this game, I’ll add your user in credits. Thank you so much ^^