Search code examples
luascriptingroblox

Lua- Printing wrong item


I tried to send the mouse target in a RemoteEvent, but when I try to print it after it gets sent it prints my username instead of the target. I tried printing before I sent the target and it didn't say my userrname.

This is the code for sending the target.

local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

tool.Activated:Connect(function()
        local target = mouse.Target
        game.ReplicatedStorage.Mine:FireServer(target)
end)

That code is inside a tool which is inside the StarterPack. This is the receiving code.

    game.ReplicatedStorage.Mine.OnServerEvent:Connect(function(target)
        print(target)
    end)

That code is inside ServerScriptService. It is meant to print the part I clicked on, but instead it prints my username.


Solution

  • The first parameter is always the instance of the player that fired the event. So your target value becomes the second parameter.

        game.ReplicatedStorage.Mine.OnServerEvent:Connect(function(player,target)
            print(target)
        end)