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

Character is supposed to respawn/load when "f" is pressed. Help?

What it is supposed to do is respawn/load your character when you press "f." I haven't scripted in a month and I kind of forgot some stuff :') So here it is.

script.Parent = game.StarterGui
wait()
local k = game.Players.LocalPlayer:GetMouse()
k.KeyDown:connect(function(a)
if a:lower() == "f" then
local c = game.Players.LocalPlayer.Character
if c then
local h = c:findFirstChild("Humanoid")
if h then h:LoadCharacter()
end
end
end
end)

I'm not sure what to do so can someone help me?

5 comments

0

first of all use UserInputService Enum.KeyCode then you need a remote event then u reload your char from there

0

Server Script

0

Okay thanks that helped

0

What kind of advice is that?

0

use userinputservice

1 answer

✓ Accepted answer

If you still need help then I have created a script for this.

First, you would need to create a RemoteEvent and place that inside of ReplicatedStorage.

Then, create a LocalScript and place that inside of StarterGui with the following code inside:

LocalScript

local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local UIS = game:GetService("UserInputService")
  
UIS.InputBegan:Connect(function(key, gpe)
    if key.KeyCode == Enum.KeyCode.F and not gpe then --if player pressed the key F it would trigger this 
	    event:FireServer() -- fire the server so it could reload the player
    end
end)

The code will check if the player pressed the F key and it would fire the RemoteEvent

Lastly, create a Script and place that inside of ServerScriptService with the following code inside:

Server Script

local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

event.OnServerEvent:Connect(function(player) -- when event is fired
    player:LoadCharacter() -- respawn the player
end)

The code will fire after the player has pressed the F. key. LoadCharacter() will reload the player's character making it respawn.

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