Owner-only object put into a certain folder

This is a support category for asking questions about how to get something done on the Roblox websites or how to do something on Roblox applications such as Roblox Studio.

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to fix my script. the script is trying to make a character for a certain player for new unlocks or Dev-Only Characters

  2. What is the issue? Include screenshots / videos if possible
    Nothing happens from the code, it wont move a selected object from on place to another

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Ive tried asking other friends but i still cant find a way

here is the code and i tried to fix all of the errors
local players = game.Players:GetPlayers()
function CheckForRoz()

for i, player in pairs(players) do
	if player.Name == "Rozurax" then
		print("Test")
		if player:FindFirstChild('Character') then

			local objectName = "OwnerSilver"
			local object = game.Workspace:FindFirstChild(objectName)
			if object then

				object.Parent = game.ReplicatedStorage.Characters
				print("Object moved to ReplicatedStorage.")
			else
				print("Object not found.")
			end
			object:Clone().Parent = player:WaitForChild("ReplicatedStorage").Characters
		end
	end
end

end
game.Players.PlayerAdded:Connect(function()
CheckForRoz()
end)

I am a new scripter and hopefully i can finish this one part of my journey

you’re not updating when players have joined, so I suggest using the player added function

game.Players.PlayerAdded:Connect(function(plr)
	if plr.Name == name then
		print("plr is owner or something")
	end
end)
2 Likes

I had already added that in the first and last line, for some reason it didnt put it in the code box

Oh, well then move the
local players = game.Players:GetPlayers()
inside the CheckForRoz() function. Basically this

function CheckForRoz()
	local players = game.Players:GetPlayers() -- we moved it inside so it updates the variable
	for i, player in pairs(players) do
		if player.Name == "Rozurax" then
			print("Test")
			if player:FindFirstChild('Character') then

				local objectName = "OwnerSilver"
				local object = game.Workspace:FindFirstChild(objectName)
				if object then

					object.Parent = game.ReplicatedStorage.Characters
					print("Object moved to ReplicatedStorage.")
				else
					print("Object not found.")
				end
				object:Clone().Parent = player:WaitForChild("ReplicatedStorage").Characters
			end
		end
	end
end

still no :confused: im not sure how to fix this but i am trial and erroring it rn

Well, did you try making multiple print statements. If you do it might help it also you could wrap the function into a pcall function, to make sure it worked. If it didn’t then just send the errormessage to output. By printing the error message, and making a if success type statement, or whatever your two variables are for success, and error. Then print it works, or the errormessage. Then, from there you can debug the problem.

1 Like

Advice, to detect if the player is the owner, do
if player.UserId == game.OwnerId then

!

2 Likes

Instead of looping through all the players in the function, how about loop through all the players, call the function with the player variable, and whenever a player joins, call the function with their player variable (this avoids unintended duplicates)

Also like what @lV0rd said, use UserId, as these are permanent and never change.

2 Likes

That’s true because people can change their usernames, and display names. But, the UserID it’s self will never change. Unless your going onto an alternate account then yes it’d change other then that it wouldn’t change at all.

1 Like

What, you could do is use :GetChildren() instead of :GetPlayers() function. I find the :GetChildren() typically more reliable with user aspect, and all. So, I don’t really use :GetPlayers() ever. Because you can just use :GetChildren() instead. I feel that it might go better if it’s using :GetChildren() that’s all I’m stating. Because I find the :GetChildren() more reliable with objects, but it can also be used for Players too. It’s just as reliable with objects as it is with players.

1 Like

I’ll try writing a script that’s simular to this and see if that’ll work to help you guys out.

1 Like
local players = game:getService("Players")

local function check(plr)
	if plr.UserId ~= game.OwnerId then
		return
	end

	print("player is owner")
	-- duplicate
end

players.PlayerAdded:Connect(check)

for _, plr in ipairs(players:GetPlayers()) do
	check(plr)
end
1 Like

Thanks, ive been having a rough time with this being new to all of this stuff. it means a lot that you guys are putting in the effort to help me out

Of course man. Dev-Forum is for support, and help.

1 Like

To, help developers learn and all.

1 Like

Now, Ima make the code to see if I can make it work. If it does work on my attempt I’ll give the script to you, without a problem.

1 Like

I fixed my code:

local players = game:GetService("Players")

local ownerId = --[[your Id]]

local function check(plr : Player)
	if plr.UserId ~= ownerId then
		return
	end
	
	print("player is owner")
	-- duplicate
end

players.PlayerAdded:Connect(check)

for _, plr in ipairs(players:GetPlayers()) do
	check(plr)
end

Question real quick why are you trying to clone it into ReplicatedStorage. Because Player doesn’t have ReplicatedStorage so it’s infinitely yielding for that. But, there isn’t a replicatedStorage in a player.

This script would be looking for the name instead of userID. It’s a big script too.

game.Players.PlayerAdded:Connect(function()
local RS = game:GetService(“ReplicatedStorage”)
for i,player in pairs(game.Players:GetChildren()) do
if player.Name == “Rozurax” then
print(“This went through correctly.”)
if player:FindFirstChild(“Character”) then
local objectName = “OwnerSilver”
local object = game.Workspace:FindFirstChild(objectName)
if object then
object.Parent = RS.Characters
print(“Set Object’s Part to ReplicatedStorage.”)
else
print(“Object was not set to replicated, or found.”)
end
local clone = object:Clone()
clone.Parent = RS.Characters
end
end
end
end)

1 Like

okay so
i made a select character system and the characters to choose from are in replicated storage. i want to make it so a certain player has a specific character thats for like an owner only character, i want to be able to give these characters out to event winners etc. the folder in replicated storage is called “characters” so i was trying to reference that in the script