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

Something is wrong with my script, and I don't know what. Could someone take a look, please?

Asked by 6 years ago

I want the Part to change color to black when I touch it, and pressing E at the same time. But it doesn't work, why?

Mouse = game.Players.LocalPlayer:GetMouse()

local Part = script.Parent

Part.BrickColor = BrickColor.new("Medium stone grey")

Mouse.KeyDown:connect(function(key)
    if Part.Touched and key == "e" then
        Part.BrickColor = BrickColor.new("Black")
    end
end)
0
and yes, the part is the parent of the script fabbekabbe 0 — 6y
0
Local Scripts don't function in workspace. KamikazeShard 23 — 6y
0
its a normal script fabbekabbe 0 — 6y
0
Normal scripts can't have local properties such as a LocalPlayer. Whoami102 21 — 6y
0
^ EtherealTrin 45 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

If it is a normal script, like everyone else was saying, then you can't use LocalPlayer.

Instead, we have to check when a player was added to the game, and take player as a parameter. Also, I recommend changing "key == "e"" to string.lower(key) == "e" as if a player is holding Shift while pressing E, it will not work.

i.e:

game.Players.PlayerAdded:connect(function(player)
    local Mouse = player:GetMouse()

    local Part = script.Parent

    Part.BrickColor = BrickColor.new("Medium stone grey")

    Mouse.KeyDown:connect(function(key)
        if Part.Touched and key == "e" then
            Part.BrickColor = BrickColor.new("Black")
        end
    end)
end)
Ad

Answer this question