Here's what I tried:
if true do
hum.OnTouch
material.switch.PartMaterial = random
end)
Here's what I tried:
if true do
hum.OnTouch
material.switch.PartMaterial = random
end)
###Since no one has bothered to explain what is happening and simply are just giving away the code, I will be doing the honours.
###Event.
An Event is a type of class in Roblox that is fired when something invokes it. Touched Event is a Roblox Script Signalwhich fires when an Object comes in contact with either Humanoid, or another Object. Since it is an Event, you will be using it alongside function.
Example: Touched, Changed etc.
function is a chunk of abstract code which is utilized to execute the code for numerous amount of times. functions are also typically called Procedure. We use functions to minimize the amount of repetitive codes.
Example:
-- [Processing Section]
local function Print ()
print("Bye");
end;
To invoke the function, you simply call it.
-- [Processing Section]
local function Print ()
print("Bye");
end;
-- [Output Section]
Print();
Since we want to change the Part's Material when we touch it, we have to connect the Touched Event with a function.
-- [Processing Section]
local function ChangePart (hit)
Part.Material = Enum.Material.Plastic;
end;
-- [Output Section]
Part.Touched:Connect(ChangePart);
Now, a new problem arises. When you use the above script and simply press play, you will notice that the Part's Material already changes before we even touch it. Well, I did tell you the definition of Touched Event above and you should be able to guess. The Part's Material changed because it touched another Object. In this particular case, it was Baseplate itself.
To counter that, we will have to check for a special property of Players's called Humanoid. A Humanoid is a special object that gives life to a Character.
-- [Processing Section]
local function ChangePart (hit)
if hit.Parent:FindFirstChild("Humanoid") then
Part.Material = Enum.Material.Plastic;
end;
end;
-- [Output Section]
Part.Touched:Connect(ChangePart);
Now this above code works because we are checking if the hit actually has a Humanoid. hit in this case is anything that touches the Part. Baseplate touches the Part but, it does not have a special property that only Character's possess and that is Humanoid.
Hope you learned something from this. I suggest you take a look into wiki before asking questions.
Useful Resources
That script is very far off. Sorry. Here is the correct script.
function ChangeMaterial(h) -- Defines function with args "h". H will be the part that touched it.
if h.Parent:FindFirstChildOfClass("Humanoid") then -- Checks if a player touched it
part.Material = "Neon" -- Switches part material to Neon. (Random material is possible but a bit too complicated to show to a new-comer
end
end
part.Touched:Connect(ChangeMaterial) -- Connects the touch event to the funtion
In the future, please make an effort to actually learn the api before asking for help.
If my answer solved your issue, please remember to mark it as correct!
In line 3, you shouldn’t be using Strings to assign Enum values. Instead use Enum.Material.Neon
Touched functions work with the Touched event. You would also have to set up a table with all the materials.
local materials = {Enum.Material.Brick,Enum.Material.Marble,Enum.Material.Brick} --add the amount of materials you want.
local part = script.Parent --a defined variable, which is the part you want to change the material
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then --checks if the part collides with a player
local choosematerial = materials[math.random(#materials)] --chooses a random material out of the material table
part.Material = choosematerial
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.
6 comments
...This is your whole script?
yeah i dont know lua it looked compatible ¯_(?)_/¯
Oh man. Please do attempt to study the wiki before creating a random script and ask for help.
guys its too late i used a freemodel
Then you better mark my answer as answered. I even took the time to explain it. Plus, it will help your negative reputation a little bit.
@kingofpronoobs Yea, you need to mark @Zafirua's answer as correct. He answered very well.