Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

5 comments

0

and yes, the part is the parent of the script

0

Local Scripts don't function in workspace.

0

its a normal script

0

Normal scripts can't have local properties such as a LocalPlayer.

0

^

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.