Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is It possible to make FindFirstChild find a part with another parts name?

so i want to find a workspace child with a folders name, for example if folder name is abc, i want it to find a child in workspace with folders name, in this case its gonna be abc.

my english isnt too god but i hope you understand what im talking about

6 comments

0

no i mean if i have a part named ABC, its gonna find a child in workspace which is also named ABC

0

ok so look im making a tycoon. and i want to make it so the cash from ore goes directly to player. furnace contains a folder which is automatically named with the players name when he joins (since its a singleplayer tycoon). Then i want to do something like this: game.Players:GetPlayerFromCharacter(game.Workspace:findFirstChild(folder.name)), but it doesnt work.

0

Try replacing that last line with, "game:GetService("Players"):FindFirstChild(folder.Name)"

0

doesnt work

0

Can you post your code and explorer?

0

ok ill post it in answers soon

3 answers

Ok! Here it is! Put this script inside the folder:

local Folder = script.Parent
local Child = game.Workspace:WaitForChild(Folder.Name)
If Child then
	--Do whatever you want!
end

ok so heres the script

local furnace = script.Parent.Parent
wait(0.5)
local folder = script.Parent.Name
furnace.Touched:Connect(function(hit)
	if hit:FindFirstChild("Cash") then
		local player = game.Players:GetPlayerFromCharacter(game.Workspace:FindFirstChild(folder.name))
		multiplier = script.Parent.Parent.Parent.Multiplier.Value
		wait(0.1)
		hit.BrickColor = BrickColor.new("Dark stone grey")
		wait(0.1)
		hit.BrickColor = BrickColor.new("Really black")
		wait(0.1)
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + hit.Cash.Value * multiplier
		hit:Destroy()
	end
end)

and heres the explorer http://prntscr.com/qry9ow

oh ye and i changed the code to what it was before since none of answers worked

8 comments

0

What is the "aaa" on line 6?

0

i just changed it to something random since it didnt work. ima change it to folder.name now tho

0

it doesnt work even with folder.name tho

0

You've made sure that when you test your game the folders name is changed to the player's name?

0

yes it is

0

Any errors? Describe in as much detail as you can what you want to happen and what is happening right now.

0

No errors until ore hits the furnace. When it does i get this: 21:13:27.157 - Argument 1 missing or nil

You should only use GetPlayerFromCharacter if you have the player's character. Since you don't have it (you're hoping to look it up but as you've mentioned there are multiple things in the workspace with the player's name), it is better to just use game.Players:FindFirstChild(folder.Name) That could only fail if you put things directly in game.Players.

You have a few other problems with your script:

  • multiplier should be local. If you declare it local on the same line as multiplier = then if Multiplier.Value changes while the animation plays, the cash will be increased by the "old" value (which is probably what you want). If you declare it local at the top of the script (which has the same end result as not declaring it local at all, but it is good practice to declare your variables 'local'), then if a Cash Object #1 hits, then the Multiplier increases (or decreases), then Cash Object #2 hits, all in the span of 0.2 seconds, then both Cash Objects will increase the player's cash by the new multiplier. Another option is to access Multiplier.Value at the very last moment (when you increase cash).
  • If a Cash object bounces/hits the furnace a few times in a row, it will end up awarding cash for each time it makes contact. To fix this, you can track the object in a table (but be sure to remove it from the table before the end of the function).

Resulting script:

local furnace = script.Parent.Parent
wait(0.5)
local folder = script.Parent.Name
local touched = {} -- Dictionary of parts that have already made contact with the furnace
local multiplierObject = script.Parent.Parent.Parent.Multiplier
furnace.Touched:Connect(function(hit)
    if touched[hit] then return end -- already handling 'hit'
    local hitCash = hit:FindFirstChild("Cash")
    if hitCash and hitCash:IsA("IntValue") then
        touched[hit] = true
        local player = game.Players:FindFirstChild(folder.Name)
        local multiplier = multiplierObject.Value
        wait(0.1)
        hit.BrickColor = BrickColor.new("Dark stone grey")
        wait(0.1)
        hit.BrickColor = BrickColor.new("Really black")
        wait(0.1)
        local leaderstats = player and player:FindFirstChild("leaderstats") -- will only try to get leaderstats if player was found
        if leaderstats then
            local cash = leaderstats:FindFirstChild("Cash")
            if cash then
                cash.Value = cash.Value + hit.Cash.Value * multiplier
            end
        end
        hit:Destroy()
        touched[hit] = nil
    end
end)

Note that I used FindFirstChild on the leaderstats and Cash to ensure that the script doesn't error (ex if the player left right after they received a cash value), or else a memory leak would occur (since touched[hit] would remain true forever). An other option is to mark the touched table as having weak keys, which tells Roblox/Lua that it can remove objects from the table if they're not referenced by anything else: local touched = setmetatable({}, {__mode="k"}).

This discussion is preserved as it was. New answers, comments, and votes are closed.