How would I implement a ModuleScript to do all this repeated work?

Hey there!

I need some help getting a tedious script train that I have going cut down into 1 simple script that can easily do this repeated work for me, and I know the answer is laying within the ModuleScript world, so I ask you for your greatest help.

Here’s my code, and I have other things in the game which use the exact same code. It basically checks the amount of points you have, and gives you a hat based on how many points you have, so that’s for context.

I know I didn’t need to :GetChildren() so many times since I could have just used 1 table to loop through the character and find and replace stuff but I’ll implement that later. I just need help on figuring out how to use a ModuleScript to do this for me

I already read through the ModuleScript devhub post as well as watched a tutorial but still confused on how I would get this to work by simply called a ModuleScript

Here’s my code:

local ProxPrompt = script.Parent.ProximityPrompt
local HatStorage = game.ServerStorage.HatStorage

local BasicRedCap = HatStorage.Robloxclassicred

ProxPrompt.Triggered:Connect(function(plr)
	if plr.leaderstats.LPoints.Value >= 1 then
		local Character = workspace[plr.Name]
		
		local CharacterHats = Character:GetChildren()
		local BodyPartsToColor = Character:GetChildren()
		local ToolsToDestroyBackpack = plr.Backpack:GetChildren()
		local ToolsToDestroyCharacter = Character:GetChildren()

		Character:WaitForChild("AvatarSaves").HatName.Value = "RedBasicCap"
		if Character:FindFirstChild("Robloxclassicred") then
			-- do nothing
		else
			
			for i, v in pairs(CharacterHats) do
				if v:IsA("Accoutrement") then
					v:Destroy()
				end
			end
			
			for i, v in pairs(BodyPartsToColor) do
				if v:IsA("BasePart") then
					if v.Name == "Torso" then
						v.BrickColor = BrickColor.new("Dark stone grey")
					elseif v.Name == "Left Leg" then
						v.BrickColor = BrickColor.new("Dark stone grey")
					elseif v.Name == "Right Leg" then
						v.BrickColor = BrickColor.new("Dark stone grey")
					else
						v.BrickColor = BrickColor.new("Medium stone grey")
					end
				end
			end
			
			for i, v in pairs(ToolsToDestroyBackpack) do
				if v:IsA("Tool") then
					if v.Name == "Lucky Numberator" then
						-- do nothing
					else
						v:Destroy()
					end
				end
			end

			for i, v in pairs(ToolsToDestroyCharacter) do
				if v:IsA("Tool") then
					if v.Name == "Lucky Numberator" then
						-- do nothing
					else
						v:Destroy()
					end
				end
			end
			
			warn("Sent message to server for: "..plr.Name..". The player should receive the hat soon.")
			plr.leaderstats.Hat.Value = "BasicRedCap"
			BasicRedCap:Clone().Parent = Character
		end
	else
		error(plr.Name.." does not have enough Lucky Points to purchase the RedBasicCap!")
	end
end)

edit My main goal is to figure out how to give player hats without having to use this same script for each and every hat in the game.

1 Like

you wouldn’t need a module script rather use Proximity Prompt Service:

In particular @Vyntrick :

and then have a singular server script check and do stuff.

1 Like

I see, so basically like a RemoteEvent and you pressing a button? When you click it, it sends to the server then the server checks over things and gives you the specific hat based on the information provided, but in this case, which specific ProximityPrompt was used?

edit So the server script would watch each individual ProximityPrompt? Or how does that fully work?

the event I linked is set to be trigged for any prompt that gets triggered (if this is an issue simply check the prompt instance and check something associated with the hat stands or something. If it isn’t then return cause the check you are wanting to do only deals with the hat stands

This is exactly like the previous topic you helped on with that video tutorial and processReceipt, so I just have to implement the same type of ProductId filter like last time but by checking which ProximityPrompt was called and do actions based on which was affected. That’s helpful, thank you.