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
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
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).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"})
.