Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
zadobyte 692 Moderation Voter
4 years ago
Edited 4 years ago

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

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. Nath390Fish 19 — 4y
0
np zadobyte 692 — 4y
Ad
Log in to vote
0
Answered by
Raccoonyz 1092 Donator Moderation Voter
4 years ago
Edited 4 years ago

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)
Log in to vote
0
Answered by 4 years ago
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

Log in to vote
0
Answered by 4 years ago

You should use @EmeraldLimes 's module Attributez rather than creating an ObjectValue

Log in to vote
0
Answered by 4 years ago

Thank's for the replies guys.

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

Answer this question