Shortening Script

Hello I was wondering if I could get help with making this following script more simpler and shorter:

local function jobSpawns(jobLocation)

	local fVar = ""
	local Data = {

		["1"] = spawnLocations.Manager,
		["2"] = spawnLocations["Assistant Manager"],
		["3"] = spawnLocations.Supervisor,
		["4"] = spawnLocations.Chef,
		["5"] = spawnLocations.Janitor,
		["6"] = spawnLocations.Cashier,

	}

	if jobLocation == Teams["Manager"] then
		fVar = Data["1"]
	end

	if jobLocation == Teams["Assistant Manager"] then
		fVar = Data["2"]
	end

	if jobLocation == Teams["Supervisor"] then
		fVar = Data["3"]
	end

	if jobLocation == Teams["Chef"] then
		fVar = Data["4"]
	end

	if jobLocation == Teams["Janitor"] then
		fVar = Data["5"]
	end

	if jobLocation == Teams["Cashier"] then
		fVar = Data["6"]
	end
	return (fVar)
end
local data = {
    ["Manager"] = spawnLocations.Manager,
    ["Assistant Manager"] = spawnLocations["Assistant Manager"]
}

fVar = Data[job] or nil

That way, you’re accessing the data dictionary directly, without having to invoke nested if-else statements.

Hope this helps.

2 Likes
local function jobSpawns(jobLocation) 

	local fVar = ""
	local Data = {

		["Manager"] = spawnLocations.Manager,
		["Assistant Manager"] = spawnLocations["Assistant Manager"],
		["Supervisor"] = spawnLocations.Supervisor,
		["Chef"] = spawnLocations.Chef,
		["Janitor"] = spawnLocations.Janitor,
		["Cashier"] = spawnLocations.Cashier,

	}
	
	fVar = Data[jobLocation] or nil
	return fVar
end

Is this correct? The script works but I’m not being placed in the correct spawn location.

Does Teams refer to the Teams service?

If so, you could simply compare the player’s team to the keys in data. Ergo:

fVar = data[plyr.Team] or nil
1 Like
	fVar = Data[jobLocation] or nil

jobLocation in my module script is reffered to as self.Job which is the team of the player

What does fVar output if you print it, using my code?

It prints out “nil” when I run the script

Then, I imagine that the value of jobLocation doesn’t exist as a key in the dictionary.

Try printing the value of jobLocation when passed to the function.

It prints out the Job button that I chose but fVar comes out as nil

Ah, so it’s an instance, or GUI button, that’s being passed to the function?

In which case:

fVar = data[jobLocation.Name] or nil
1 Like

I added .Name and fvar is now printing out the my team/the button I chose but I’m not being spawned in the correct location still

The above works under the assumption that the button is named with the corresponding team.

Is your Manager button named Manager in the explorer?

Yes it is
[charatcer limits, dgfidbguis]

I would just do this.

local function jobSpawns(jobLocation)
	local Data = {
		Teams["Manager"] = spawnLocations.Manager,
		Teams["Assistant Manager"] = spawnLocations["Assistant Manager"],
		Teams["Supervisor"] = spawnLocations.Supervisor,
		Teams["Chef"] = spawnLocations.Chef,
		Teams["Janitor"] = spawnLocations.Janitor,
		Teams["Cashier"] = spawnLocations.Cashier,
	}
	return Data[jobLocation]
end

Or alternatively

local function jobSpawns(jobLocation)
	return spawnLocations:FindFirstchild(jobLocation.Name)
end
2 Likes