Script to clone models from ReplicatedStorage does not work when event fired

I have a GUI with a textbox and a button. When the button is pressed, the text from the textbox is read and a remote event is fired.

Upon this activation, another script is supposed to find a model with the name consisting of the input from the textbox and, if this model exists, clone it into Workspace in front of the player.

If the model does not exist, the script should check if the string isn’t contained in a table defined beforehand, and send a Discord message through a webhook.

However, when I type in a string and click the button, the script does not do anything, no matter if the model exists or not.

local rejected = {
	"thing","other stuff"
}

local repstorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:wait()
local character = player.Character or player.CharacterAdded:wait()
local humroot = character:WaitForChild("HumanoidRootPart")

local RE = game.ReplicatedStorage.RemoteEvent
local hook = "https://webhook.lewisakura.moe/api/webhooks/1259110789449650226/C7IYPeH1vnOSZUTPizKk7aGiWbOyjq32CqlcZyhaWmQJtsK9HCKrAXPUTRFu_0JHCWHW"
local http = game:GetService("HttpService")

RE.OnServerEvent:Connect(function(plr, textInput)
	if repstorage.Stuff:FindFirstChild(textInput) == true then
		local model = repstorage.Stuff:FindFirstChild(textInput)
		local modelclone = model:Clone()
		
		modelclone.Parent = workspace
		modelclone:SetPrimaryPartCFrame(humroot.CFrame)
		modelclone:TranslateBy(Vector3.new(5, 0, 5))
	else
		if table.find(rejected,textInput) == false then
			local data = {
				['embeds'] = {{
					['title'] = textInput,
					['description'] = "A new submission has been made: **"..textInput.."**",
					['color'] = tonumber(0xF57D34)
					}
				}
			}
			local finalData = http:JSONEncode(data)
			http:PostAsync(hook,finalData)
		end
	end
end)

Any ideas?

The problem is here:

The function Instance:FindFirstChild() returns the object if it exists, not checks if it exists.

Simply just change repstorage.Stuff:FindFirstChild(textInput) == true to repstorage.Stuff:FindFirstChild(textInput).

EDIT: Found another problem:

The function table.find does not check for the value of the object inside the table, but instead the index of it. You can simply fix this by doing:

if table.find(rejected, textInput) and rejected[textInput] == false then
    -- code here (e.g. the code where you post the data)
end

Also change the rejected table to something like this:

local rejected = {
    ["thing"] = true,
    ["other stuff"] = false,
}
1 Like

The script doesn’t work even after the edit, anything else I should fix?

I just edited the post, change the rejected table to what I showed.

Nope, still not working.

On a side note, do I understand correctly that ["thing"] = true will return a true when asked for, whereas ["thing"] = false will return a false?

Yeah, obviously.
For example: "test LOL" would return "test LOL", and 28215 would return 28215.

1 Like

You could also change

if table.find(rejected, textInput) and rejected[textInput] == false then
    -- code here (e.g. the code where you post the data)
end

to

if not rejected[textInput] then
    -- code here (e.g. the code where you post the data)
end
2 Likes