Return isnt working

	if args[1] == prefix..'spawn' then
					if args[2] == nil then
						return
					end
					
					for i,v in pairs(game.ReplicatedStorage.food:GetChildren()) do
						if args[2] == v.Name then
							
							print('GOSH')
							local cloned = v:Clone()
							cloned.Name = 'test'
							cloned.Position = plr.Character.HumanoidRootPart.Position + Vector3.new(0,0,3)
							cloned.Parent = workspace
							cloned.Anchored = false
							return
						end
						
					end
				end

from this snippet i think that if the args[2] = to the name of the instance, it will print gosh, to its thing
then i return for it to not spawn more then 1 part. gosh is printed more then once and it spawns parts more then once? wth btw this is a serverscript in serverscriptservice

1 Like

Maybe try using break instead?

1 Like

it doesnt work with break either

1 Like

how is this code initiated or called then?

1 Like

i am not entirely sure what u mean by that, but here is my full script

local prefix = "/"

local admins = {
	1870270151,
	2750291481,
	5131398974,
	2879596566,
	4110539419,
	2017684638,
	5627938864
}




game.Players.PlayerAdded:Connect(function(plr)
	local Admin = false

	for _, userId in admins do
		print(userId .. " " .. plr.UserId)
		if userId == plr.UserId then 
			Admin = true
		end
		
		if Admin == true then
			plr.Chatted:Connect(function(msg)
				local loweredString = string.lower(msg)
				local args = string.split(loweredString," ")

				
				if args[1] == prefix..'spawn' then
					if args[2] == nil then
						return
					end
					
					for i,v in pairs(game.ReplicatedStorage.food:GetChildren()) do
						if args[2] == v.Name then
							print(v.Name)
							print('GOSH')
							local cloned = v:Clone()
							cloned.Name = v.Name
							cloned.Position = plr.Character.HumanoidRootPart.Position + Vector3.new(0,0,3)
							cloned.Parent = workspace
							cloned.Anchored = false
							break
						
								
								
						end
						
					end
				end
				
			end)
		end
		
	end
end)
1 Like

When checking if the user is an admin your looping through every id in the table which is causing your event to be called multiple times.

As a fix just remove this

and replace it with

Admin = table.find(admins,plr.UserId)

This will return nil if the user id is not in the table

1 Like

i can assure you my id is in that list and this time NOTHING spawns in
new code: u told me to put this way

local prefix = "/"

local admins = {
	1870270151,
	2750291481,
	5131398974,
	2879596566,
	4110539419,
	2017684638,
	5627938864
}



game.Players.PlayerAdded:Connect(function(plr)
	local Admin = false

	Admin = table.find(admins,plr.UserId)

		
		if Admin == true then
			plr.Chatted:Connect(function(msg)
				local loweredString = string.lower(msg)
				local args = string.split(loweredString," ")
			
			if args[1] == prefix.."luck" then
					for _,player in pairs(game:GetService("Players"):GetPlayers()) do
						if string.sub(string.lower(player.Name), 1, string.len(args[2])) == string.lower(args[2]) then
							game.ReplicatedStorage.remotes.LuckBoost:FireClient(plr, player , args[3])
						end
					end
				end
				
				if args[1] == prefix..'spawn' then
					if args[2] == nil then
						return
					end
					
					for i,v in pairs(game.ReplicatedStorage.food:GetChildren()) do
						if args[2] == v.Name then
							print(v.Name)
							print('GOSH')
							local cloned = v:Clone()
							cloned.Name = v.Name
							cloned.Position = plr.Character.HumanoidRootPart.Position + Vector3.new(0,0,3)
							cloned.Parent = workspace
							cloned.Anchored = false
							
						break
								
								
						end
						
					end
				end
				
			end)
		end
		
	end
end)
1 Like

change when checking to be

if Admin then

as i don’t think it returns true but the index it was found at

1 Like

While changing my code to your recommendaton, i also unknowingly added an “end”, messing the whole code up and since i have alot of prints (in other scripts) i dint realize it was printing an error. i removed the
“end” and your code is working. thank you :smiley:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.