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

Tycoon Question: "Hit" Function and Usage?

Asked by 6 years ago

I'm very new at scripting and game-building in general, and I have a question I hope someone can answer about a tycoon element I'm using. Being the noobie I am, I decided to use a tycoon kit to figure out what the heck is going on in the code, but when I tried to manipulate the properties of bricks when they went through an upgrader, nothing worked. Here is the original source code:

script.Parent.Upgrade.Touched:connect(function(hit) if hit:FindFirstChild("Cash") then hit.Cash.Value = hit.Cash.Value * 2 hit.Reflectance = hit.Reflectance + 0.1 hit.Material = [ right here ] end end)

Keep in mind that the Reflectance and Cash value manipulations both worked.

It's probably some small error I've made, but I used multiple variations trying to change the material of the brick. Here are variations I've used:

hit.Material = Cobblestone / cobblestone [neither worked]

hit.Material = hit.Cobblestone / cobblestone

hit.Material = Material.Cobblestone / cobblestone [you get the idea.]

Please bear in mind that I'm entirely new to this. I won't be writing algorithms anytime soon. XD

Thanks, Puredew

2 answers

Log in to vote
0
Answered by 6 years ago

Either of these will work

-- Using a string
hit.Material = "Cobblestone"
-- Using an Enum
hit.Material = Enum.Material.Cobblestone
Ad
Log in to vote
0
Answered by 6 years ago

Hi Puredew,

Make sure that you understand that Hit is referring to an object in Roblox space that is acted upon when touching "Upgrade". So essentially, Hit can be anything. That's why when I usually setup Touched events, I always check if Hit is only certain type of object in Roblox by using IsA() method. You don't have to do this for the code to work, but I just wanted to let you know.

So, here is the code that I tested just now, and it works.

part = script.Parent


part.Touched:Connect(function(hit)
    if hit:IsA("Part") then
        if hit:FindFirstChild("Cash") then
            hit.Material = Enum.Material.Cobblestone
        end
    end
end)

Please please please! Note that Material accepts ENUM. Do not use a String value! You can learn more about Enum (Enumeration) here: http://wiki.roblox.com/index.php?title=Enumeration

Please let me know if you have other questions. Thanks.

Answer this question