Hi, I am trying to make a feature where you press the “R” key on your keyboard and it changes your avatar into a rat. The code doesn’t error, but all it does is make a rat fly out of the player’s HRP. This is run by a LocalScript parented to StarterCharacterScripts. I’ve tried moving it to a few different locations and nothing has worked. I’m still pretty new to scripting, so I’m not really sure what to do now.
Here is my code:
local UserInputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Player.Character:WaitForChild("HumanoidRootPart")
local morph = game.Workspace.Rat
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
morph:Clone()
morph.HumanoidRootPart.Anchored = false
morph:SetPrimaryPartCFrame(Character.PrimaryPart.CFrame)
Player.Character = morph
morph.Parent = workspace
print("works")
end
end)
If you think you know what went wrong, please let me know. Have a good day!
Hello!
Your script is mostly correct, however, you made two mistakes.
First one is that you placed the morph in the workspace, when it should only be put there after the character is changed. So, instead, you should place it somewhere such as the ServerStorage.
The other issue is that you used a LocalScript, instead of a ServerScript. For you to change a player character, it needs to be ran in the server, not in the client. Therefore, you can do the following:
Insert a RemoteEvent inside of ReplicatedStorage. This will allow the Client to communicate with the Server, so it knows when the player pressed the letter “R”. We’ll name this RemoteEvent “RatEvent”.
Create a ServerScript in the ServerScriptService, with the following code:
game:GetService("ReplicatedStorage").RatEvent.OnServerEvent:Connect(function(Player)
local Character = Player.Character
local HumanoidRootPart = Character.HumanoidRootPart
morph:Clone()
morph.HumanoidRootPart.Anchored = false
morph:SetPrimaryPartCFrame(Character.PrimaryPart.CFrame)
Player.Character = morph
morph.Parent = workspace
end)
Now, you must adapt your LocalScript so that it fires the RemoteEvent instead of changing the player character. Set it to the following:
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
-- Firing the RemoteEvent once the R key is pressed
game:GetService("ReplicatedStorage").RatEvent:FireServer()
end
end)
It worked! Thank you so much! Now, how would you make it to where once the player is changed into the rat, they can press “R” again and go back to their regular avatar?
You could have a variable in your code that changes when the player becomes a Rat. And, when they press the R button again, the script would call Player:LoadCharacter() to respawn them with their original character.
The server is the one who should keep track of these values, just so the client can’t potentially mislead it into doing something different. However, in this case, the client can do it as well.
It depends on your case, honestly. If you left the player character with the name of “Rat”, you could just check the Character name and do it that way.
local morph = game.ServerStorage.Rat
game:GetService("ReplicatedStorage").RatEvent.OnServerEvent:Connect(function(Player)
local Character = Player.Character
local HumanoidRootPart = Character.HumanoidRootPart
-- Check if the character name is "Rat"
if Character.Name == "Rat" then
local position = HumanoidRootPart.CFrame
Player:LoadCharacter() -- Respawn player
Player.Character.HumanoidRootPart.CFrame = position -- Teleport them back to where they were
return
end
morph:Clone()
morph.HumanoidRootPart.Anchored = false
morph:SetPrimaryPartCFrame(Character.PrimaryPart.CFrame)
Player.Character = morph
morph.Parent = workspace
end)
But is anything happening in specific? Is the person re-spawning but still with the Rat uniform (this would indicate that the Server is not detecting that the user is a Rat), or something different?
No need. You can have it all be in one event. The best way to do this is to have the server verify which character the person is currently set to. Maybe you could check for a specific part inside of the character instead of the name, or try to find a solution that works for your specific case.
You can become a rat, but you can’t change back into your Roblox character, even after I changed the server script with the code you provided. I tried restarting Studio and moving things around, but nothing’s changed.