Need help about understanding RemoteEvents

Hi, I need help to understand to how to use RemoteEvents:
I want to make a script that when a players touuches a specific part, it show a GUI message.

I’m kinda at the learning phase because I litterally need other scripters to learn how to script :sweat_smile:

So I had a script that does the same thing on the server side but it seems it’s a bad idea to other people so I decided to make another script.

Here’s the script so far: (I’m sure that 99% it’s a obvious error but i’m a noobie lol)

Server side:

game.Workspace.Islands.IslandMain.MainIsland.Reset.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		game:WaitForChild("ReplicatedStorage"):WaitForChild("RemoteThings"):WaitForChild("RemoteTeleport"):FireClient(game:WaitForChild("Players").LocalPlayer)
	end
end)

Client side:

-- Script de Téléportation par Mr_RainBowsYT, fait le 19/05/20 à 23:30 
--Ne pas modifier ce script si rien n'est cassé. Merci
local Spawn = game.Workspace.Islands.IslandMain.MainIsland.SpawnLocation
local AA = true
local Players = game:GetService("Players")
local TweenInformation = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.Out)



-- AA est un indicateur pour savoir quand un joueur est téléporté pour éviter de créer
--plusieurs requêtes de téléportation.

game:WaitForChild("ReplicatedStorage"):WaitForChild("RemoteThings"):WaitForChild("RemoteTeleport"):OnClientEvent():Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
        local warnedGui = player:WaitForChild("PlayerGui"):WaitForChild("OceanFallWarned")
		if AA == true then
			AA = false
			hit.Parent.HumanoidRootPart.CFrame = Spawn.CFrame*CFrame.new(0, 4, 0)
			warnedGui.Enabled = true
			warnedGui.Background:TweenPosition(UDim2.new(0.5, 0, 0.02, 0),"Out","Back",1)
			wait(3)
			warnedGui.Background:TweenPosition(UDim2.new(0.5, 0, -0.107, 0),"In","Back",1)
			wait(1)
			AA = true
			warnedGui.Enabled = false
		end
    end)

(Don’t worry about the comment I wrote it in french because my native language is french)

If you could help me, I will be very thankful because i could understand RemoteEvents now :smiley:
(I tried looking at the API documentation and YT videos but I understand nothing :frowning: )

error I am getting :

image

Path:

Edit: I’m currently reading all the replies, i’m really thankful for all the people helping me :smiley:

4 Likes

first of all you can’t use localplayer in a server script, use GetPlayerFromCharacter like this

if hit.Parent:FindFirstChild("Humanoid") then
      local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
       remoteEvent:FireClient(player) -- This will work unless the character is an npc]
    end
end

and use:

game:WaitForChild("ReplicatedStorage"):WaitForChild("RemoteThings"):WaitForChild("RemoteTeleport"):OnClientEvent:Connect(function()

instead of:

game:WaitForChild("ReplicatedStorage"):WaitForChild("RemoteThings"):WaitForChild("RemoteTeleport"):OnClientEvent():Connect(function(hit)

because its an event and you have to connect it, and in the localscript, you can just use localPlayer, and you can’t get the player that is put into the fireclient as an argument

Edit: Noticed I made some mistakes in the script :joy: I have fixed it

3 Likes

That would error too, be right back, i will go format the code in studio.

Edit:

-- ServerScript in ServerScriptService.
game.Workspace.Islands.IslandMain.MainIsland.Reset.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		game:GetService("ReplicatedStorage"):WaitForChild("RemoteThings").RemoteTeleport:FireClient(plr)
	end
end)

Edit2:

-- Script de Téléportation par Mr_RainBowsYT, fait le 19/05/20 à 23:30 
--Ne pas modifier ce script si rien n'est cassé. Merci
local Spawn = game.Workspace.Islands.IslandMain.MainIsland.SpawnLocation
local AA = false
local Players = game:GetService("Players")
local TweenInformation = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.Out)



-- AA est un indicateur pour savoir quand un joueur est téléporté pour éviter de créer
--plusieurs requêtes de téléportation.

game:GetService("ReplicatedStorage"):WaitForChild("RemoteThings").RemoteTeleport.OnClientEvent:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
    local warnedGui = player.PlayerGui.OceanFallWarned
	if AA == false then
		AA = true
		hit.Parent.HumanoidRootPart.CFrame = Spawn.CFrame*CFrame.new(0, 4, 0) -- I don't know but.. This part may error, test the script so we have the results.
		warnedGui.Enabled = true
		warnedGui.Background:TweenPosition(UDim2.new(0.5, 0, 0.02, 0),"Out","Back",1)
		wait(3)
		warnedGui.Background:TweenPosition(UDim2.new(0.5, 0, -0.107, 0),"In","Back",1)
		wait(1)
		AA = false
		warnedGui.Enabled = false
	end
end)
4 Likes

It still may not hurt to check for a Humanoid since that will remove most cases of random parts or other debris and you never know when someone may have a name identical to a part’s name.

2 Likes

like @xZylter said, you can to check for a humanoid, idk if it will error, but try to

2 Likes

Your script still is not the best to use since you assume that you will return a truthful value in all scenarios. It’s still a good idea to ensure GetPlayerFromCharacter did return a real value.

3 Likes

Accidently deleted it… Whoops.

1 Like

Ik that, but I have been typing and coding for a few hours, so I forgot, lemme edit it

1 Like

I’am pretty sure :GetPlayerFromCharacter() would check if the part/character in its parameter, is actually an player’s character, but who knows right?

1 Like

It’s name is self-explanatory, but it returns nil if the hit.Parent is an npc, I’m pretty sure, (not confirmed)

1 Like

I’am just going to say the errors that you done in the code you gave:

:OnClientEvent? Wait, isn’t that an event?

no, the second one is his errors, lol, just to explain.

idk what you mean by that, I did make it an event

1 Like

The Developer page for :GetPlayerFromCharacter() says it will return nil if there is no Player associated with the given parameter.

2 Likes

Yes… I literally have gone to the GetPlayerFromCharacter page… Before you even said that.

image

2 Likes
-- Script de Téléportation par Mr_RainBowsYT, fait le 19/05/20 à 23:30 
--Ne pas modifier ce script si rien n'est cassé. Merci
local Spawn = game.Workspace.Islands.IslandMain.MainIsland.SpawnLocation
local AA = false
local Players = game:GetService("Players")
local TweenInformation = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.Out)



-- AA est un indicateur pour savoir quand un joueur est téléporté pour éviter de créer
--plusieurs requêtes de téléportation.

game:GetService("ReplicatedStorage"):WaitForChild("RemoteThings").RemoteTeleport.OnClientEvent:Connect(function(plr)
	local warnedGui = plr.PlayerGui.OceanFallWarned
	local char = plr.Character or plr.CharacterAdded:Wait()
	if AA == false then
		AA = true
		char:WaitForChild("HumanoidRootPart").CFrame = Spawn.CFrame + Vector3.new(0,4,0) -- I don't know but.. This part may error, test the script so we have the results.
		warnedGui.Enabled = true
		warnedGui.Background:TweenPosition(UDim2.new(0.5, 0, 0.02, 0),"Out","Back",1)
		wait(3)
		warnedGui.Background:TweenPosition(UDim2.new(0.5, 0, -0.107, 0),"In","Back",1)
		wait(1)
		AA = false
		warnedGui.Enabled = false
	end
end)

@OP/@Mr_RainBowsYT , use this script instead.

1 Like

Ok I tried the script but I think it’s the same error as the 1st one, i’m trying to see if putting part of the old script works

btw I still don’t understand why there is any error messages in Output lol 1st time i’m seeing this

I’am checking something at the devhub, it seems that you forgot an statement at the TweenPosition…

Edit:

-- Script de Téléportation par Mr_RainBowsYT, fait le 19/05/20 à 23:30 
--Ne pas modifier ce script si rien n'est cassé. Merci
local Spawn = game.Workspace.Islands.IslandMain.MainIsland.SpawnLocation
local AA = false
local Players = game:GetService("Players")
local TweenInformation = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.Out)



-- AA est un indicateur pour savoir quand un joueur est téléporté pour éviter de créer
--plusieurs requêtes de téléportation.

game:GetService("ReplicatedStorage"):WaitForChild("RemoteThings").RemoteTeleport.OnClientEvent:Connect(function(plr)
	local warnedGui = plr.PlayerGui.OceanFallWarned
	local char = plr.Character or plr.CharacterAdded:Wait()
	if AA == false then
		AA = true
		char:WaitForChild("HumanoidRootPart").CFrame = Spawn.CFrame + Vector3.new(0,4,0) -- I don't know but.. This part may error, test the script so we have the results.
		warnedGui.Enabled = true
		warnedGui.Background:TweenPosition(UDim2.new(0.5, 0, 0.02, 0),"Out","Back",1,false,nil)
		wait(3)
		warnedGui.Background:TweenPosition(UDim2.new(0.5, 0, -0.107, 0),"In","Back",1,false,nil)
		wait(1)
		AA = false
		warnedGui.Enabled = false
	end
end)

Try the code above.

Edit: The code didn’t work, but after the @OP doing some searches, he managed to do it.

1 Like