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

How to make a script that allows you to become the owner of the part when touched?

Asked by 3 years ago
local Button = script.Parent.Button2
local Drop = script.Parent.Drop
local Ready = true

function MakeBlock()
    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"


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

This script allows a button to create a part, which is named Batter, then after 400 seconds, deletes it.

I need to know how to make the script allow someone to become the owner of the created part when they touch it.

I've tried many things, but they've all failed.

My main goal with my game is to do something similar to what Lumber Tycoon 2 does with chopped down trees. (Getting money from a part touching another part)

3 answers

Log in to vote
0
Answered by
Gingerely 245 Moderation Voter
3 years ago

I would recommend using object value followed as

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"

    if not Button.Parent:FindFirstChild('Player Who Owns the Part') then
        plrValue = Instance.new('ObjectValue', Button.Parent);--Assuming button is the      click detector and Button.Parent is the part;
        plrValue.Value = plr;
        plrValue.Name = 'Player Who Owns the Part';
    else
        return; -- if someone owns it then it returns making the event useless for the other person
    end;
    if plrValue ~= nil then
        -- do stuff plrValue.Value would be player
    end;
    game:GetService'Debris':AddItem(
        game.Debris:AddItem(Block, 400)
        wait(3)
        Ready = true
    end
end
Button.ClickDetector.MouseClick:Connect(MakeBlock)
0
Thank you :) Nath390Fish 19 — 3y
Ad
Log in to vote
0
Answered by
zadobyte 692 Moderation Voter
3 years ago

You could use CollectionService to tag the part with the player's name (:AddTag(Block, plr.Name [assuming you had plr as a parameter of the clickdetector function]) After that you can use :GetTags(Block)[1] to check who owns this part.

0
I've never used that before. Struggling to implement it.. But thank you for pointing me in the right direction. Nath390Fish 19 — 3y
Log in to vote
0
Answered by 3 years ago

So now i've made it insert a value, which was another one of my ideas.

    local String = Instance.new("StringValue", game.Workspace.Cake.Batter)
    String.Name = "DefaultName"
    String.Value = 1

With this, I could use another script that could change the value on touch..

I'll figure something out..

If anyone knows a way I could link a value to a human, that would be fantastic. ^^

Answer this question