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

Problems with CharacterLoad(), and RemoteEvents?

I have a LocalScript in a playergui's ScreenGui, and what I want it to do whenever you click on the button is that your HUD turns invisible, you change character appearance, then load character, then get the weapons and boosts. This is what I have.

button.MouseButton1Down:connect(function()
	if currentloadout.CharacterId.Value == 1 then
		player.CharacterAppearanceId = 264064675
		game:GetService("ReplicatedStorage").LoadCharacterEvent.OnServerEvent:connect(function(player)
    	player:LoadCharacter()
		local humanoid = player.Character:FindFirstChild("Humanoid")
		humanoid.MaxHealth = 150
		humanoid.Health = 150
		humanoid.WalkSpeed = 40
		heroweapons:FindFirstChild("HeroSword"):clone().Parent = player.Backpack
		hud:FindFirstChild("Deploy").Visible = false
		hud:FindFirstChild("LoadoutHud").Visible = false
		hud:FindFirstChild("Backgrounder").Visible = false
		end)
	end
end)

It doesn't load the character, so I added a RemoteEvent, the problem is, I'm still a bit new to RemoteEvents, and I'm still trying to find out how to use it properly.

Is this the correct way of using RemoteEvents? And are there bugs in the script that I need to fix?

Line four is cut off in this script, but it is actually

game:GetService("ReplicatedStorage").LoadCharacterEvent.OnSeverEvent:connect (function(player)

1 answer

This is an example on how you could use remote events. Also, Im gonna pretend that remote is a variable to a remotevent so i save time. LocalScript:

remote:FireServer('print("Hello world!")')

ServerScript:

remote.OnServerEvent:connect(function(plr, code) --The player module will always be here.
	loadstring(code) --This will print Hello World!
end)

It could work the other way around. ServerScript:

remote:FireClient(game.Players.Player1, 'Hello world!') --The player module must be the first argument that you want to send to.

LocalScript:

remote.OnClientEvent:connect(function(code)
	print(code)
end)

You can also fire all the clients!

remote:FireAllClients('Hello world!')

All localscripts:

remote.OnClientEvent:connect(function(code)
	print(code)
end)

You cannot do :FireAllServers() or whatever.

Let's get on what you should do. LocalScript:

button.MouseButton1Down:connect(function()
    if currentloadout.CharacterId.Value == 1 then
        player.CharacterAppearanceId = 264064675
        game:GetService("ReplicatedStorage").LoadCharacterEvent:FireServer()
        local humanoid = player.Character:FindFirstChild("Humanoid")
        humanoid.MaxHealth = 150
        humanoid.Health = 150
        humanoid.WalkSpeed = 40
        heroweapons:FindFirstChild("HeroSword"):clone().Parent = player.Backpack
        hud:FindFirstChild("Deploy").Visible = false
        hud:FindFirstChild("LoadoutHud").Visible = false
        hud:FindFirstChild("Backgrounder").Visible = false
        end)
    end
end)

ServerScript:

game:GetService("ReplicatedStorage").LoadCharacterEvent.OnSeverEvent:connect (function(plr)
	plr:LoadCharacter()
end)

3 comments

0

I'm starting to understand the way that remoteevents are being used in this situation. Are there other ways of using them? Please link the wikia to me for RemoteEvents. And one more thing, Output says that it loadcharacter() can only be called in backend server. So does that mean I do LoadCharacter on the serverscript?

1

Yes, as a LocalScript cannot call LoadCharacter

0

thanks hiim! You really helped me out!

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