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 7 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?

01Mouse = game.Players.LocalPlayer:GetMouse()
02 
03local Part = script.Parent
04 
05Part.BrickColor = BrickColor.new("Medium stone grey")
06 
07Mouse.KeyDown:connect(function(key)
08    if Part.Touched and key == "e" then
09        Part.BrickColor = BrickColor.new("Black")
10    end
11end)
0
and yes, the part is the parent of the script fabbekabbe 0 — 7y
0
Local Scripts don't function in workspace. KamikazeShard 23 — 7y
0
its a normal script fabbekabbe 0 — 7y
0
Normal scripts can't have local properties such as a LocalPlayer. Whoami102 21 — 7y
0
^ EtherealTrin 45 — 7y

1 answer

Log in to vote
0
Answered by 7 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:

01game.Players.PlayerAdded:connect(function(player)
02    local Mouse = player:GetMouse()
03 
04    local Part = script.Parent
05 
06    Part.BrickColor = BrickColor.new("Medium stone grey")
07 
08    Mouse.KeyDown:connect(function(key)
09        if Part.Touched and key == "e" then
10            Part.BrickColor = BrickColor.new("Black")
11        end
12    end)
13end)
Ad

Answer this question