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 that allows you to become the owner of the part when touched?

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

✓ Accepted answer

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)

1 comment

0

Thank you :)

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.

1 comment

0

I've never used that before. Struggling to implement it.. But thank you for pointing me in the right direction.

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. ^^

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