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)
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.
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)