Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a script that makes your name the value of a stringvalue but only if none already are?

I tried the elseif method but it didn't work

	local plr = game.Players.LocalPlayer 
local char = plr.Character or workspace.ChildAdded:Wait() 
local endingpos = Vector3.new(-18.47, 0.5, 367.63) 
local Jobslot1 = game.Workspace.Jobs.SalesmanJob.JobSlot1
local Jobslot2 = game.Workspace.Jobs.SalesmanJob.JobSlot2
local Jobslot3 = game.Workspace.Jobs.SalesmanJob.JobSlot3
local Jobslot4 = game.Workspace.Jobs.SalesmanJob.JobSlot4
local Jobslot5 = game.Workspace.Jobs.SalesmanJob.JobSlot5
local Jobslot6 = game.Workspace.Jobs.SalesmanJob.JobSlot6
local Jobslot7 = game.Workspace.Jobs.SalesmanJob.JobSlot7
local Jobslot8 = game.Workspace.Jobs.SalesmanJob.JobSlot8
local Jobslot9 = game.Workspace.Jobs.SalesmanJob.JobSlot9
local Jobslot10 = game.Workspace.Jobs.SalesmanJob.JobSlot10
local Players = game:GetService("Players")

function employed() 
	if Jobslot1.Value == nil then
	plr.Name = Jobslot1.Value
	elseif Jobslot2.Value == nil then
		plr.Name = Jobslot2.Value
	elseif Jobslot3.Value == nil then
		plr.Name = Jobslot3.Value
	elseif Jobslot4.Value == nil then
		plr.Name = Jobslot4.Value
	elseif Jobslot5.Value == nil then
		plr.Name = Jobslot5.Value
	elseif Jobslot6.Value == nil then
		plr.Name = Jobslot6.Value
	elseif Jobslot7.Value == nil then
		plr.Name = Jobslot7.Value
	elseif Jobslot8.Value == nil then
		plr.Name = Jobslot8.Value
	elseif Jobslot9.Value == nil then
		plr.Name = Jobslot9.Value
	elseif Jobslot10.Value == nil then
		plr.Name = Jobslot10.Value
					
    char:MoveTo(endingpos) 

end
		end
script.Parent.MouseButton1Click:Connect(employed)

1 answer

I'm not entirely sure what you're doing, but I can optimize your code using a for loop to make it more readable.

local plr = game.Players.LocalPlayer 
local char = plr.Character or workspace.ChildAdded:Wait() 
local endingpos = Vector3.new(-18.47, 0.5, 367.63)
local Players = game:GetService("Players")

function employed()
	for i = 1, 10 do
		local jobSlot = game.Workspace.Jobs.SalesmanJob:FindFirstChild("JobSlot" .. i)
		if (jobSlot and jobSlot.Value == "") then
			jobSlot.Value = plr.Name
			break;
		end
	end
	
	char:MoveTo(endingpos)
end

script.Parent.MouseButton1Click:Connect(employed)

I also noticed in your original script you did "plr.Name = JobslotN.Value" which is a simple mistake. It really should be "JobslotN.Value = plr.Name" to set the job slot's value.

You also tried to check if a stringvalues value is nil, which will never happen. The default value for a string object is just an empty string "".

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