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.