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

How to make a script insert a string into the newest part in a group?

Problem:

local String = Instance.new("ObjectValue", game.Workspace.Cake:FindFirstChild("Batter"))
		String.Value = plr
		String.Name = "Player Who Owns the Part"

Creates a string but puts it into the same part every time a part is made, and not the newest part.

Full Code:

local Button = script.Parent.Button2
local Drop = script.Parent.Drop
local Ready = true
local plrValue;


function MakeBlock(plr) -- added plr, because first argument of mouseclick is the player who has clicked it
    if Ready then
        Ready = false
        local Block = Instance.new("Part", game.Workspace.Cake)
        Block.CFrame = Drop.CFrame
        Block.Size = Vector3.new(3, 1.2, 3)
        Block.Material = Enum.Material.SmoothPlastic
        Block.BrickColor = BrickColor.new("Pastel brown")
		Block.Name = "Batter"
		
		

local String = Instance.new("ObjectValue", game.Workspace.Cake:FindFirstChild("Batter"))
		String.Value = plr
		String.Name = "Player Who Owns the Part"
		


        game.Debris:AddItem(Block, 400)
        wait(3)
        Ready = true
		    end
		end
Button.ClickDetector.MouseClick:Connect(MakeBlock)

Everything works, I just need it to insert a String into the newest part of the group..

This script listens to a button that tells the script to make a new part called Batter, in a group named Cake.

Then it gives the Batter a Object Value which states the owner.

It keeps placing the string into the same part though.. Here's whats happening. I need to find a way to make it put a string in the newest part

If anyone can help, that would be great. :)

5 answers

✓ Accepted answer

Just set the object's parent to Block instead of :FindFirstChild("Batter")

2 comments

0

Wow haha. Thank you. :D I dont know how I didnt think of that. I sat here for a while thinking of complicated solutions, yet the answer was so simple.

0

np

FindFirstChild does what it says - it finds the first child that matches the name. The newest batter won't be the first child.

local Button = script.Parent.Button2
local Drop = script.Parent.Drop
local Ready = true
local plrValue;


function MakeBlock(plr) -- added plr, because first argument of mouseclick is the player who has clicked it
    if Ready then
        Ready = false
        local Block = Instance.new("Part", game.Workspace.Cake)
        Block.CFrame = Drop.CFrame
        Block.Size = Vector3.new(3, 1.2, 3)
        Block.Material = Enum.Material.SmoothPlastic
        Block.BrickColor = BrickColor.new("Pastel brown")
        Block.Name = "Batter"



local String = Instance.new("ObjectValue")
String.Parent = Block
        String.Value = plr
        String.Name = "Player Who Owns the Part"



        game.Debris:AddItem(Block, 400)
        wait(3)
        Ready = true
            end
        end
Button.ClickDetector.MouseClick:Connect(MakeBlock)
local Button = script.Parent.Button2
local Drop = script.Parent.Drop
local Ready = true
local plrValue;
local newBatter;

function MakeBlock(plr) -- added plr, because first argument of mouseclick is the player who has clicked it
    if Ready then
        Ready = false
        newBatter  = Instance.new("Part", game.Workspace.Cake)
        Block.CFrame = Drop.CFrame
        Block.Size = Vector3.new(3, 1.2, 3)
        Block.Material = Enum.Material.SmoothPlastic
        Block.BrickColor = BrickColor.new("Pastel brown")
        Block.Name = "Batter"

	local String = Instance.new("ObjectValue", newBatter)
        String.Value = plr
        String.Name = "Player Who Owns the Part"

        game.Debris:AddItem(Block, 400)
        wait(3)
        Ready = true
    end
end
Button.ClickDetector.MouseClick:Connect(MakeBlock)

so i modified to code so that it will do what you wanted, basically i constantly keep a reference to the last created batter and give that the string, sintead of look for the firstchild named batter

Thank's for the replies guys.

All your suggestions look like they'll work. :)

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