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

How to create event where when a player steps on a part, the part's transparency turns to 1?

Asked by 3 years ago
Edited 3 years ago

Hello I am trying to make a command where when a part is stepped on by the player, it disappears. I'm working on a tycoon game. My current code makes it so the brick disappears before I even touch it. Also I want to make it into a function as I will be using multiple parts and make it so the transparency changes to 1 when the player has an X amount of money and touches the part.

function ChangeTransparency(part, newTransparency)
    part.Transparency = newTransparency
    print "Worked"
end

workspace.ButtonTest.Touched:Connect (ChangeTransparency(game.Workspace.ButtonTest, 1))

0
Errors: 1. You forgot to connect your code with an RBXScriptSignal of a player (hit.Parent). 2. Part isn't defined. 3. Line 6 has an invalid argument etc. JesseSong 3916 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

There's a bit of a problem with your code, first off, you are running ChangeTransparency instead of connecting it, you are running the function and then connecting nothing (nil) to the event, another thing, you forgot to check if the touched object is a player (or if you want, a rig), to do this, you can check if a humanoid exists in the hit part parent, because the hit parameter only gives you a part, for example, if you touch a part with your left arm, hit will be your left arm and hit.Parent will be the character, if you want only player touching to make it transparent, you need to add another step, you can use GetPlayerFromCharacter to get the player or check if the touched character is a player, also you don't need a change transparency function lol

local players = game:GetService("Players") -- Useful for later
local part = workspace.ButtonTest -- the part that will change transparency, this is only for if you want more than one button, so you don't have to change the whole code to make another button
local transparencyToSetTo = 1 -- The transparency to set the part to on touch (optional)

part.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid") -- We use FindFirstChild instead of a period (.) or a WaitForChild is because we don't know if hit.Parent has a humanoid, for example, if a part in the workspace touches the button, hit.Parent will be workspace and there will be no humanoid and the script will error and break
    if (humanoid) then
        -- if a character
        -- Just set the transparency here if you want, but if you don't want dummys or npcs to activate the button, put the next few lines
        -- part.Transparency = transparencyToSetTo
        local player = players:GetPlayerFrom(hit.Parent) -- this is to check if the character that touched the part has a player assigned to it
        if (not player) then
            -- If the character has no player, for example, npc or dummy
            return -- Don't let it go past this point, that's what the return is for
        end
        part.Transparency = transparencyToSetTo
    end
end)

0
Thank you so much!! GiantAgeman 0 — 3y
0
accept the answer if it helps :D AnasBahauddin1978 715 — 3y
Ad

Answer this question