Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I go about combining multiple scripts into a sigular one?

Asked by 4 years ago
Edited 4 years ago

Hi, I have this very basic "face changer" script that you place inside of a block with a decal on it, and when you touch the decal, your face copies the decal onto it. Now I have several of these decal blocks lined up with a script in each one and wanted to create a overarching script that could substitute each script for a single one. How would I go about doing this? I would want the player to touch one of the decals and for that decal to appear on their face. There would be serval decal blocks all next to each other.

Thank you in advance! Any feedback is great!

This is the script/code for one block. I was thinking of maybe giving each block a numbervalue?

// My workspace looks like this:
//Model
//-FaceChanger
//  -Decal
//-FaceChanger
//  -Decal


//Maybe I do like 

//Model
//-FaceChanger1
//  -Decal
//-FaceChanger2
//  -Decal

//If I did that, I would need to change function getDecal() but this is where I get lost

function onTouch(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        local head = hit.Parent:FindFirstChild("Head")
        if head ~= nil then
            local face = head:FindFirstChild("face")
            if face.Texture ~= getDecal().Texture then
                face.Texture = getDecal().Texture
            end
        end
    end
end

function getDecal()
    local decals
    decals = {}
    for i,child in pairs(script.Parent:GetChildren()) do
        if child.className == "Decal" then
            table.insert(decals, child)
        end
    end
    if #decals ~= 0 then
        return decals[math.random(1, #decals)]
    end
    return nil
end
script.Parent.Touched:Connect(onTouch)

2 answers

Log in to vote
0
Answered by
0msh 333 Moderation Voter
4 years ago

you can have a script inside of ServerScriptService and do something like this:

never tested it, may have some errors

local decals = game.Workspace:GetDescendants()
for i,v in pairs (decals) do
if v:IsA("Decal") then -- check if the descendant is a decal
    if v.Parent.Name == "FaceChanger" then -- the decal's parent's name
        v.Touched:Connect(function(hit)
            -- clones the texture to the hit.Parent's face, make sure to check if their humanoid is present first
        end)
    end
end
end
0
Thank you, and I love the idea of putting it in the ServerScriptService although it didn't work. I appreciate it tho! 2sail2you 4 — 4y
0
why didnt it work? royaltoe 5144 — 4y
0
Take a look at what I wrote in the comments below, but basically nothing happens if I touch the part with a decal in it. Not sure what I should do 2sail2you 4 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

This is what I now have in the serverscriptservice


function getDecal() local decals = game.Workspace:GetDescendants() for i,v in pairs (decals) do if v:IsA("Decal") then -- check if the descendant is a decal if v.Parent.Name == "FaceChanger" then -- the decal's parent's name v.Touched:Connect(function(hit) -- clones the texture to the hit.Parent's face, make sure to check if their humanoid is present first end) end end end end function onTouch(hit) if hit.Parent:FindFirstChild("Humanoid") ~= nil then local head = hit.Parent:FindFirstChild("Head") if head ~= nil then local face = head:FindFirstChild("face") if face.Texture ~= getDecal().Texture then face.Texture = getDecal().Texture end end end end script.Parent.Touched:Connect(onTouch)
0
And that works? moo1210 587 — 4y
0
No unfortunately not :/ 2sail2you 4 — 4y
0
dude, you need to put the on touch function below v.Touched, you connected the function to script.Parent which would be ServerScriptService 0msh 333 — 4y
0
It is below? 2sail2you 4 — 4y
0
no it's not... you're not connecting the right functions, i suggest you go watch some tutorials on the basics first 0msh 333 — 4y

Answer this question