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

How do I make my character invisible using a key press event?

I tried to make the character invisible by pressing Q but all that did was make the character invisible to me and not to other players. I know it has to do with using game.Players.LocalPlayer, and I really don't know how to make this key press function turn your character invisible to everyone. It may have to do with my script being a LocalScript, but I don't know any other way. Here is my script:

local m = game.Players.LocalPlayer:GetMouse()
local plr = game.Players.LocalPlayer.Character

m.KeyDown:Connect(function(k)
    if k == "q" then
        if db == true then
			for i, Child in pairs(plr:GetChildren()) do
				if Child:IsA('Part') or Child:IsA('MeshPart') or Child:IsA('Accessory') then
					Child.Transparency = 0.5
				end
			end
			plr.Head.face.Transparency = 0.5
			plr.HumanoidRootPart.Transparency = 1
			wait(10)
			for i, Child in pairs(plr:GetChildren()) do
			if Child:IsA('Part') or Child:IsA('MeshPart') or Child:IsA('Accessory') then
				Child.Transparency = 0
			end
			end
			plr.Head.face.Transparency = 0
			plr.HumanoidRootPart.Transparency = 1
	end
end
end)

Yes yes I know I am using KeyDown instead of UserInputService, and I am doing that because I am using the Q key and not any fancy keys.

1 answer

✓ Accepted answer

Not sure what 'fancy keys' you speak of, so I'll take it you just don't know how to use UserInputService. Nothing is wrong with that and you shouldn't excuse trying to learn something new. Just know the KeyDown from :GetMouse() is deprecated and better methods are available with more functionality.

You 'listen' for player keys using the InputBegan event from UserInputService. Inside the event function, you check the event's first parameter, InputObject, and it's KeyCode.

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(inputObject, gameProcessed)
	if not gameProcessed then return end

	if inputObject.KeyCode == Enum.KeyCode.Q then
		--code
	end
end)

Just by reading the code above you will know that anything in the if-check would run if the key that is pressed is "Q". There is also the parameter check for gameProcessed. This is here to prevent code from running if the user is interacting with a GUI or chat. More info of gameProcessed parameter here.

The last thing you need to do is use a RemoteEvent to have the server handle the character turn invisible. If you keep this on the client it wouldn't replicate to other players.

What you will need is a RemoteEvent in ReplicatedStorage and a Script in ServerScriptService.

--server script

local RS = game:GetService("ReplicatedStorage")
local remoteEvent = RS.RemoteEvent

--Connect to event. Player is always passed through :FireServer().
remoteEvent.OnServerEvent:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	for _, child in pairs(character:GetChildren()) do
		if child:IsA("BasePart")  then
			child.Transparency = 1 --Immediately sets to fully transparent
		end
	end
end)

The local script will handle telling the server whenever the player gives the "Q" input. We use :FireServer() function from the RemoteEvent we put into ReplicatedStorage.

--local script

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local remoteEvent = RS:WaitForChild("RemoteEvent")

UIS.InputBegan:Connect(function(inputObject, gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.Q then
		remoteEvent:FireServer() --Nothing passed to the server
	end
end)

From here is up to you how you wanted it. I really recommend UserInputService for the functionality it offers. There is also ContextActionService which allows you to bind multiple keys to functions.

Let me know if I missed anything or if you want anything explained.


##RemoteEvents

4 comments

0

This did not really work.

0

Have you followed the steps I gave to what you needed? You need the local script under the player (So like StarterPlayerScripts), the server script in ServerScriptService, and a RemoteEvent in ReplicatedStorage. What exactly do you have and did you edit anything?

0

I edited my post. The problem probably was that the RemoteEvent wasn't loading after the local script ran. See line 06 of my local script code to see the change.

0

It works now, thanks

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