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

RemoteEvent printing the player's name?

No matter what I do, read or anything, I can't understand what's going on. I can't really explain everything so, could anyone figure out what's wrong with this?

LocalScript:

local Tool = script.Parent;
local mouse = game.Players.LocalPlayer:GetMouse()
enabled = true

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local swapp = ReplicatedStorage:WaitForChild("CreatePartEvent")

function onEquipped()
	Tool.Handle.BrickColor = BrickColor.Random()
end

Tool.Activated:connect(function()
	local pos = mouse.Hit+Vector3.new(0,3,0)
	pos = CFrame.new(pos.X,pos.Y,pos.Z)

	if mouse.Target.Parent:FindFirstChild("Humanoid") ~= nil then
		local player = game.Players.LocalPlayer.Name
		local mousetargetto = mouse.Target.Parent.HumanoidRootPart
		swapp:FireServer(player,mousetargetto)
		game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = pos
		
		end
end)

script.Parent.Equipped:connect(onEquipped)


Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent", ReplicatedStorage)
createPartEvent.Name = "CreatePartEvent"

local function onServerEvent(player,mousetargetto)
	print(mousetargetto)
	mousetargetto.Position = game.Workspace[player].HumanoidRootPart.Position
	end
createPartEvent.OnServerEvent:Connect(onServerEvent)

2 comments

0

You have to explain what your script does.

0

It just teleports to another player with a humanoidrootpart. What I am trying to do is that the player will switch position with the other player. Kind of a body swap potion sort of thing.

2 answers

When you fire a RemoteEvent to the server, you don't need to pass the player argument because you are firing to the server, not a client. FireServer and OnServerEvent also view the parameters since that's what you're confused about.

LocalScript

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

remoteEvent:FireServer()

Script

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

remoteEvent.OnServerEvent:Connect(function(player)
  print(player.Name)
end)

As you see, I didn't define it. Always remember that OnServerEvent that the first parameter is always the client, and the parameters after that are your arguments.

2 comments

0

Yeah, but I need the player name so that I can do my script

0

@Rukreep In the script the first parameter is the player

I made it work. Problem is that the player is getting teleported through the client, not the server. Any help? Localscript:

local Tool = script.Parent;
local mouse = game.Players.LocalPlayer:GetMouse()
enabled = true

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local swapp = ReplicatedStorage:WaitForChild("CreatePartEvent")



Tool.Activated:connect(function()

		local mousetargetto = mouse.Target
		swapp:FireServer(mousetargetto)

	
end)





Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent", ReplicatedStorage)
createPartEvent.Name = "CreatePartEvent"

local function onServerEvent(player,mousetargetto)

	if mousetargetto.Parent:FindFirstChild("Humanoid") ~= nil then

	local pos = player.Character.HumanoidRootPart.Position
	local targetpos = mousetargetto.Parent.HumanoidRootPart.Position
	
	player.Character.HumanoidRootPart.Position = targetpos
	mousetargetto.Parent.HumanoidRootPart.Position = pos	
	end
end
createPartEvent.OnServerEvent:Connect(onServerEvent)
function onEquipped()
	script.Parent.Handle.BrickColor = BrickColor.Random()
end

script.Parent.Equipped:connect(onEquipped)

1 comment

0

Nevermind I got it working

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