Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

RemoteEvent printing the player's name?

Asked by
Rukreep 15
3 years ago

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)
0
You have to explain what your script does. sne_123456 439 — 3y
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. Rukreep 15 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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.

0
Yeah, but I need the player name so that I can do my script Rukreep 15 — 3y
0
@Rukreep In the script the first parameter is the player MarkedTomato 810 — 3y
Ad
Log in to vote
0
Answered by
Rukreep 15
3 years ago
Edited 3 years ago

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)

0
Nevermind I got it working Rukreep 15 — 3y

Answer this question