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

How to change part properties once button is pressed locally?

Asked by 4 years ago
Edited 4 years ago

Hey ya'll!

I've created a sort of 'button' system. When a humanoid connects with a part, a different part has it's properties changed and a sound plays.

This works as expected, except when the button is pressed all players hear the sound/have the variable changed.

Here is my script atm:

local played = false

    script.Parent.Touched:Connect(function(h)       
        if played == false then
            script.Parent.SuccessSound:Play()
            played = true
            wait(2)
            played = false
        end
    end)

script.Parent.Touched:Connect(function(h)
    local hum = h.Parent:FindFirstChild("Humanoid")
    if hum then
        script.Parent.Parent.ButtonPlatform.Transparency = 0

script.Parent.Touched:Connect(function(h)
    local hum = h.Parent:FindFirstChild("Humanoid")
    if hum then
        script.Parent.Parent.ButtonPlatform.CanCollide = true



end end) end end)

All of this script works completely fine and reliably but I'm still struggling to figure out how to make sure that only the person who hits the button sees and hears the changes.

Thanks!

1 answer

Log in to vote
1
Answered by 4 years ago

In order to change the properties of a part for a single person, you can use a remote event to change the property from a local script. Insert a remote event into ReplicatedStorage and name it "PropertyChange". Once this is done we can start working on the script. Your ServerScript is mostly set up how it needs to be but we can copy and paste the parts that change the properties to a notepad or somewhere else accessible so you can copy it. Your new script should look like this:

local played = false

script.Parent.Touched:Connect(function(h)
    local hum = h.Parent:FindFirstChild("Humanoid")
    if hum then
        game.ReplicatedStorage.PropertyChange:FireClient(game.Players[h.Parent.Name])

--I also took away two of the touched events, as you can just change both properties and play the sound in the same one.

    end
end)

Now in order for that script to work successfully we need a LocalScript in the StarterPlayerScripts folder (also move your audio here). The contents of this script will read as follows:

game.ReplicatedStorage.PropertyChange.OnClientEvent:Connect(function()
    game.Players.LocalPlayer.PlayerScripts.--[[reference your audio]]:Play()
    --[[reference the part you're changing]].Transparency = 0
    --[[reference the part you're changing]].CanCollide = true
end)

This should work but I don't have access to studio currently so comment any errors if they should arise. Hope I helped you out.

0
That worked perfectly!! TYSM! ittyisaduck 21 — 4y
Ad

Answer this question