Click to own part using object value

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a part that you can claim, and only you can edit that part.

  2. What is the issue? Include screenshots / videos if possible!
    I added to parts to test if it would work, but it didn’t, it will edit for both parts but I don’t know what to do.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried the dev hub, devforum, scripting helpers, and youtube but still haven’t found my answer.

I do have some scripts to show you:

-- Child Of ClickDetector
local PlayerCollectedItem = false
local Status = script.Parent.Parent.Status

game.Players.PlayerAdded:Connect(function(player)
	script.Parent.MouseClick:Connect(function(player)
		Status.Value = player.Name
		PlayerCollectedItem = true
		
script.Parent.Parent.BillboardGui.TextLabel.Text = "Owned By "..player.Name
		local ObjectValue = Instance.new("ObjectValue")
		ObjectValue.Name = "ObjectValue"
		ObjectValue.Value = workspace.MainParts.Main1
		ObjectValue.Parent = player
	end)
end)
-- MainPart1(Child Of Folder, Folder Inside Frame)
local remoteEvent = game:GetService("ReplicatedStorage").EditMessage
local EditButtonUi = game.Players.LocalPlayer.PlayerGui:WaitForChild("EditingUi").EditButton

script.Parent.Parent.DoneButton.MouseButton1Click:Connect(function()
	remoteEvent:FireServer(workspace.MainParts.Main1, "name", script.Parent.Parent.GameName.Text)
	remoteEvent:FireServer("description", script.Parent.Parent.Description.Text)
	game.Players.LocalPlayer.PlayerGui:WaitForChild("EditingUi").EditGui.Visible = false
	EditButtonUi.Visible = true
end)

remoteEvent.OnClientEvent:Connect(function(type_, message)
	if type_ == "name" then
		workspace.MainParts.Main1.SurfaceGui.GameNameLabel.Text = message
	elseif type_ == "description"then
		workspace.MainParts.Main1.SurfaceGui.GameDescriptionLabel.Text = message
	end
end)
-- MainPart2(Child Of Folder, Folder Inside Frame)
local remoteEvent = game:GetService("ReplicatedStorage").EditMessage
local EditButtonUi = game.Players.LocalPlayer.PlayerGui:WaitForChild("EditingUi").EditButton

script.Parent.Parent.DoneButton.MouseButton1Click:Connect(function()
	remoteEvent:FireServer("name", script.Parent.Parent.GameName.Text)
	remoteEvent:FireServer("description", script.Parent.Parent.Description.Text)
	game.Players.LocalPlayer.PlayerGui:WaitForChild("EditingUi").EditGui.Visible = false
	EditButtonUi.Visible = true
end)

remoteEvent.OnClientEvent:Connect(function(type_, message)
	if type_ == "name" then
		workspace.MainParts.Main2.SurfaceGui.GameNameLabel.Text = message
	elseif type_ == "description" then
		workspace.MainParts.Main2.SurfaceGui.GameDescriptionLabel.Text = message
	end
end)
--Server Script(Child Of Surface Gui's. Suface Guis inside the main Parts)
local remoteEvent = game:GetService("ReplicatedStorage").EditMessage

game.Players.PlayerAdded:Connect(function(player)
	script.Parent.Parent.ClickDetector.MouseClick:Connect(function(player)
		player.PlayerGui:WaitForChild("EditingUi").EditGui.Visible = true
	end)
end)

remoteEvent.OnServerEvent:Connect(function(player, type_, message)
	remoteEvent:FireAllClients(type_, game:GetService("Chat"):FilterStringForBroadcast(message, player)) --Send our message back to all clients.
end)

image

robloxapp-20210831-1955311.wmv (1.1 MB)

1 Like

I made a quick script, I used userIds instead of player names but it really doesn’t matter:

local OwnedBy = script.Parent.Parent.OwnedBy --Int value to store userId
local UserPartData = require(game.ServerStorage.UserPartData)

local function doEdit(player) --Run function once all check are done
	table.insert(UserPartData, player.UserId, true) --Add value to table (true meaning the user owns a part already)
	--logic
end

script.Parent.MouseClick:Connect(function(player)
	local userAlreadyOwnsAPart = table.find(UserPartData, player.UserId) --Check if user already owns a part
	
	if userAlreadyOwnsAPart == false or userAlreadyOwnsAPart == nil then --if user doesn't own a part yet they can claim one
		if OwnedBy.Value == 0 then --Check that no one else already owns it
			OwnedBy.Value = player.UserId --Assign value
			doEdit(player)
		end

	else --if they do own one then they can edit one but only if it's theirs
		if OwnedBy.Value == player.UserId then
			doEdit(player)
		end
	end
end)


-- Better put this part of the script in a seperate server script inside ServerScriptService to save resources --
game.Players.PlayerRemoving:Connect(function(player) --Remove player from UserPartData and reset their part when they leave
	if UserPartData[player.UserId] ~= nil then
		table.remove(UserPartData, player.UserId)
	end
end)

Note I didn’t test it

Error:

23:39:53.814 Workspace.MainParts.Main1.ClickDetector.Script:10: invalid argument #1 to ‘find’ (table expected, got Instance) - Server - Script:10

There was also another error about the require part, and is the UserPartData supposed to be an int value object value or something else? I put it as an object value then changed it to a int value but its the same error.

There was another error too:

23:39:53.814 Workspace.MainParts.Main1.ClickDetector.Script:10: invalid argument #1 to ‘find’ (table expected, got Instance) - Server - Script:10

My bad, The UserPartData is a ModuleScript inside ServerStorage, this module script will put each player that claims a part inside a table to prevent a user from claiming multiple parts.

1 Like

I added the same script to the a other part, but it still puts my user id in the other part. theres also an error when I leave:

15:37:27.148 911550563 is not a valid member of ModuleScript “ServerStorage.UserPartData” - Server - Script:5

As mentioned before, I used userIds instead of names, because of this I changes StringValue instaances with IntValues. You can use names as well; just change player.UserId to player.Name and make sure that this time you are using StringValues.

Its the exact same but for player name

Ok, I fixed it and added edits to it. Thanks!

1 Like